-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eilyushenko
committed
Nov 25, 2024
1 parent
7b3bbe9
commit 9ea2723
Showing
22 changed files
with
245 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using RateLimiter.Api.Infrastructure.Attributes; | ||
using RateLimiter.Api.Infrastructure.Filters; | ||
using RateLimiter.Core.Domain.Enums; | ||
|
||
namespace RateLimiter.Api.Controllers | ||
{ | ||
[Authorize] | ||
[ApiController] | ||
[Route("api/target")] | ||
public class TargetController : ControllerBase | ||
public class TargetController | ||
{ | ||
[HttpGet] | ||
[Route("test-endpoint")] | ||
public Task<string> TestEndpoint() | ||
{ | ||
var a = User.Claims.ToList(); | ||
[Route("limited-info")] | ||
[AppliedRule(RuleType.RequestPerTimeSpan, RuleType.TimeSpanPassedSinceLastCall)] | ||
[ServiceFilter(typeof(RuleFilter))] | ||
public Task GetLimitedInfo() => Task.CompletedTask; | ||
|
||
return Task.FromResult("Ok"); | ||
} | ||
[HttpGet] | ||
[Route("full-info")] | ||
public Task GetFullInfo() => Task.CompletedTask; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
RateLimiter.Api/Infrastructure/Attributes/AppliedRuleAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using RateLimiter.Core.Domain.Enums; | ||
|
||
namespace RateLimiter.Api.Infrastructure.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Method)] | ||
public class AppliedRuleAttribute : Attribute | ||
{ | ||
public RuleType[] RuleTypes { get; } | ||
|
||
public AppliedRuleAttribute(params RuleType[] ruleTypes) | ||
{ | ||
RuleTypes = ruleTypes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using RateLimiter.Api.Infrastructure.Attributes; | ||
using RateLimiter.BusinessLogic.Services; | ||
using RateLimiter.Core.Domain.Enums; | ||
using RateLimiter.Core.Helpers; | ||
using System.Security.Claims; | ||
|
||
namespace RateLimiter.Api.Infrastructure.Filters | ||
{ | ||
public class RuleFilter : IAsyncActionFilter | ||
{ | ||
private readonly IRuleFactory _ruleFactory; | ||
|
||
public RuleFilter(IRuleFactory ruleFactory) | ||
{ | ||
_ruleFactory = ruleFactory; | ||
} | ||
|
||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
var appliedRuleAttribute = context.ActionDescriptor.EndpointMetadata | ||
.OfType<AppliedRuleAttribute>() | ||
.FirstOrDefault(); | ||
|
||
if (appliedRuleAttribute == null || !appliedRuleAttribute.RuleTypes.Any()) | ||
{ | ||
throw new Exception($"{nameof(AppliedRuleAttribute)} and {nameof(RuleFilter)} have been applied incorecctly."); | ||
} | ||
|
||
var ruleTypes = appliedRuleAttribute.RuleTypes.ToList(); | ||
var (regionType, uniqueId) = GetClaimValues(context.HttpContext.User?.Claims); | ||
|
||
// TO DO | ||
|
||
await next(); | ||
} | ||
|
||
private (RegionType regionType, string uniqueId) GetClaimValues(IEnumerable<Claim> claims) | ||
{ | ||
var region = claims.FirstOrDefault(c => c.Type == nameof(CustomClaimTypes.Region)).Value; | ||
var identifierId = claims.FirstOrDefault(c => c.Type == nameof(CustomClaimTypes.UniqueIdentifier)).Value; | ||
|
||
return ((RegionType)Enum.Parse(typeof(RegionType), region), identifierId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,46 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using RateLimiter.Api.Infrastructure.Filters; | ||
using RateLimiter.BusinessLogic.Services; | ||
using RateLimiter.BusinessLogic.Services.Implementation; | ||
using RateLimiter.Core.Helpers; | ||
using RateLimiter.BusinessLogic.Services.Implementation.RateLimiter.Factory; | ||
using RateLimiter.BusinessLogic.Services.Implementation.RateLimiter.Rules.EU; | ||
using RateLimiter.BusinessLogic.Services.Implementation.RateLimiter.Rules.USA; | ||
using RateLimiter.Core.Settings; | ||
|
||
namespace RateLimiter.Api.Infrastructure | ||
{ | ||
public static class IocConfig | ||
{ | ||
public static void AddRateLimiterServices(this IServiceCollection services) | ||
public static void AddFilters(this IServiceCollection services) | ||
{ | ||
AddBussinessLogicServices(services); | ||
AddDataAccessLayerRepositories(services); | ||
services.AddScoped<RuleFilter>(); | ||
} | ||
|
||
private static void AddBussinessLogicServices(this IServiceCollection services) | ||
{ } | ||
|
||
private static void AddDataAccessLayerRepositories(this IServiceCollection services) | ||
{ } | ||
|
||
public static void AddTokenConfigurationServices(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.Configure<TokenSettings>(configuration.GetSection(nameof(TokenSettings))); | ||
services.AddSingleton(provider => | ||
services.AddSingleton<IKeyGeneratorService>(provider => | ||
{ | ||
var secretKey = configuration.GetValue<string>("GeneratorSecretKey"); | ||
return new KeyGenerator(secretKey); | ||
return new KeyGeneratorService(secretKey); | ||
}); | ||
|
||
services.AddScoped<ITokenService, TokenService>(); | ||
} | ||
|
||
public static void AddRateLimiterServices(this IServiceCollection services) | ||
{ | ||
AddBussinessLogicServices(services); | ||
AddDataAccessLayerRepositories(services); | ||
} | ||
|
||
private static void AddBussinessLogicServices(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IRuleFactory, RuleFactory>(); | ||
services.AddScoped<IRuleService, LastCallTimeRule>(); | ||
services.AddScoped<IRuleService, RequestPerTimeRule>(); | ||
} | ||
|
||
private static void AddDataAccessLayerRepositories(this IServiceCollection services) | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
RateLimiter.BusinessLogic/Services/IKeyGeneratorService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace RateLimiter.BusinessLogic.Services | ||
{ | ||
public interface IKeyGeneratorService | ||
{ | ||
string SigningAlgorithm { get; } | ||
SecurityKey GetKey(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using RateLimiter.Core.Domain.Enums; | ||
|
||
namespace RateLimiter.BusinessLogic.Services | ||
{ | ||
public interface IRuleFactory | ||
{ | ||
IRuleService GetRule(RegionType regionType, RuleType ruleType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using RateLimiter.Core.Domain.Enums; | ||
|
||
namespace RateLimiter.BusinessLogic.Services | ||
{ | ||
public interface IRuleService | ||
{ | ||
public RegionType RegionType { get; } | ||
public RuleType RuleType { get; } | ||
Task ApplyRule(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
using RateLimiter.Core.Domain.Enums; | ||
using RateLimiter.Core.Helpers; | ||
using RateLimiter.Core.Settings; | ||
|
||
namespace RateLimiter.BusinessLogic.Services | ||
{ | ||
public interface ITokenService | ||
{ | ||
KeyGenerator KeyGenerator { get; } | ||
IKeyGeneratorService KeyGeneratorService { get; } | ||
TokenSettings TokenSettings { get; } | ||
Task<string> GenerateTokenAsync(RegionTokenType type); | ||
Task<string> GenerateTokenAsync(RegionType type); | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
RateLimiter.Core/Helpers/KeyGenerator.cs → ...ces/Implementation/KeyGeneratorService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
RateLimiter.BusinessLogic/Services/Implementation/RateLimiter/Factory/RuleFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using RateLimiter.Core.Domain.Enums; | ||
|
||
namespace RateLimiter.BusinessLogic.Services.Implementation.RateLimiter.Factory | ||
{ | ||
public class RuleFactory : IRuleFactory | ||
{ | ||
private readonly IEnumerable<IRuleService> _rules; | ||
|
||
public RuleFactory(IEnumerable<IRuleService> rules) | ||
{ | ||
_rules = rules; | ||
} | ||
|
||
public IRuleService GetRule(RegionType regionType, RuleType ruleType) | ||
=> _rules.Single(x => x.RegionType == regionType && x.RuleType == ruleType); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
RateLimiter.BusinessLogic/Services/Implementation/RateLimiter/Rules/EU/LastCallTimeRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using RateLimiter.Core.Domain.Enums; | ||
|
||
namespace RateLimiter.BusinessLogic.Services.Implementation.RateLimiter.Rules.EU | ||
{ | ||
public class LastCallTimeRule : IRuleService | ||
{ | ||
public RegionType RegionType => RegionType.EU; | ||
public RuleType RuleType => RuleType.TimeSpanPassedSinceLastCall; | ||
|
||
public Task ApplyRule() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...Limiter.BusinessLogic/Services/Implementation/RateLimiter/Rules/USA/RequestPerTimeRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using RateLimiter.Core.Domain.Enums; | ||
|
||
namespace RateLimiter.BusinessLogic.Services.Implementation.RateLimiter.Rules.USA | ||
{ | ||
public class RequestPerTimeRule : IRuleService | ||
{ | ||
public RegionType RegionType => RegionType.US; | ||
public RuleType RuleType => RuleType.RequestPerTimeSpan; | ||
|
||
public Task ApplyRule() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
Oops, something went wrong.