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>
        /// 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)
        {
            StringBuilder stringBuilder = new StringBuilder();
            string        str1          = method.ToUpper() + "&";
            string        str2          = OAuthTools.UrlEncodeRelaxed(OAuthTools.ConstructRequestUrl(new Uri(url))) + "&";
            string        str3          = OAuthTools.UrlEncodeRelaxed(OAuthTools.NormalizeRequestParameters(parameters));

            stringBuilder.Append(str1);
            stringBuilder.Append(str2);
            stringBuilder.Append(str3);
            return(stringBuilder.ToString());
        }
Exemple #3
0
        private static string WriteAuthorizationQuery(WebParameterCollection parameters)
        {
            StringBuilder stringBuilder = new StringBuilder();

            parameters.Sort((WebParameter l, WebParameter r) => l.Name.CompareTo(r.Name));
            int num = 0;

            foreach (WebParameter webParameter in parameters.Where((WebParameter parameter) => !OAuthRequest.IsNullOrBlank(parameter.Name) && !OAuthRequest.IsNullOrBlank(parameter.Value) && (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))))
            {
                num++;
                string format = (num >= parameters.Count) ? "{0}={1}" : "{0}={1}&";
                stringBuilder.AppendFormat(format, webParameter.Name, webParameter.Value);
            }
            return(stringBuilder.ToString());
        }
        /// <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.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 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 (Type == OAuthRequestType.ProtectedResource)
            {
                if (Token != null)
                {
                    authParameters.Add(new WebParameter("oauth_token", Token));
                }
            }
            else
            {
                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);
            }
        }
Exemple #6
0
        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);
            }
        }
Exemple #7
0
        private string WriteAuthorizationHeader(WebParameterCollection parameters)
        {
            StringBuilder stringBuilder = new StringBuilder("OAuth ");

            if (!OAuthRequest.IsNullOrBlank(this.Realm))
            {
                stringBuilder.AppendFormat("realm=\"{0}\",", OAuthTools.UrlEncodeRelaxed(this.Realm));
            }
            parameters.Sort((WebParameter l, WebParameter r) => l.Name.CompareTo(r.Name));
            int num = 0;

            foreach (WebParameter webParameter in parameters.Where((WebParameter parameter) => !OAuthRequest.IsNullOrBlank(parameter.Name) && !OAuthRequest.IsNullOrBlank(parameter.Value) && (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))))
            {
                num++;
                string format = (num >= parameters.Count) ? "{0}=\"{1}\"" : "{0}=\"{1}\",";
                stringBuilder.AppendFormat(format, webParameter.Name, webParameter.Value);
            }
            return(stringBuilder.ToString());
        }
Exemple #8
0
        private void AddXAuthParameters(ICollection <WebParameter> parameters, string timestamp, string nonce)
        {
            WebParameterCollection webParameterCollection = new WebParameterCollection
            {
                new WebParameter("x_auth_username", this.ClientUsername),
                new WebParameter("x_auth_password", this.ClientPassword),
                new WebParameter("x_uuid", "TEST"),
                new WebParameter("x_auth_mode", "client_auth"),
                new WebParameter("oauth_consumer_key", this.ConsumerKey),
                new WebParameter("oauth_signature_method", OAuthRequest.ToRequestValue(this.SignatureMethod)),
                new WebParameter("oauth_timestamp", timestamp),
                new WebParameter("oauth_nonce", nonce),
                new WebParameter("oauth_version", this.Version ?? "1.0")
            };

            foreach (WebParameter item in webParameterCollection)
            {
                parameters.Add(item);
            }
        }
