Exemple #1
0
        public static bool Validate(string method, string url, string posted, string authorizationHeader, double numSecondsValid, Func <string, string> GetConsumerSecret, bool throwOnError = false, Func <string, string, string> GetTokenSecret = null)
        {
            method = method ?? "GET";

            if (numSecondsValid < 0 || numSecondsValid > MaxNonceAge.TotalSeconds)
            {
                throw new ArgumentException(string.Format("Must be more than 0 and less than {0} seconds", MaxNonceAge.TotalSeconds), "numSecondsValid");
            }

            var query = new Utilities.Query(url, posted);

            if (!authorizationHeader.IsNullOrEmpty())
            {
                var authorization = ParseAuthorizationHeader(authorizationHeader);
                authorization.Keys.ForEach(key => query[key] = authorization[key]);
            }

            if (query["oauth_version"] != "1.0")
            {
                if (throwOnError)
                {
                    throw new System.Web.HttpException(401, "Invalid version specified");
                }
            }

            if (numSecondsValid > 0)
            {
                double timestamp = query["oauth_timestamp"].ToDouble();
                double diff      = Math.Abs(DateTime.UtcNow.GetSecondsSince1970() - timestamp);

                if (diff > numSecondsValid)
                {
                    if (throwOnError)
                    {
                        throw new System.Web.HttpException(401, "The timestamp is too old");
                    }
                    return(false);
                }

                DateTime used = _NonceCache[query["oauth_nonce"]];
                if (used.AddSeconds(numSecondsValid) > DateTime.UtcNow)
                {
                    if (throwOnError)
                    {
                        throw new System.Web.HttpException(401, "The nonce is not unique");
                    }
                    return(false);
                }
                _NonceCache[query["oauth_nonce"]] = DateTime.UtcNow;
            }

            string hashAlgorithm = query["oauth_signature_method"];
            int    q             = url.IndexOf('?');
            string path          = q == -1 ? url : url.Substring(0, q);

            string secret = GetConsumerSecret(query["oauth_consumer_key"].NotEmpty(query["client_id"]));
            string sig;

            try {
                var querystring = GetQueryString(query, true);
                sig = GetSignature(method, hashAlgorithm, secret, path, querystring, GetTokenSecret != null && query.ContainsKey("oauth_token") ? GetTokenSecret(query["oauth_token"], query["oauth_verifier"]) : null);
            } catch (Exception) {
                if (throwOnError)
                {
                    throw;
                }
                return(false);
            }

            var testSig = query["oauth_signature"];

            if (sig != testSig)
            {
                if (throwOnError)
                {
                    throw new System.Web.HttpException(401, string.Format("The signature is invalid. {0}", GetQueryString(query, false)));
                }
                return(false);
            }

            return(true);
        }