Beispiel #1
0
        public string ToOAuthHeader()
        {
            var authHeader = new StringBuilder();

            authHeader.Append(string.Format("{0} ", OAuthConstants.AUTHORIZATION_OAUTH));

            if (!string.IsNullOrEmpty(RequestParameters[OAuthParameter.OAUTH_REALM]))
            {
                authHeader.Append(string.Format("{0}={1},", OAuthParameter.OAUTH_REALM, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_REALM])));
            }

            authHeader.Append(string.Format("{0}={1},", OAuthParameter.OAUTH_CONSUMER_KEY, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_CONSUMER_KEY])));

            if (!string.IsNullOrEmpty(RequestParameters[OAuthParameter.OAUTH_TOKEN]))
            {
                authHeader.Append(string.Format("{0}={1},", OAuthParameter.OAUTH_TOKEN, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_TOKEN])));
            }

            authHeader.Append(string.Format("{0}={1},", OAuthParameter.OAUTH_SIGNATURE_METHOD, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_SIGNATURE_METHOD])));
            authHeader.Append(string.Format("{0}={1},", OAuthParameter.OAUTH_SIGNATURE, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_SIGNATURE])));
            authHeader.Append(string.Format("{0}={1},", OAuthParameter.OAUTH_TIMESTAMP, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_TIMESTAMP])));
            authHeader.Append(string.Format("{0}={1}", OAuthParameter.OAUTH_NONCE, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_NONCE])));

            if (!string.IsNullOrEmpty(RequestParameters[OAuthParameter.OAUTH_VERSION]))
            {
                authHeader.Append(string.Format(",{0}={1}", OAuthParameter.OAUTH_VERSION, OAuthParameter.UrlEncode(RequestParameters[OAuthParameter.OAUTH_VERSION])));
            }

            return(authHeader.ToString());
        }
Beispiel #2
0
        private static string GetSortedString(NameValueCollection list)
        {
            var sb   = new StringBuilder();
            var keys = list.AllKeys;

            Array.Sort(keys);

            foreach (var key in keys)
            {
                var valuesArray = list.GetValues(key);
                if (valuesArray == null)
                {
                    continue;
                }

                Array.Sort(valuesArray);

                foreach (var myvalue in valuesArray)
                {
                    sb.Append(OAuthParameter.UrlEncode(key)).Append('=');
                    sb.Append(OAuthParameter.UrlEncode(myvalue));
                    sb.Append('&');
                }
            }

            if (sb.Length > 1)
            {
                sb.Remove(sb.Length - 1, 1);
            }

            return(sb.ToString());
        }
Beispiel #3
0
        public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken)
        {
            if (consumer == null)
            {
                throw new ArgumentNullException(ERROR_CONSUMER_NULL);
            }

            if (ioAuthToken == null)
            {
                throw new ArgumentNullException(ERROR_TOKEN_NULL);
            }

            var requestUri = string.Format("{0}://{1}{2}", webRequest.RequestUri.Scheme, webRequest.RequestUri.Authority, webRequest.RequestUri.AbsolutePath);
            var request    = WebRequest.Create(requestUri);

            request.Method = webRequest.Method;

            var           key             = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret));
            HashAlgorithm hashAlgorithm   = new HMACSHA1(Encoding.UTF8.GetBytes(key));
            var           canonicalString = this.GetCanonicalString(request, consumer, ioAuthToken);
            var           encoded         = Encoding.UTF8.GetBytes(canonicalString);
            var           result          = Convert.ToBase64String(hashAlgorithm.ComputeHash(encoded));

            return(result);
        }
Beispiel #4
0
        protected string GetCanonicalString(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken)
        {
            if (string.IsNullOrEmpty(webRequest.Method))
            {
                throw new ArgumentNullException(ERROR_HTTP_METHOD);
            }

            var stringToSign = new StringBuilder();

            stringToSign.Append(webRequest.Method.ToUpper().Trim());
            stringToSign.Append(OAuthParameter.QUERYSTRING_SEPERATOR);
            //TODO: check if querystring should be part of it
            stringToSign.Append(OAuthParameter.UrlEncode(webRequest.RequestUri.AbsoluteUri));
            stringToSign.Append(OAuthParameter.QUERYSTRING_SEPERATOR);

            if (RequestParameters[OAuthParameter.OAUTH_REALM] != null)
            {
                RequestParameters.Remove(OAuthParameter.OAUTH_REALM);
            }

            if (RequestParameters[OAuthParameter.OAUTH_SIGNATURE] != null)
            {
                RequestParameters.Remove(OAuthParameter.OAUTH_SIGNATURE);
            }

            //TODO: input correct parameters
            if (RequestParameters[OAuthParameter.OAUTH_SIGNATURE_METHOD] == null)
            {
                RequestParameters.Add(OAuthParameter.OAUTH_SIGNATURE_METHOD, GeneralUtil.SignatureMethodTypeToString(consumer.OAuthSignatureMethod));
            }

            if (RequestParameters[OAuthParameter.OAUTH_CONSUMER_KEY] == null)
            {
                RequestParameters.Add(OAuthParameter.OAUTH_CONSUMER_KEY, consumer.ConsumerKey);
            }

            if (RequestParameters[OAuthParameter.OAUTH_VERSION] == null)
            {
                RequestParameters.Add(OAuthParameter.OAUTH_VERSION, GeneralUtil.OAuthVersionTypeToString(consumer.OAuthVersion));
            }

            if (RequestParameters[OAuthParameter.OAUTH_TIMESTAMP] == null)
            {
                RequestParameters.Add(OAuthParameter.OAUTH_TIMESTAMP, GenerateTimeStamp());
            }
            if (RequestParameters[OAuthParameter.OAUTH_NONCE] == null)
            {
                RequestParameters.Add(OAuthParameter.OAUTH_NONCE, GenerateNonce());
            }
            if (RequestParameters[OAuthParameter.OAUTH_TOKEN] == null && !string.IsNullOrEmpty(ioAuthToken.TokenKey))
            {
                RequestParameters.Add(OAuthParameter.OAUTH_TOKEN, ioAuthToken.TokenKey);
            }

            stringToSign.Append(OAuthParameter.UrlEncode(GetNormalizedParameterString(RequestParameters).Trim()));

            return(stringToSign.ToString());
        }
        public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken)
        {
            if (consumer == null)
            {
                throw new ArgumentNullException(ERROR_CONSUMER_NULL);
            }

            if (ioAuthToken == null)
            {
                throw new ArgumentNullException(ERROR_TOKEN_NULL);
            }

            var result = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret));

            return(result);
        }
        public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken)
        {
            if (consumer == null)
            {
                throw new ArgumentNullException(ERROR_CONSUMER_NULL);
            }

            if (ioAuthToken == null)
            {
                throw new ArgumentNullException(ERROR_TOKEN_NULL);
            }

            var           key           = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret));
            HashAlgorithm hashAlgorithm = new HMACSHA1(Encoding.UTF8.GetBytes(key));
            var           encoded       = Encoding.UTF8.GetBytes(this.GetCanonicalString(webRequest, consumer, ioAuthToken));
            var           result        = Convert.ToBase64String(hashAlgorithm.ComputeHash(encoded));

            return(result);
        }
 public string ToQueryString()
 {
     return(string.Format(QUERYSTRING_FORMAT, OAUTH_TOKEN, OAuthParameter.UrlEncode(this.TokenKey), OAUTH_TOKEN_SECRET, OAuthParameter.UrlEncode(this.TokenSecret)));
 }