Exemplo n.º 1
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"));

#pragma warning disable IDE0058 // Expression value is never used
            copy.RemoveAll(exclusions);
#pragma warning restore IDE0058 // Expression value is never used

            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);
        }
Exemplo n.º 2
0
        private string WriteAuthorizationHeader(WebParameterCollection parameters)
        {
            var sb = new StringBuilder("OAuth ");

            if (!string.IsNullOrWhiteSpace(this.Realm))
            {
#pragma warning disable IDE0058 // Expression value is never used
                sb.AppendFormat("realm=\"{0}\",", OAuthTools.UrlEncodeRelaxed(this.Realm));
#pragma warning restore IDE0058 // Expression value is never used
            }

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

            if (this.Type == OAuthRequestType.ProtectedResource)
            {
                foreach (var parameter in parameters.Where(parameter =>
                                                           !string.IsNullOrWhiteSpace(parameter.Name) &&
                                                           !string.IsNullOrWhiteSpace(parameter.Value) &&
                                                           (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_")) || parameter.Name == "oauth_token" && parameter.Value != null))
                {
#pragma warning disable IDE0058 // Expression value is never used
                    sb.AppendFormat("{0}=\"{1}\",", parameter.Name, parameter.Value);
#pragma warning restore IDE0058 // Expression value is never used
                }
            }
            else
            {
                foreach (var parameter in parameters.Where(parameter =>
                                                           !string.IsNullOrWhiteSpace(parameter.Name) &&
                                                           !string.IsNullOrWhiteSpace(parameter.Value) &&
                                                           (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))))
                {
#pragma warning disable IDE0058 // Expression value is never used
                    sb.AppendFormat("{0}=\"{1}\",", parameter.Name, parameter.Value);
#pragma warning restore IDE0058 // Expression value is never used
                }
            }

#pragma warning disable IDE0058 // Expression value is never used
            sb.Remove(sb.Length - 1, 1);
#pragma warning restore IDE0058 // Expression value is never used

            var authorization = sb.ToString();
            return(authorization);
        }
Exemplo n.º 3
0
        private void AddXAuthParameters(ICollection <WebParameter> parameters, string?timestamp, string?nonce)
        {
            if (string.IsNullOrWhiteSpace(this.ClientUsername))
            {
                throw new NullReferenceException($"{nameof(this.ClientUsername)} must not be null, empty or whitespace");
            }

            if (string.IsNullOrWhiteSpace(this.ClientPassword))
            {
                throw new NullReferenceException($"{nameof(this.ClientPassword)} must not be null, empty or whitespace");
            }

            if (string.IsNullOrWhiteSpace(this.ConsumerKey))
            {
                throw new NullReferenceException($"{nameof(this.ConsumerKey)} must not be null, empty or whitespace");
            }

            if (string.IsNullOrWhiteSpace(timestamp))
            {
                throw new NullReferenceException($"parameter {nameof(timestamp)} must not be null, empty or whitespace");
            }

            if (string.IsNullOrWhiteSpace(nonce))
            {
                throw new NullReferenceException($"parameter {nameof(nonce)} must not be null, empty or whitespace");
            }

            var authParameters = new WebParameterCollection
            {
                new WebParameter("x_auth_username", this.ClientUsername),
                new WebParameter("x_auth_password", this.ClientPassword),
                new WebParameter("x_auth_mode", "client_auth"),
                new WebParameter("oauth_consumer_key", this.ConsumerKey),
                new WebParameter("oauth_signature_method", ToRequestValue(this.SignatureMethod)),
                new WebParameter("oauth_timestamp", timestamp),
                new WebParameter("oauth_nonce", nonce),
                new WebParameter("oauth_version", this.Version ?? "1.0")
            };

            foreach (var authParameter in authParameters)
            {
                parameters.Add(authParameter);
            }
        }
Exemplo n.º 4
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 uri           = new Uri(url);
            var requestUrl    = string.Concat(UrlEncodeRelaxed(ConstructRequestUrl(uri)), "&");

            parameters.AddRange(WebUtils.ParseQueryString(uri));
            var requestParameters = UrlEncodeRelaxed(NormalizeRequestParameters(parameters));

#pragma warning disable IDE0058 // Expression value is never used
            sb.Append(requestMethod);
            sb.Append(requestUrl);
            sb.Append(requestParameters);
#pragma warning restore IDE0058 // Expression value is never used

            return(sb.ToString());
        }
Exemplo n.º 5
0
        private string GetAuthorizationQuery(WebParameterCollection parameters)
        {
            switch (this.Type)
            {
            case OAuthRequestType.RequestToken:
                ValidateRequestState();
                return(GetSignatureAuthorizationQuery(parameters));

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

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

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

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 6
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 =>
                                                       !string.IsNullOrWhiteSpace(parameter.Name) &&
                                                       !string.IsNullOrWhiteSpace(parameter.Value) &&
                                                       (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))))
            {
                count++;
                var format = count < parameters.Count ? "{0}={1}&" : "{0}={1}";

#pragma warning disable IDE0058 // Expression value is never used
                sb.AppendFormat(format, parameter.Name, parameter.Value);
#pragma warning restore IDE0058 // Expression value is never used
            }

            var authorization = sb.ToString();
            return(authorization);
        }
Exemplo n.º 7
0
        public string GetAuthorizationQuery()
        {
            var collection = new WebParameterCollection(0);

            return(GetAuthorizationQuery(collection));
        }
Exemplo n.º 8
0
        public string GetAuthorizationQuery(IDictionary <string, string> parameters)
        {
            var collection = new WebParameterCollection(parameters);

            return(GetAuthorizationQuery(collection));
        }
Exemplo n.º 9
0
        public string GetAuthorizationQuery(NameValueCollection parameters)
        {
            var collection = new WebParameterCollection(parameters);

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