Beispiel #1
0
        /// <summary>
        /// Generates a <see cref="OAuthWebQueryInfo"/> instance to pass to an
        /// <see cref="IAuthenticator" /> for the purpose of exchanging a request token
        /// 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://oauth.net/core/1.0#anchor9"/>
        /// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
        public virtual OAuthWebQueryInfo BuildAccessTokenInfo(string method, WebParameterCollection parameters)
        {
            ValidateAccessRequestState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            var uri       = new Uri(AccessTokenUrl);
            var timestamp = OAuthTools.GetTimestamp();
            var nonce     = OAuthTools.GetNonce();

            AddAuthParameters(parameters, timestamp, nonce);
            var signatureBase = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), parameters);
            var signature     = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret, TokenSecret);
            var info          = new OAuthWebQueryInfo
            {
                WebMethod          = method,
                ParameterHandling  = ParameterHandling,
                ConsumerKey        = ConsumerKey,
                Token              = Token,
                SignatureMethod    = SignatureMethod.ToRequestValue(),
                SignatureTreatment = SignatureTreatment,
                Signature          = signature,
                Timestamp          = timestamp,
                Nonce              = nonce,
                Version            = Version ?? "1.0",
                Verifier           = Verifier,
                Callback           = CallbackUrl,
                TokenSecret        = TokenSecret,
                ConsumerSecret     = ConsumerSecret,
            };

            return(info);
        }
Beispiel #2
0
        /// <summary>
        /// Generates a <see cref="OAuthWebQueryInfo"/> instance to pass to an
        /// <see cref="IAuthenticator" /> for the purpose of requesting an
        /// unauthorized request token.
        /// </summary>
        /// <param name="method">The HTTP method for the intended request</param>
        /// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
        /// <seealso cref="http://oauth.net/core/1.0#anchor9"/>
        /// <returns></returns>
        public virtual OAuthWebQueryInfo BuildRequestTokenInfo(string method, WebParameterCollection parameters)
        {
            ValidateTokenRequestState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            var timestamp = OAuthTools.GetTimestamp();
            var nonce     = OAuthTools.GetNonce();

            AddAuthParameters(parameters, timestamp, nonce);
            var signatureBase = OAuthTools.ConcatenateRequestElements(method, RequestTokenUrl, parameters);
            var signature     = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret);
            var info          = new OAuthWebQueryInfo
            {
                WebMethod          = method,
                ParameterHandling  = ParameterHandling,
                ConsumerKey        = ConsumerKey,
                SignatureMethod    = SignatureMethod.ToRequestValue(),
                SignatureTreatment = SignatureTreatment,
                Signature          = signature,
                Timestamp          = timestamp,
                Nonce          = nonce,
                Version        = Version ?? "1.0",
                Callback       = OAuthTools.UrlEncodeRelaxed(CallbackUrl ?? ""),
                TokenSecret    = TokenSecret,
                ConsumerSecret = ConsumerSecret
            };

            return(info);
        }
Beispiel #3
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);
            }
        }
