Exemple #1
0
        /// <summary>
        /// Creates a request elements concatentation value to send with a request.
        /// This is also known as the signature base.
        /// </summary>
        /// <seealso href="http://oauth.net/core/1.0#rfc.section.9.1.3"/>
        /// <seealso href="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     = string.Concat(method.ToUpper(), "&");
            var requestUrl        = string.Concat(UrlEncodeRelaxed(ConstructRequestUrl(new Uri(url))), "&");
            var requestParameters = UrlEncodeRelaxed(NormalizeRequestParameters(parameters));

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

            return(sb.ToString());
        }
Exemple #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 => EqualsIgnoreCase(n.Name, "oauth_signature"));

            copy.RemoveAll(exclusions);

            foreach (var parameter in copy)
            {
                parameter.Name  = UrlEncodeStrict(parameter.Name);
                parameter.Value = UrlEncodeStrict(parameter.Value);
            }

            copy.Sort((x, y) => x.Name.Equals(y.Name) ? x.Value.CompareTo(y.Value) : x.Name.CompareTo(y.Name));
            return(copy);
        }
        private void AddXAuthParameters(ICollection <WebParameter> parameters, string timestamp, string nonce)
        {
            var authParameters = new WebParameterCollection
            {
                new WebParameter("x_auth_username", ClientUsername),
                new WebParameter("x_auth_password", ClientPassword),
                new WebParameter("x_auth_mode", "client_auth"),
                new WebParameter("oauth_consumer_key", ConsumerKey),
                new WebParameter("oauth_signature_method", ToRequestValue(SignatureMethod)),
                new WebParameter("oauth_timestamp", timestamp),
                new WebParameter("oauth_nonce", nonce),
                new WebParameter("oauth_version", Version ?? "1.0")
            };

            foreach (var authParameter in authParameters)
            {
                parameters.Add(authParameter);
            }
        }
        private static string WriteAuthorizationQuery(WebParameterCollection parameters)
        {
            var sb = new StringBuilder();

            parameters.Sort((l, r) => l.Name.CompareTo(r.Name));

            var count = 0;

            foreach (var parameter in parameters.Where(parameter =>
                                                       !IsNullOrBlank(parameter.Name) &&
                                                       !IsNullOrBlank(parameter.Value) &&
                                                       (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))))
            {
                count++;
                var format = count < parameters.Count ? "{0}={1}&" : "{0}={1}";
                sb.AppendFormat(format, parameter.Name, parameter.Value);
            }

            var authorization = sb.ToString();

            return(authorization);
        }
        public string GetAuthorizationHeader(WebParameterCollection parameters)
        {
            switch (Type)
            {
            case OAuthRequestType.RequestToken:
                ValidateRequestState();
                return(GetSignatureAuthorizationHeader(parameters));

            case OAuthRequestType.AccessToken:
                ValidateAccessRequestState();
                return(GetSignatureAuthorizationHeader(parameters));

            case OAuthRequestType.ProtectedResource:
                ValidateProtectedResourceState();
                return(GetSignatureAuthorizationHeader(parameters));

            case OAuthRequestType.ClientAuthentication:
                ValidateClientAuthAccessRequestState();
                return(GetClientSignatureAuthorizationHeader(parameters));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private void AddAuthParameters(ICollection <WebParameter> parameters, string timestamp, string nonce)
        {
            var authParameters = new WebParameterCollection
            {
                new WebParameter("oauth_consumer_key", ConsumerKey),
                new WebParameter("oauth_nonce", nonce),
                new WebParameter("oauth_signature_method", ToRequestValue(SignatureMethod)),
                new WebParameter("oauth_timestamp", timestamp),
                new WebParameter("oauth_version", Version ?? "1.0")
            };

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

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

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

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

            foreach (var authParameter in authParameters)
            {
                parameters.Add(authParameter);
            }
        }
        public string GetAuthorizationHeader()
        {
            var collection = new WebParameterCollection(Parameters);

            return(GetAuthorizationHeader(collection));
        }
        public string GetAuthorizationQuery()
        {
            var collection = new WebParameterCollection(0);

            return(GetAuthorizationQuery(collection));
        }
        public string GetAuthorizationQuery(IDictionary <string, string> parameters)
        {
            var collection = new WebParameterCollection(parameters);

            return(GetAuthorizationQuery(collection));
        }
        public string GetAuthorizationQuery(NameValueCollection parameters)
        {
            var collection = new WebParameterCollection(parameters);

            return(GetAuthorizationQuery(collection));
        }
 public virtual void AddRange(WebParameterCollection collection)
 {
     AddCollection(collection);
 }