Ejemplo n.º 1
0
        /// <summary>
        ///     Creates a request elements concatentation value to send with a request.
        ///     This is also known as the signature base.
        /// </summary>
        /// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.1.3" />
        /// <seealso cref="http://oauth.net/core/1.0#sig_base_example" />
        /// <param name="method">The request's HTTP method type</param>
        /// <param name="url">The request URL</param>
        /// <param name="parameters">The request's parameters</param>
        /// <returns>A signature base string</returns>
        public static string ConcatenateRequestElements(string method, string url, WebParameterCollection parameters)
        {
            var sb = new StringBuilder();

            // Separating &'s are not URL encoded
            var requestMethod     = method.ToUpper().Then("&");
            var requestUrl        = UrlEncodeRelaxed(ConstructRequestUrl(url.AsUri())).Then("&");
            var requestParameters = UrlEncodeRelaxed(NormalizeRequestParameters(parameters));

            sb.Append(requestMethod);
            sb.Append(requestUrl);
            sb.Append(requestParameters);

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Sorts a <see cref="WebParameterCollection" /> by name, and then value if equal.
        /// </summary>
        /// <param name="parameters">A collection of parameters to sort</param>
        /// <returns>A sorted parameter collection</returns>
        public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters)
        {
            var copy       = new WebParameterCollection(parameters);
            var exclusions = copy.Where(n => n.Name.EqualsIgnoreCase("oauth_signature"));

            copy.RemoveAll(exclusions);
            copy.ForEach(p =>
            {
                p.Name  = UrlEncodeStrict(p.Name);
                p.Value = UrlEncodeStrict(p.Value);
            });
            copy.Sort((x, y) => string.CompareOrdinal(x.Name, y.Name) != 0
                ? string.CompareOrdinal(x.Name, y.Name)
                : string.CompareOrdinal(x.Value, y.Value));

            return(copy);
        }
Ejemplo n.º 3
0
        private void AddXAuthParameters(ICollection <WebPair> parameters, string timestamp, string nonce)
        {
            var authParameters = new WebParameterCollection
            {
                new WebPair("x_auth_username", ClientUsername),
                new WebPair("x_auth_password", ClientPassword),
                new WebPair("x_auth_mode", "client_auth"),
                new WebPair("oauth_consumer_key", ConsumerKey),
                new WebPair("oauth_signature_method", SignatureMethod.ToRequestValue()),
                new WebPair("oauth_timestamp", timestamp),
                new WebPair("oauth_nonce", nonce),
                new WebPair("oauth_version", Version ?? "1.0")
            };

            foreach (var authParameter in authParameters)
            {
                parameters.Add(authParameter);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Generates a <see cref="OAuthWebQueryInfo" /> instance to pass to an
        ///     <see cref="IAuthenticator" /> for the purpose of exchanging user credentials
        ///     for an access token authorized by the user at the Service Provider site.
        /// </summary>
        /// <param name="method">The HTTP method for the intended request</param>
        /// <seealso cref="http://tools.ietf.org/html/draft-dehora-farrell-oauth-accesstoken-creds-00#section-4" />
        /// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
        public virtual OAuthWebQueryInfo BuildClientAuthAccessTokenInfo(string method,
                                                                        WebParameterCollection parameters)
        {
            ValidateClientAuthAccessRequestState();

            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }

            var uri       = new Uri(AccessTokenUrl);
            var timestamp = OAuthTools.GetTimestamp();
            var nonce     = OAuthTools.GetNonce();

            AddXAuthParameters(parameters, timestamp, nonce);

            var signatureBase = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), parameters);
            var signature     = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase,
                                                        ConsumerSecret);

            var info = new OAuthWebQueryInfo
            {
                WebMethod          = method,
                ParameterHandling  = ParameterHandling,
                ClientMode         = "client_auth",
                ClientUsername     = ClientUsername,
                ClientPassword     = ClientPassword,
                ConsumerKey        = ConsumerKey,
                SignatureMethod    = SignatureMethod.ToRequestValue(),
                SignatureTreatment = SignatureTreatment,
                Signature          = signature,
                Timestamp          = timestamp,
                Nonce          = nonce,
                Version        = Version ?? "1.0",
                TokenSecret    = TokenSecret,
                ConsumerSecret = ConsumerSecret
            };

            return(info);
        }
Ejemplo n.º 5
0
        private void AddAuthParameters(ICollection <WebPair> parameters, string timestamp, string nonce)
        {
            var authParameters = new WebParameterCollection
            {
                new WebPair("oauth_consumer_key", ConsumerKey),
                new WebPair("oauth_nonce", nonce),
                new WebPair("oauth_signature_method", SignatureMethod.ToRequestValue()),
                new WebPair("oauth_timestamp", timestamp),
                new WebPair("oauth_version", Version ?? "1.0")
            };

            if (!Token.IsNullOrBlank())
            {
                authParameters.Add(new WebPair("oauth_token", Token));
            }

            if (!CallbackUrl.IsNullOrBlank())
            {
                authParameters.Add(new WebPair("oauth_callback", CallbackUrl));
            }

            if (!Verifier.IsNullOrBlank())
            {
                authParameters.Add(new WebPair("oauth_verifier", Verifier));
            }

            if (!SessionHandle.IsNullOrBlank())
            {
                authParameters.Add(new WebPair("oauth_session_handle", SessionHandle));
            }

            foreach (var authParameter in authParameters)
            {
                parameters.Add(authParameter);
            }
        }