Beispiel #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);
        }
        private void AddAuthParameters(ICollection <WebPair> parameters, string timestamp, string nonce)
        {
            WebParameterCollection webParameterCollection = new WebParameterCollection();

            webParameterCollection.Add(new WebPair("oauth_consumer_key", ConsumerKey));
            webParameterCollection.Add(new WebPair("oauth_nonce", nonce));
            webParameterCollection.Add(new WebPair("oauth_signature_method", SignatureMethod.ToRequestValue()));
            webParameterCollection.Add(new WebPair("oauth_timestamp", timestamp));
            webParameterCollection.Add(new WebPair("oauth_version", Version ?? "1.0"));
            WebParameterCollection webParameterCollection2 = webParameterCollection;

            if (!Token.IsNullOrBlank())
            {
                webParameterCollection2.Add(new WebPair("oauth_token", Token));
            }
            if (!CallbackUrl.IsNullOrBlank())
            {
                webParameterCollection2.Add(new WebPair("oauth_callback", CallbackUrl));
            }
            if (!Verifier.IsNullOrBlank())
            {
                webParameterCollection2.Add(new WebPair("oauth_verifier", Verifier));
            }
            if (!SessionHandle.IsNullOrBlank())
            {
                webParameterCollection2.Add(new WebPair("oauth_session_handle", SessionHandle));
            }
            foreach (WebPair item in webParameterCollection2)
            {
                parameters.Add(item);
            }
        }
        public virtual OAuthWebQueryInfo BuildClientAuthAccessTokenInfo(string method, WebParameterCollection parameters)
        {
            ValidateClientAuthAccessRequestState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            Uri    uri       = new Uri(AccessTokenUrl);
            string timestamp = OAuthTools.GetTimestamp();
            string nonce     = OAuthTools.GetNonce();

            AddXAuthParameters(parameters, timestamp, nonce);
            string            signatureBase     = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), parameters);
            string            signature         = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret);
            OAuthWebQueryInfo oAuthWebQueryInfo = new OAuthWebQueryInfo();

            oAuthWebQueryInfo.WebMethod          = method;
            oAuthWebQueryInfo.ParameterHandling  = ParameterHandling;
            oAuthWebQueryInfo.ClientMode         = "client_auth";
            oAuthWebQueryInfo.ClientUsername     = ClientUsername;
            oAuthWebQueryInfo.ClientPassword     = ClientPassword;
            oAuthWebQueryInfo.ConsumerKey        = ConsumerKey;
            oAuthWebQueryInfo.SignatureMethod    = SignatureMethod.ToRequestValue();
            oAuthWebQueryInfo.SignatureTreatment = SignatureTreatment;
            oAuthWebQueryInfo.Signature          = signature;
            oAuthWebQueryInfo.Timestamp          = timestamp;
            oAuthWebQueryInfo.Nonce          = nonce;
            oAuthWebQueryInfo.Version        = (Version ?? "1.0");
            oAuthWebQueryInfo.TokenSecret    = TokenSecret;
            oAuthWebQueryInfo.ConsumerSecret = ConsumerSecret;
            return(oAuthWebQueryInfo);
        }
        public virtual OAuthWebQueryInfo BuildRequestTokenInfo(string method, WebParameterCollection parameters)
        {
            ValidateTokenRequestState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            string timestamp = OAuthTools.GetTimestamp();
            string nonce     = OAuthTools.GetNonce();

            AddAuthParameters(parameters, timestamp, nonce);
            string            signatureBase     = OAuthTools.ConcatenateRequestElements(method, RequestTokenUrl, parameters);
            string            signature         = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret);
            OAuthWebQueryInfo oAuthWebQueryInfo = new OAuthWebQueryInfo();

            oAuthWebQueryInfo.WebMethod          = method;
            oAuthWebQueryInfo.ParameterHandling  = ParameterHandling;
            oAuthWebQueryInfo.ConsumerKey        = ConsumerKey;
            oAuthWebQueryInfo.SignatureMethod    = SignatureMethod.ToRequestValue();
            oAuthWebQueryInfo.SignatureTreatment = SignatureTreatment;
            oAuthWebQueryInfo.Signature          = signature;
            oAuthWebQueryInfo.Timestamp          = timestamp;
            oAuthWebQueryInfo.Nonce          = nonce;
            oAuthWebQueryInfo.Version        = (Version ?? "1.0");
            oAuthWebQueryInfo.Callback       = OAuthTools.UrlEncodeRelaxed(CallbackUrl ?? "");
            oAuthWebQueryInfo.TokenSecret    = TokenSecret;
            oAuthWebQueryInfo.ConsumerSecret = ConsumerSecret;
            return(oAuthWebQueryInfo);
        }
Beispiel #8
0
        WebPairCollection GenerateAuthParameters(string timestamp, string nonce)
        {
            var authParameters = new WebPairCollection
            {
                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.IsEmpty())
            {
                authParameters.Add(new WebPair("oauth_token", Token));
            }

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

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

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

            return(authParameters);
        }
