Пример #1
0
        public ValueProviderResult GetValue(string key)
        {
            var header = headers.FirstOrDefault(h => predicate(h, key));

            if (header.Key.IsNotNullOrEmpty())
            {
                key = header.Key; // Replace the passed in key with the header name
                var values = headers.GetValues(key);

                if (values.Count() > 1) // We got a list of values
                {
                    return(new ValueProviderResult(values, null, CultureInfo.CurrentCulture));
                }
                else
                {
                    // We could have received multiple values (comma separated) or just one value
                    var value = values.First();
                    values = value.Split(',').Select(x => x.Trim()).ToArray();
                    if (values.Count() > 1)
                    {
                        return(new ValueProviderResult(values, null, CultureInfo.CurrentCulture));
                    }
                    else
                    {
                        return(new ValueProviderResult(value, value, CultureInfo.CurrentCulture));
                    }
                }
            }
            return(null);
        }
Пример #2
0
        public static void ApplyRequestChainHeader(this HttpRequestHeaders headers, IRequestId requestId)
        {
            string headerValue = requestId.Value.ToString();

            if (requestId.Depth.HasValue)
            {
                var newDepth = requestId.Depth.Value + 1;
                headerValue = $"{requestId.Value}:{newDepth}";
            }

            var existingHeader = headers
                                 .FirstOrDefault(a => string.Equals(a.Key, requestId.RequestChainHeaderKey));

            if (!Equals(existingHeader, default(KeyValuePair <string, IEnumerable <string> >)))
            {
                if (existingHeader.Value.Any(a => string.Equals(a, headerValue, StringComparison.OrdinalIgnoreCase)))
                {
                    // Header is already in place... exit no need to proceed
                    return;
                }
                else
                {
                    var firstHeader = existingHeader.Value.FirstOrDefault();
                    var msg         = $"Attempted to set RequestChainHeader when it already exists and does not match (\"{firstHeader}\")";
                    throw new InvalidOperationException(msg);
                }
            }

            headers.Add(requestId.RequestChainHeaderKey, headerValue);
        }
        public ValueProviderResult GetValue(string key)
        {
            var header = headers.FirstOrDefault(h => predicate(h, key));

            if (!String.IsNullOrEmpty(header.Key))
            {
                key = header.Key;
                var values = headers.GetValues(key);
                if (values.Count() > 1)
                {
                    return(new ValueProviderResult(values, null, CultureInfo.CurrentCulture));
                }
                else
                {
                    string value = values.First();
                    values = value.Split(',').Select(x => x.Trim()).ToArray();
                    if (values.Count() > 1)
                    {
                        return(new ValueProviderResult(values, null, CultureInfo.CurrentCulture));
                    }
                    else
                    {
                        return(new ValueProviderResult(value, value, CultureInfo.CurrentCulture));
                    }
                }
            }
            return(null);
        }
Пример #4
0
        protected string GetHeader(HttpRequestHeaders headers, string key)
        {
            var header = headers.FirstOrDefault(a => a.Key.ToLower().Equals(key.ToLower()));

            if (headers == null || !headers.Any() || header.Key == null || string.IsNullOrEmpty(header.Value.FirstOrDefault()))
            {
                throw new LoginFaildException("请求的Header中必须要有AppId和Secrect信息!");
            }
            return(header.Value.FirstOrDefault());
        }
Пример #5
0
        protected bool ReadAuthenticationContent(HttpRequestHeaders authenticationContent, string key, out string value)
        {
            value = string.Empty;
            var content = authenticationContent.FirstOrDefault(pair => pair.Key == key);

            if (content.Value == null || content.Value.Count() != 1)
            {
                return(false);
            }
            value = content.Value.First();
            return(true);
        }
Пример #6
0
        /// <summary>
        /// Determines if an incoming webhook request is authentic.
        /// </summary>
        /// <param name="requestHeaders">The request's headers.</param>
        /// <param name="requestBody">The body of the request.</param>
        /// <param name="shopifySecretKey">Your app's secret key.</param>
        /// <returns>A boolean indicating whether the webhook is authentic or not.</returns>
        public static bool IsAuthenticWebhook(HttpRequestHeaders requestHeaders, string requestBody, string shopifySecretKey)
        {
            var hmacHeaderValue = requestHeaders.FirstOrDefault(kvp => kvp.Key.Equals("X-Shopify-Hmac-SHA256", StringComparison.OrdinalIgnoreCase)).Value.FirstOrDefault();

            if (string.IsNullOrEmpty(hmacHeaderValue))
            {
                return(false);
            }

            //Compute a hash from the apiKey and the request body
            string     hmacHeader = hmacHeaderValue;
            HMACSHA256 hmac       = new HMACSHA256(Encoding.UTF8.GetBytes(shopifySecretKey));
            string     hash       = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(requestBody)));

            //Webhook is valid if computed hash matches the header hash
            return(hash == hmacHeader);
        }
Пример #7
0
        internal static int ValidateToken(HttpRequestHeaders headers)
        {
            //if (!headers.Contains("Authorization") || headers.FirstOrDefault(h => h.Key == "Authorization").Value?.FirstOrDefault() == null)
            //{
            //    throw new SecurityException("Token is invalid");
            //}

            string headerToken = headers.FirstOrDefault(h => h.Key == "Authorization").Value?.FirstOrDefault();

            if (string.IsNullOrEmpty(headerToken))
            {
                throw new SecurityException("Token is invalid");
            }

            headerToken = headerToken.Replace("bearer ", "");

            var token    = new JwtSecurityToken(headerToken);
            var username = token.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;
            int id;

            if (string.IsNullOrEmpty(username))
            {
                throw new SecurityException("Token is invalid");
            }

            if (!int.TryParse(username, out id))
            {
                throw new SecurityException("Token is invalid");
            }

            if (DateTime.Now > token.ValidTo)
            {
                throw new SecurityException("Token is invalid");
            }

            return(id);
        }
Пример #8
0
        public string GetHeader(HttpRequestHeaders headers, string headerName)
        {
            var headerValue = headers.FirstOrDefault(x => x.Key == headerName).Value?.First();

            return(headerValue);
        }
Пример #9
0
 public static string GetAuthenticationInfoValue(this HttpRequestHeaders headers)
 {
     return(headers.FirstOrDefault(h => h.Key.Equals(SettingsManager.AuthenticationHeaderKey, StringComparison.OrdinalIgnoreCase)).Value?.FirstOrDefault());
 }
Пример #10
0
 private static string GetHeaderValue(HttpRequestHeaders headers, string header)
 {
     return(headers.FirstOrDefault(h => h.Key.ToLower() == header.ToLower()).Value?.FirstOrDefault()?.Split(',')[0]);
 }