예제 #1
0
        static string GenerateSignature(string consumerSecret, Uri uri, HttpMethod method, Token token, IEnumerable<KeyValuePair<string, string>> parameters)
        {
            if (ComputeHash == null)
            {
                throw new InvalidOperationException("ComputeHash is null, must initialize before call OAuthUtility.HashFunction = /* your computeHash code */ at once.");
            }

            var hmacKeyBase = consumerSecret.UrlEncode() + "&" + ((token == null) ? "" : token.Secret).UrlEncode();

            // escaped => unescaped[]
            var queryParams = Utility.ParseQueryString(uri.GetComponents(UriComponents.Query | UriComponents.KeepDelimiter, UriFormat.UriEscaped));

            var stringParameter = parameters
                .Where(x => x.Key.ToLower() != "realm")
                .Concat(queryParams)
                .Select(p => new { Key = p.Key.UrlEncode(), Value = p.Value.UrlEncode() })
                .OrderBy(p => p.Key, StringComparer.Ordinal)
                .ThenBy(p => p.Value, StringComparer.Ordinal)
                .Select(p => p.Key + "=" + p.Value)
                .ToString("&");
            var signatureBase = method.ToString() +
                "&" + uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped).UrlEncode() +
                "&" + stringParameter.UrlEncode();

            var hash = ComputeHash(Encoding.UTF8.GetBytes(hmacKeyBase), Encoding.UTF8.GetBytes(signatureBase));
            return Convert.ToBase64String(hash).UrlEncode();
        }
 public OAuthEchoMessageHandler(string serviceProvider, string realm, string consumerKey, string consumerSecret,
                                 Token token = null,
                                 IEnumerable<KeyValuePair<string, string>> optionalOAuthHeaderParameters = null)
     : this(new HttpClientHandler(), serviceProvider, realm, consumerKey, consumerSecret, token,
         optionalOAuthHeaderParameters)
 {
 }
 public OAuthEchoMessageHandler(HttpMessageHandler innerHandler, string serviceProvider, string realm,
                                 string consumerKey, string consumerSecret,
                                 Token token = null,
                                 IEnumerable<KeyValuePair<string, string>> optionalOAuthHeaderParameters = null)
     : base(new OAuthMessageHandler(new OAuthEchoMessagePostHandler(innerHandler, serviceProvider),
                                    consumerKey, consumerSecret, token, AddRealm(optionalOAuthHeaderParameters, realm)))
 {
 }
예제 #4
0
        public static IEnumerable<KeyValuePair<string, string>> BuildBasicParameters(string consumerKey, string consumerSecret, string url, HttpMethod method, Token token = null, IEnumerable<KeyValuePair<string, string>> optionalParameters = null)
        {
            Precondition.NotNull(url, "url");

            var parameters = new List<KeyValuePair<string, string>>(capacity: 7)
            {
                new KeyValuePair<string,string>("oauth_consumer_key", consumerKey),
                new KeyValuePair<string,string>("oauth_nonce", random.Next().ToString() ),
                new KeyValuePair<string,string>("oauth_timestamp", DateTime.UtcNow.ToUnixTime().ToString() ),
                new KeyValuePair<string,string>("oauth_signature_method", "HMAC-SHA1" ),
                new KeyValuePair<string,string>("oauth_version", "1.0" )
            };
            if (token != null) parameters.Add(new KeyValuePair<string, string>("oauth_token", token.Key));
            if (optionalParameters == null) optionalParameters = Enumerable.Empty<KeyValuePair<string, string>>();

            var signature = GenerateSignature(consumerSecret, new Uri(url), method, token, parameters.Concat(optionalParameters));

            parameters.Add(new KeyValuePair<string, string>("oauth_signature", signature));

            return parameters;
        }
예제 #5
0
 public OAuthMessageHandler(HttpMessageHandler innerHandler, string consumerKey, string consumerSecret, Token token = null, IEnumerable <KeyValuePair <string, string> > optionalOAuthHeaderParameters = null)
     : base(innerHandler)
 {
     this.consumerKey    = consumerKey;
     this.consumerSecret = consumerSecret;
     this.token          = token;
     this.parameters     = optionalOAuthHeaderParameters ?? Enumerable.Empty <KeyValuePair <string, string> >();
 }
예제 #6
0
 public OAuthMessageHandler(string consumerKey, string consumerSecret, Token token = null, IEnumerable <KeyValuePair <string, string> > optionalOAuthHeaderParameters = null)
     : this(new HttpClientHandler(), consumerKey, consumerSecret, token, optionalOAuthHeaderParameters)
 {
 }