Exemplo n.º 1
0
        private static string GenerateSignatureBase(ITokensBase tokens, string endpoint, Query query, string httpMethod, string timeStamp, string nonce, bool cloneQuery = true)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            var parameters = cloneQuery
                ? new Query(query)
                : (query ?? new Query());

            parameters[OAuthKeys.Version]         = OAuthVersion;
            parameters[OAuthKeys.Nonce]           = nonce;
            parameters[OAuthKeys.Timestamp]       = timeStamp;
            parameters[OAuthKeys.SignatureMethod] = HMACSHA1SignatureType;
            parameters[OAuthKeys.ConsumerKey]     = tokens.ConsumerKey;

            if (!string.IsNullOrEmpty(tokens.ApiToken))
            {
                parameters[OAuthKeys.Token] = tokens.ApiToken;
            }

            var normalizedRequestParameters = string.Join("&", parameters.GetRequestParameters(true));

            var signatureBase = new[]
            {
                httpMethod.ToUpper(),
                UrlEncode(endpoint),
                UrlEncode(normalizedRequestParameters)
            };

            return(string.Join("&", signatureBase));
        }
Exemplo n.º 2
0
        public static string HashHMACSHA1(ITokensBase token, string signatureBase)
        {
            var hashAlgo = new HMACSHA1();

            hashAlgo.Key = Encoding.UTF8.GetBytes($"{ token.ConsumerSecret }&{ token.ApiTokenSecret }");

            return(GenerateSignatureUsingHash(signatureBase, hashAlgo));
        }
Exemplo n.º 3
0
        public static string GenerateAuthenticationHeader(string endpoint, ITokensBase tokens, Query query, string httpMethod, string timeStamp, string nonce)
        {
            query = new Query(query);
            var signatureBase = GenerateSignatureBase(tokens, endpoint, query, httpMethod, timeStamp, nonce, false);

            query[OAuthKeys.Signature] = HashHMACSHA1(tokens, signatureBase);

            return($"OAuth { string.Join(",", query.GetOrdered().Select(kvp => Query.GetParameterPairDq(kvp.Key, kvp.Value))) }");
        }
Exemplo n.º 4
0
        public static HttpWebRequest CreateOAuthWebRequest(string endpoint, ITokensBase tokens, Query query, string method, bool autoSetting = true)
        {
            query  = query ?? new Query();
            method = method?.ToUpper() ?? "GET";

            var timeStamp = OAuthHelper.GenerateTimeStamp();
            var nonce     = OAuthHelper.GenerateNonce();

            endpoint = endpoint.Split(new[] { '?', '&', '#' }, 2)[0];

            var oauthHeader = OAuthHelper.GenerateAuthenticationHeader(endpoint, tokens, query, method, timeStamp, nonce);

            if (autoSetting && method == "GET")
            {
                endpoint = $"{ endpoint.Split(new[] { '?', '&' }, 2)[0] }?{ string.Join("&", query.GetRequestParameters()) }";
            }

            var webReq = WebRequest.CreateHttp(endpoint);

            webReq.Method = method;
            webReq.Headers.Add(HttpRequestHeader.Authorization, oauthHeader);

            if (autoSetting)
            {
                if (method == "POST")
                {
                    webReq.ContentType = "application/x-www-form-urlencoded";

                    if (query != null && query.Count > 0)
                    {
                        using (var str = webReq.GetRequestStream())
                        {
                            var queryString = string.Join("&", query.GetRequestParameters());
                            var data        = Encoding.UTF8.GetBytes(queryString);
                            str.Write(data, 0, data.Length);

                            data = null;
                        }
                    }
                }
            }


            return(webReq);
        }
Exemplo n.º 5
0
        public static Task <T> OAuthGet <T>(string endpoint, ITokensBase tokens, Query query) where T : class
        {
            var webReq = CreateOAuthWebRequest(endpoint, tokens, query, "get");

            return(SendRequest <T>(webReq));
        }
Exemplo n.º 6
0
 public static string GenerateSignature(string endpoint, ITokensBase tokens, Query query, string httpMethod, string timeStamp, string nonce)
 {
     return(GenerateSignatureBase(tokens, endpoint, query, httpMethod, timeStamp, nonce));
 }