Example #1
0
        public void ValidateParameters(CallParameters parameters, string consumerTimeStamp, string consumerNonce)
        {
            if (string.IsNullOrEmpty(parameters.ConsumerKey))
            {
                ThrowAuthenticationException("Consumer Key must be provided");
            }

            if (string.IsNullOrEmpty(parameters.ConsumerSecret))
            {
                ThrowAuthenticationException("Consumer Secret must be provided");
            }

            if (parameters.HttpMethod == null)
            {
                ThrowAuthenticationException("Http Method must be provided");
            }

            if (string.IsNullOrEmpty(parameters.Url))
            {
                ThrowAuthenticationException("Url must be provided");
            }

            if (string.IsNullOrEmpty(consumerTimeStamp))
            {
                ThrowAuthenticationException("Consumer timestamp must be provided");
            }

            if (string.IsNullOrEmpty(consumerNonce))
            {
                ThrowAuthenticationException("Consumer nonce must be provided");
            }
        }
        public string GetHashEncodedSignitureFor(CallParameters callParameters, string consumerTimeStamp, string nonce)
        {
            _oAuthValidator.ValidateParameters(callParameters, consumerTimeStamp, nonce);

            var oAuthSigniture = _oAuthSignitureBuilder.Build(callParameters, consumerTimeStamp, nonce);
            var hashAlgorithm = _hashAlgorithmBuilder.Build(callParameters.ConsumerSecret, callParameters.TokenSecret);

            _oAuthValidator.ValidateSignitureAndAlgorithm(oAuthSigniture, hashAlgorithm);

            return oAuthSigniture.ToBase64StringUsingHashAlgorithm(hashAlgorithm);
        }
        public OAuthSigniture Build(CallParameters callParameters, string consumerTimeStamp, string nonce)
        {
            var queryParameters = _queryParameterBuilder.Build(callParameters, consumerTimeStamp, nonce);
            var normalizedUrl = callParameters.Url.ToNormalizedUrl();
            var normalizedRequestParameters = queryParameters.ToNormalizedRequestParameters();

            var oAuthSignitureValue = new StringBuilder();
            oAuthSignitureValue.AppendFormat("{0}&", callParameters.HttpMethod.ToString().ToUpper());
            oAuthSignitureValue.AppendFormat("{0}&", normalizedUrl.UrlEncode());
            oAuthSignitureValue.AppendFormat("{0}", normalizedRequestParameters.UrlEncode());

            return new OAuthSigniture { Value = oAuthSignitureValue.ToString() };
        }
        public AuthorizationHeader Build(CallParameters parameters)
        {
            var currentUnixTime = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var consumerTimestamp = Convert.ToInt64(currentUnixTime.TotalSeconds).ToString(CultureInfo.InvariantCulture);
            var consumerNonce = new Random().Next(123400, 9999999).ToString(CultureInfo.InvariantCulture);

            var encodedOAuthSigniture = _oAuthSignitureProvider.GetHashEncodedSignitureFor(parameters, consumerTimestamp, consumerNonce).UrlEncode();

            return new AuthorizationHeader
            {
                Value = string.Format(
                    "OAuth realm=\"http://www.opentable.com/, oauth_consumer_key=\"{0}\", oauth_signature_method=\"HMAC-SHA1\", oauth_signature=\"{1}\", oauth_timestamp=\"{2}\", oauth_token=\"\", oauth_nonce=\"{3}\", oauth_version=\"1.0\"",
                    parameters.ConsumerKey, encodedOAuthSigniture, consumerTimestamp, consumerNonce)
            };
        }
        public IList<QueryParameter> Build(CallParameters callParameters, string consumerTimeStamp, string nonce)
        {
            var outcome = new List<QueryParameter>();
            var queryStringParameters = callParameters.Url.ToApiQuerystringParameters();

            if (!string.IsNullOrEmpty(queryStringParameters))
            {
                var p = queryStringParameters.Split('&');
                foreach (var s in p)
                {
                    if (!string.IsNullOrEmpty(s) && !s.StartsWith(OAuthConstants.OAuthParameterPrefix))
                    {
                        if (s.IndexOf('=') > -1)
                        {
                            var temp = s.Split('=');
                            outcome.Add(new QueryParameter(temp[0], temp[1]));
                        }
                        else
                        {
                            outcome.Add(new QueryParameter(s, string.Empty));
                        }
                    }
                }
            }

            outcome.Add(new QueryParameter(OAuthConstants.OAuthVersionKey, OAuthConstants.OAuthVersion));
            outcome.Add(new QueryParameter(OAuthConstants.OAuthNonceKey, nonce));
            outcome.Add(new QueryParameter(OAuthConstants.OAuthTimestampKey, consumerTimeStamp));
            outcome.Add(new QueryParameter(OAuthConstants.OAuthSignatureMethodKey, "HMAC-SHA1"));
            outcome.Add(new QueryParameter(OAuthConstants.OAuthConsumerKeyKey, callParameters.ConsumerKey));
            outcome.Add(new QueryParameter(OAuthConstants.OAuthTokenKey, callParameters.Token));

            outcome.Sort(new QueryParameterComparer());

            return outcome;
        }