public RateLimitPolicy(string requestKey, string routeTemplate, string httpMethod,
                               IList <AllowedConsumptionRate> allowedCallRates, bool allowAttributeOverride = false, string name = "")
        {
            if (string.IsNullOrWhiteSpace(requestKey))
            {
                throw new ArgumentNullException(nameof(requestKey),
                                                "requestKey cannot be null or whitespace");
            }

            if (requestKey.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(requestKey),
                                                      "requestKey cannot be empty");
            }

            if (string.IsNullOrWhiteSpace(routeTemplate) || routeTemplate.Length == 0)
            {
                routeTemplate = AllRequestPaths;
            }

            if (string.IsNullOrWhiteSpace(httpMethod))
            {
                httpMethod = AllHttpMethods;
            }

            RequestKey             = requestKey;
            RouteTemplate          = routeTemplate;
            HttpMethod             = httpMethod;
            AllowedCallRates       = allowedCallRates;
            AllowAttributeOverride = allowAttributeOverride;
            Name = name;
            Key  = new RateLimitingPolicyKey(RequestKey, routeTemplate, httpMethod);
        }
Esempio n. 2
0
        /// <summary>
        /// Policies the exists for request.
        /// </summary>
        /// <param name="requestKey">The request key (for example the client_id from ClaimsPrincipal.</param>
        /// <param name="requestPath">The request path.</param>
        /// <param name="httpMethod">The httpMethod like GET.</param>
        /// <returns></returns>
        public bool PolicyExists(string requestKey, string requestPath, string httpMethod)
        {
            var key = new RateLimitingPolicyKey(requestKey, requestPath, httpMethod);

            return(_entries.ContainsKey(key) || _entries.ContainsKey(AllRequestsKey));
        }
Esempio n. 3
0
        public async Task <RateLimitPolicy> GetPolicyAsync(RateLimitingRequest rateLimitingRequest)
        {
            if (IsWhiteListedPath(rateLimitingRequest.RouteTemplate) ||
                IsWhiteListedPath(rateLimitingRequest.Path))
            {
                return(null);
            }

            var providedPolicyEntry = await _policyProvider.GetPolicyAsync(rateLimitingRequest).ConfigureAwait(false);

            if (providedPolicyEntry == null ||
                IsWhiteListedRequestKey(providedPolicyEntry.RequestKey))
            {
                return(null);
            }

            if (providedPolicyEntry.AllowedCallRates != null && providedPolicyEntry.AllowedCallRates.Any())
            {
                return(providedPolicyEntry);
            }

            var policyKey = new RateLimitingPolicyKey(providedPolicyEntry.RequestKey,
                                                      rateLimitingRequest.RouteTemplate, rateLimitingRequest.Method);

            if (!_entries.ContainsKey(policyKey))
            {
                policyKey = new RateLimitingPolicyKey(providedPolicyEntry.RequestKey,
                                                      rateLimitingRequest.RouteTemplate, AllHttpMethods);
            }

            if (!_entries.ContainsKey(policyKey))
            {
                policyKey = new RateLimitingPolicyKey(providedPolicyEntry.RequestKey,
                                                      AllRequestPaths, rateLimitingRequest.Method);
            }

            if (!_entries.ContainsKey(policyKey))
            {
                policyKey = new RateLimitingPolicyKey(providedPolicyEntry.RequestKey,
                                                      AllRequestPaths, AllHttpMethods);
            }

            if (!_entries.ContainsKey(policyKey))
            {
                policyKey = new RateLimitingPolicyKey(AllRequestKeys,
                                                      rateLimitingRequest.RouteTemplate, rateLimitingRequest.Method);
            }

            if (!_entries.ContainsKey(policyKey))
            {
                policyKey = new RateLimitingPolicyKey(AllRequestKeys,
                                                      rateLimitingRequest.RouteTemplate, AllHttpMethods);
            }

            if (!_entries.ContainsKey(policyKey))
            {
                policyKey = AllRequestsByHttpMethodKeyMapping[rateLimitingRequest.Method.ToUpperInvariant()];
            }

            if (!_entries.ContainsKey(policyKey))
            {
                policyKey = AllRequestsKey;
            }

            return(_entries.ContainsKey(policyKey) ?
                   new RateLimitPolicy(providedPolicyEntry.RequestKey,
                                       _entries[policyKey].RouteTemplate, _entries[policyKey].HttpMethod,
                                       _entries[policyKey].AllowedCallRates, _entries[policyKey].AllowAttributeOverride,
                                       _entries[policyKey].Name) : null);
        }