Beispiel #1
0
        /// <summary>
        /// OAuth1.0a - 6.2.1. - Consumer Directs the User to the Service Provider
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual string GetAuthorizationUrl(OAuthParameters parameters)
        {
            string authorizeTokenUrl = parameters.Url.TrimEnd('/');
            string requestToken      = parameters.Values["oauth_token"];

            string authorzationUrl = Uri.UnescapeDataString($"{authorizeTokenUrl}?oauth_consumer_key={_config.ConsumerKey}&oauth_token={requestToken}");

            if (parameters.Values.TryGetValue("oauth_callback", out string oauth_callback))
            {
                authorzationUrl += $"&oauth_callback={oauth_callback}";
            }

            return(authorzationUrl);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters"></param>
        /// <returns></returns>
        protected virtual async Task <T> SendTokenRequestAsync <T>(OAuthParameters parameters) where T : TokenInfo, new()
        {
            if (!(parameters.HttpMethod.Equals(HttpMethod.Get) || parameters.HttpMethod.Equals(HttpMethod.Post)))
            {
                throw new NotImplementedException($"GetRequestTokenAsync by method: {parameters.HttpMethod.Method} is not implemented.");
            }
            //

            var requestParameters = parameters.Values;

            if (requestParameters.TryGetValue("oauth_token_secret", out string oauth_token_secret))
            {
                requestParameters.Remove("oauth_token_secret");
            }

            string signatureBaseString = GetSignatureBaseString(parameters.HttpMethod, parameters.Url, requestParameters);
            string signature           = GetSignature(signatureBaseString, _config.ConsumerSecret, oauth_token_secret);

            requestParameters.Add("oauth_signature", Uri.EscapeDataString(signature));

            string requestUrl = parameters.Binding == OAuthParametersBinding.QueryString
            ? $"{parameters.Url}?{ConcatParameterList(requestParameters, "&", quotedValues: true)}"
            : parameters.Url;

            var oauthHeader = parameters.Binding == OAuthParametersBinding.Header
            ? GetOAuthHeader(parameters.HttpMethod, parameters.Url, requestParameters)
            : null;

            string postData = parameters.Binding == OAuthParametersBinding.Body
            ? $"{ConcatParameterList(requestParameters, "&", quotedValues: true)}"
            : null;

            string responseText = await SendOAuthDataAsync(parameters.HttpMethod, requestUrl, oauthHeader, postData);

            if (!responseText.ToLowerInvariant().Contains("oauth_token"))
            {
                var builder = new StringBuilder($"An error occured when trying to {parameters.HttpMethod.Method}: {parameters.Url}.");
                builder.Append($"The response was: >> { responseText} <<\n\n");
                throw new Exception(builder.ToString());
            }

            return(GetTokenInfo <T>(responseText));
        }
Beispiel #3
0
 /// <summary>
 /// This behavior is optional. Implement as needed via an override in a sub-class.
 /// </summary>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public virtual Task <AccessTokenInfo> RevokeAccessTokenAsync(OAuthParameters parameters)
 {
     throw new NotImplementedException("This behavior is optional. Implement as needed via an override in a sub-class.");
 }
Beispiel #4
0
        /// <summary>
        /// OAuth1.0a - 6.3.1. - Consumer Requests an Access Token
        /// </summary>
        /// <param name="requestToken"></param>
        /// <param name="requestTokenSecret"></param>
        /// <param name="verifier"></param>
        /// <returns></returns>
        public virtual async Task <AccessTokenInfo> GetAccessTokenAsync(OAuthParameters parameters)
        {
            parameters.Values = GetOAuthRequestParameters(parameters.Values);

            return(await SendTokenRequestAsync <AccessTokenInfo>(parameters));
        }