Exemplo n.º 1
0
        protected virtual string AppendParameters(string url, bool escape, bool skipOAuth)
        {
            var parameters = 0;

            foreach (var parameter in Parameters.Where(
                         parameter => !(parameter is HttpPostParameter) || Method == WebMethod.Post
                         ))
            {
                if (skipOAuth && parameter.Name.StartsWith("oauth_"))
                {
                    continue;
                }

                var value = escape
                                ? OAuthTools.UrlEncodeStrict(parameter.Value)
                                : parameter.Value;

                // GET parameters in URL
                url = url.Then(parameters > 0 || url.Contains("?") ? "&" : "?");
                url = url.Then("{0}={1}".FormatWith(parameter.Name, value));
                parameters++;
            }

            return(url);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates a <see cref="OAuthWebQueryInfo"/> instance to pass to an
        /// <see cref="OAuthWebQuery" /> 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(WebMethod 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,
                UserAgent      = "Hammock",
                TokenSecret    = TokenSecret,
                ConsumerSecret = ConsumerSecret
            };

            return(info);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates a <see cref="OAuthWebQueryInfo"/> instance to pass to an
        /// <see cref="OAuthWebQuery" /> 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(WebMethod 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,
                Callback       = OAuthTools.UrlEncodeRelaxed(CallbackUrl ?? ""),
                UserAgent      = "Hammock",
                TokenSecret    = TokenSecret,
                ConsumerSecret = ConsumerSecret
            };

            return(info);
        }
Exemplo n.º 4
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);
        }