Exemple #9
0
        /// <summary>
        /// Sorts a <see cref="T:OAuth.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)
        {
            WebParameterCollection     source      = new WebParameterCollection((IEnumerable <WebParameter>)parameters);
            IEnumerable <WebParameter> parameters1 = source.Where <WebParameter>((Func <WebParameter, bool>)(n => OAuthTools.EqualsIgnoreCase(n.Name, "oauth_signature")));

            source.RemoveAll(parameters1);
            foreach (WebParameter webParameter in source)
            {
                webParameter.Value = OAuthTools.UrlEncodeStrict(webParameter.Value);
            }
            source.Sort((Comparison <WebParameter>)((x, y) =>
            {
                if (!x.Name.Equals(y.Name))
                {
                    return(x.Name.CompareTo(y.Name));
                }
                return(x.Value.CompareTo(y.Value));
            }));
            return(source);
        }
        private static string WriteAuthorizationQuery(WebParameterCollection parameters)
        {
            StringBuilder stringBuilder = new StringBuilder();

            parameters.Sort((Comparison <WebParameter>)((l, r) => l.Name.CompareTo(r.Name)));
            int num = 0;

            foreach (WebParameter webParameter in parameters.Where <WebParameter>((Func <WebParameter, bool>)(parameter =>
            {
                if (!OAuthRequest.IsNullOrBlank(parameter.Name) && !OAuthRequest.IsNullOrBlank(parameter.Value))
                {
                    return(parameter.Name.StartsWith("oauth_"));
                }
                return(false);
            })))
            {
                ++num;
                string format = num < parameters.Count ? "{0}={1}&" : "{0}={1}";
                stringBuilder.AppendFormat(format, (object)webParameter.Name, (object)webParameter.Value);
            }
            return(stringBuilder.ToString());
        }
Exemple #11
0
        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);
        }
        private string WriteAuthorizationHeader(WebParameterCollection parameters)
        {
            var sb = new StringBuilder("OAuth ");

            if (!IsNullOrBlank(Realm))
            {
                sb.AppendFormat("realm=\"{0}\",", OAuthTools.UrlEncodeRelaxed(Realm));
            }

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

            if (Type == OAuthRequestType.ProtectedResource)
            {
                foreach (var parameter in parameters.Where(parameter =>
                                                           !IsNullOrBlank(parameter.Name) &&
                                                           !IsNullOrBlank(parameter.Value) &&
                                                           (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_")) || parameter.Name == "oauth_token" && parameter.Value != null))
                {
                    sb.AppendFormat("{0}=\"{1}\",", parameter.Name, parameter.Value);
                }
            }
            else
            {
                foreach (var parameter in parameters.Where(parameter =>
                                                           !IsNullOrBlank(parameter.Name) &&
                                                           !IsNullOrBlank(parameter.Value) &&
                                                           (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))))
                {
                    sb.AppendFormat("{0}=\"{1}\",", parameter.Name, parameter.Value);
                }
            }

            sb.Remove(sb.Length - 1, 1);

            var authorization = sb.ToString();

            return(authorization);
        }
Exemple #13
0
        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();
            }
        }
Exemple #14
0
 public virtual void AddRange(WebParameterCollection collection)
 {
     this.AddCollection((IEnumerable <WebParameter>)collection);
 }
Exemple #15
0
        public string GetAuthorizationHeader()
        {
            var collection = new WebParameterCollection(0);

            return(GetAuthorizationHeader(collection));
        }
Exemple #16
0
        public string GetAuthorizationHeader(IDictionary <string, string> parameters)
        {
            var collection = new WebParameterCollection(parameters);

            return(GetAuthorizationHeader(collection));
        }
Exemple #17
0
        public string GetAuthorizationHeader(NameValueCollection parameters)
        {
            WebParameterCollection parameters2 = new WebParameterCollection(parameters);

            return(this.GetAuthorizationHeader(parameters2));
        }
 public virtual void AddRange(WebParameterCollection collection)
 {
     AddCollection(collection);
 }
Exemple #19
0
        public string GetAuthorizationHeader(IDictionary <string, string> parameters)
        {
            WebParameterCollection parameters2 = new WebParameterCollection(parameters);

            return(this.GetAuthorizationHeader(parameters2));
        }
Exemple #20
0
        public string GetAuthorizationHeader()
        {
            WebParameterCollection parameters = new WebParameterCollection(0);

            return(this.GetAuthorizationHeader(parameters));
        }
Exemple #21
0
        public string GetAuthorizationHeader(NameValueCollection parameters)
        {
            var collection = new WebParameterCollection(parameters);

            return(GetAuthorizationHeader(collection));
        }
        public static string NormalizeRequestParameters(WebParameterCollection parameters)
        {
            WebParameterCollection collection = OAuthTools.SortParametersExcludingSignature(parameters);

            return(OAuthTools.Concatenate(collection, "=", "&"));
        }