private string GetAuthorizationHeader(WebParameterCollection parameters)
        {
            var sb = new StringBuilder();

            if (!string.IsNullOrEmpty(Realm))
            {
                sb.Append(string.Format("realm=\"{0}\",", OAuthTools.UrlEncodeRelaxed(Realm)));
            }

            parameters.Sort((l, r) => string.Compare(l.Name, r.Name, StringComparison.Ordinal));
            var parameterCount = 0;
            var oathParameters = parameters.Where(
                parameter => !string.IsNullOrEmpty(parameter.Name) &&
                !string.IsNullOrEmpty(parameter.Value) &&
                (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))).ToList();

            foreach (var parameter in oathParameters)
            {
                parameterCount++;
                var format = parameterCount < oathParameters.Count ? "{0}=\"{1}\"," : "{0}=\"{1}\"";
                sb.Append(string.Format(format, parameter.Name, parameter.Value));
            }

            var authorization = sb.ToString();

            return(string.Format("{0} {1}", AuthenticationMethod, authorization));
        }
Exemplo n.º 2
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>
        /// <a href="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 OAuthWebQueryInfo BuildClientAuthAccessTokenInfo(string method, WebParameterCollection parameters)
        {
            ValidateClientAuthAccessRequestState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            var uri       = new Uri(AccessTokenUrl);
            var timestamp = CreateTimestampFunc();
            var nonce     = OAuthTools.GetNonce(RandomNumberGenerator);

            AddXAuthParameters(parameters, timestamp, nonce);
            var signatureBase = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), parameters);
            var signature     = OAuthTools.GetSignature(SignatureProvider, SignatureTreatment, signatureBase, ConsumerSecret);
            var info          = new OAuthWebQueryInfo
            {
                WebMethod          = method,
                ParameterHandling  = ParameterHandling,
                ClientMode         = "client_auth",
                ClientUsername     = ClientUsername,
                ClientPassword     = ClientPassword,
                ConsumerKey        = ConsumerKey,
                SignatureMethod    = SignatureProvider.Id,
                SignatureTreatment = SignatureTreatment,
                Signature          = signature,
                Timestamp          = timestamp,
                Nonce          = nonce,
                Version        = Version ?? "1.0",
                TokenSecret    = TokenSecret,
                ConsumerSecret = ConsumerSecret
            };

            return(info);
        }
Exemplo n.º 3
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>
        /// <a href="http://oauth.net/core/1.0#anchor9"/>
        /// <returns></returns>
        public OAuthWebQueryInfo BuildRequestTokenInfo(string method, WebParameterCollection parameters)
        {
            ValidateTokenRequestState();
            if (parameters == null)
            {
                parameters = new WebParameterCollection();
            }
            var timestamp = CreateTimestampFunc();
            var nonce     = OAuthTools.GetNonce(RandomNumberGenerator);

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

            return(info);
        }
Exemplo n.º 4
0
        public 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 = uri.Query.ParseQueryString();

            foreach (var parameter in urlParameters.Keys)
            {
                parameters.Add(new WebParameter(parameter, urlParameters[parameter], WebParameterType.Query));
            }
            var timestamp = CreateTimestampFunc();
            var nonce     = OAuthTools.GetNonce(RandomNumberGenerator);

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

            return(info);
        }