Beispiel #9
0
        public virtual OAuthWebQueryInfo BuildProtectedResourceInfo(string method, WebParameterCollection parameters, string url)
        {
            ValidateProtectedResourceState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            // Include url parameters in query pool
            var uri = new Uri(url);

#if !SILVERLIGHT && !WINDOWS_PHONE
            var urlParameters = HttpUtility.ParseQueryString(uri.Query);
#else
            var urlParameters = uri.Query.ParseQueryString();
#endif
#if !SILVERLIGHT && !WINDOWS_PHONE
            foreach (var parameter in urlParameters.AllKeys)
#else
            foreach (var parameter in urlParameters.Keys)
#endif
            {
                switch (method.ToUpperInvariant())
                {
                case "POST":
                    parameters.Add(new HttpPostParameter(parameter, urlParameters[parameter]));
                    break;

                default:
                    parameters.Add(parameter, urlParameters[parameter]);
                    break;
                }
            }
            var timestamp = OAuthTools.GetTimestamp();
            var nonce     = OAuthTools.GetNonce();
            AddAuthParameters(parameters, timestamp, nonce);
            var signatureBase = OAuthTools.ConcatenateRequestElements(method, url, parameters);
            var signature     = OAuthTools.GetSignature(
                SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret, TokenSecret
                );
            var info = new OAuthWebQueryInfo
            {
                WebMethod          = method,
                ParameterHandling  = ParameterHandling,
                ConsumerKey        = ConsumerKey,
                Token              = Token,
                SignatureMethod    = SignatureMethod.ToRequestValue(),
                SignatureTreatment = SignatureTreatment,
                Signature          = signature,
                Timestamp          = timestamp,
                Nonce              = nonce,
                Version            = Version ?? "1.0",
                Callback           = CallbackUrl,
                ConsumerSecret     = ConsumerSecret,
                TokenSecret        = TokenSecret
            };
            return(info);
        }
Beispiel #10
0
        public virtual OAuthWebQueryInfo BuildProtectedResourceInfo(string method, WebParameterCollection parameters,
                                                                    string url)
        {
            ValidateProtectedResourceState();

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

            // Include url parameters in query pool...
            var uri           = new Uri(url);
            var urlParameters = System.Web.HttpUtility.ParseQueryString(uri.Query);

            //...unless they were already present at some point, such as being added to the request object
            foreach (var parameter in urlParameters.AllKeys.Where(k => !parameters.Names.Contains(k)))
            {
                switch (method.ToUpperInvariant())
                {
                case "POST":
                    parameters.Add(new HttpPostParameter(parameter, urlParameters[parameter]));
                    break;

                default:
                    parameters.Add(parameter, urlParameters[parameter]);
                    break;
                }
            }

            var timestamp = OAuthTools.GetTimestamp();
            var nonce     = OAuthTools.GetNonce();

            AddAuthParameters(parameters, timestamp, nonce);

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

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

            return(info);
        }
Beispiel #11
0
 WebPairCollection GenerateXAuthParameters(string timestamp, string nonce)
 => new WebPairCollection
 {
     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")
 };
        public virtual OAuthWebQueryInfo BuildProtectedResourceInfo(string method, WebParameterCollection parameters, string url)
        {
            ValidateProtectedResourceState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            Uri uri = new Uri(url);
            NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(uri.Query);

            string[] allKeys = nameValueCollection.AllKeys;
            foreach (string name in allKeys)
            {
                string text = method.ToUpperInvariant();
                if (text != null && text == "POST")
                {
                    parameters.Add(new HttpPostParameter(name, nameValueCollection[name]));
                }
                else
                {
                    parameters.Add(name, nameValueCollection[name]);
                }
            }
            string timestamp = OAuthTools.GetTimestamp();
            string nonce     = OAuthTools.GetNonce();

            AddAuthParameters(parameters, timestamp, nonce);
            string            signatureBase     = OAuthTools.ConcatenateRequestElements(method, url, parameters);
            string            signature         = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret, TokenSecret);
            OAuthWebQueryInfo oAuthWebQueryInfo = new OAuthWebQueryInfo();

            oAuthWebQueryInfo.WebMethod          = method;
            oAuthWebQueryInfo.ParameterHandling  = ParameterHandling;
            oAuthWebQueryInfo.ConsumerKey        = ConsumerKey;
            oAuthWebQueryInfo.Token              = Token;
            oAuthWebQueryInfo.SignatureMethod    = SignatureMethod.ToRequestValue();
            oAuthWebQueryInfo.SignatureTreatment = SignatureTreatment;
            oAuthWebQueryInfo.Signature          = signature;
            oAuthWebQueryInfo.Timestamp          = timestamp;
            oAuthWebQueryInfo.Nonce              = nonce;
            oAuthWebQueryInfo.Version            = (Version ?? "1.0");
            oAuthWebQueryInfo.Callback           = CallbackUrl;
            oAuthWebQueryInfo.ConsumerSecret     = ConsumerSecret;
            oAuthWebQueryInfo.TokenSecret        = TokenSecret;
            return(oAuthWebQueryInfo);
        }
Beispiel #13
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);
            }
        }
        private void AddXAuthParameters(ICollection <WebPair> parameters, string timestamp, string nonce)
        {
            WebParameterCollection webParameterCollection = new WebParameterCollection();

            webParameterCollection.Add(new WebPair("x_auth_username", ClientUsername));
            webParameterCollection.Add(new WebPair("x_auth_password", ClientPassword));
            webParameterCollection.Add(new WebPair("x_auth_mode", "client_auth"));
            webParameterCollection.Add(new WebPair("oauth_consumer_key", ConsumerKey));
            webParameterCollection.Add(new WebPair("oauth_signature_method", SignatureMethod.ToRequestValue()));
            webParameterCollection.Add(new WebPair("oauth_timestamp", timestamp));
            webParameterCollection.Add(new WebPair("oauth_nonce", nonce));
            webParameterCollection.Add(new WebPair("oauth_version", Version ?? "1.0"));
            WebParameterCollection webParameterCollection2 = webParameterCollection;

            foreach (WebPair item in webParameterCollection2)
            {
                parameters.Add(item);
            }
        }
Beispiel #15
0
        public virtual OAuthWebQueryInfo BuildProtectedResourceInfo(WebMethod method,
                                                                    WebParameterCollection parameters,
                                                                    string url)
        {
            ValidateProtectedResourceState();

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

            // Include url parameters in query pool
            var uri = new Uri(url);

#if !SILVERLIGHT && !MonoTouch
            var urlParameters = ParseQuery(uri.Query);
#else
            var urlParameters = uri.Query.ParseQueryString();
#endif

#if !SILVERLIGHT && !MonoTouch
            foreach (var parameter in urlParameters)
#else
            foreach (var parameter in urlParameters.Keys)
#endif
            {
                switch (method)
                {
                case WebMethod.Post:
                    parameters.Add(new HttpPostParameter(parameter.Key, parameter.Value));
                    break;

                default:
                    parameters.Add(parameter.Key, parameter.Value);
                    break;
                }
            }

            var timestamp = OAuthTools.GetTimestamp();
            var nonce     = OAuthTools.GetNonce();

            // [DC] Make a copy of the parameters so that the signature double-encode isn't used
            var copy = new WebParameterCollection();
            foreach (var parameter in parameters)
            {
                copy.Add(new WebPair(parameter.Name, parameter.Value));
            }

            AddAuthParameters(copy, timestamp, nonce);

            // [DC] Escape parameters at this point; do not escape again if recalculating
            var signatureBase = OAuthTools.ConcatenateRequestElements(method, url, copy);
            var signature     = OAuthTools.GetSignature(
                SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret, TokenSecret
                );

            var info = new OAuthWebQueryInfo
            {
                WebMethod          = method,
                ParameterHandling  = ParameterHandling,
                ConsumerKey        = ConsumerKey,
                Token              = Token,
                SignatureMethod    = SignatureMethod.ToRequestValue(),
                SignatureTreatment = SignatureTreatment,
                Signature          = signature,
                Timestamp          = timestamp,
                Nonce              = nonce,
                Version            = Version ?? "1.0",
                Callback           = CallbackUrl,
                UserAgent          = "Hammock",
                ConsumerSecret     = ConsumerSecret,
                TokenSecret        = TokenSecret
            };

            return(info);
        }