mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Names the rate limiting policies with constants
Replaces the literal authentication rate limiting policy name with a constant on PresentationApiLayerServices, adds the track stream policy constant next to it, and uses both from the endpoints that register them.
This commit is contained in:
1 parent
eecf4315c6
commit
0ad6e1307b
4 files changed
+25
-4
No files matched your search
+19
-1
@@ -17,6 +17,7 @@ using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.RateLimiting;
|
||||
@@ -31,6 +32,9 @@ namespace Lumina.Presentation.Api.Common.DependencyInjection;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static class PresentationApiLayerServices
|
||||
{
|
||||
public const string AUTHENTICATION_RATE_LIMIT_POLICY = "authenticationPolicy";
|
||||
public const string TRACK_STREAM_RATE_LIMIT_POLICY = "trackStreamPolicy";
|
||||
|
||||
/// <summary>
|
||||
/// Registers the services of the Presentation Api layer into the Dependency Injection container.
|
||||
/// </summary>
|
||||
@@ -94,7 +98,7 @@ public static class PresentationApiLayerServices
|
||||
|
||||
await onRejectedContext.HttpContext.Response.WriteAsJsonAsync(problem, cancellationToken);
|
||||
};
|
||||
rateLimiterOptions.AddPolicy("authenticationPolicy", httpContext =>
|
||||
rateLimiterOptions.AddPolicy(AUTHENTICATION_RATE_LIMIT_POLICY, httpContext =>
|
||||
{
|
||||
string clientIp = httpContext.Request.Headers["X-Forwarded-For"] // First check forwarded headers, in case this is behind a reverse proxy.
|
||||
.FirstOrDefault()?.Split(',')[0].Trim() // Take first IP, if multiple available, to avoid IP spoofing attempts,
|
||||
@@ -110,6 +114,20 @@ public static class PresentationApiLayerServices
|
||||
AutoReplenishment = true // Automatically reset rate limiter tokens.
|
||||
});
|
||||
});
|
||||
rateLimiterOptions.AddPolicy(TRACK_STREAM_RATE_LIMIT_POLICY, httpContext =>
|
||||
{
|
||||
// The track stream is limited per user, so that a single user cannot hold an unbounded number of long lived audio streams open at once.
|
||||
// This is deliberately not the login policy: it is a concurrency limit, partitioned per authenticated user, not a per IP attempt window.
|
||||
string userId = httpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "anonymous";
|
||||
return RateLimitPartition.GetConcurrencyLimiter(
|
||||
partitionKey: $"trackStream_{userId}",
|
||||
factory: _ => new ConcurrencyLimiterOptions
|
||||
{
|
||||
PermitLimit = 4, // Allow 4 concurrent audio streams per user.
|
||||
QueueLimit = 0, // Don't queue requests, a request beyond the limit is rejected instead of waiting.
|
||||
QueueProcessingOrder = QueueProcessingOrder.OldestFirst
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Add authentication and specify the JWT scheme to check tokens against.
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@ using Lumina.Application.Core.UsersManagement.Authentication.Queries.LoginUser;
|
||||
using Lumina.Contracts.Requests.Authentication;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.DependencyInjection;
|
||||
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@@ -42,7 +43,7 @@ public class LoginEndpoint : BaseEndpoint<LoginRequest, IResult>
|
||||
Version(1);
|
||||
AllowAnonymous();
|
||||
DontCatchExceptions();
|
||||
Options(x => x.RequireRateLimiting("authenticationPolicy"));
|
||||
Options(x => x.RequireRateLimiting(PresentationApiLayerServices.AUTHENTICATION_RATE_LIMIT_POLICY));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ using Lumina.Application.Core.UsersManagement.Authentication.Commands.RecoverPas
|
||||
using Lumina.Contracts.Requests.Authentication;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.DependencyInjection;
|
||||
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@@ -41,7 +42,7 @@ public class RecoverPasswordEndpoint : BaseEndpoint<RecoverPasswordRequest, IRes
|
||||
Version(1);
|
||||
AllowAnonymous();
|
||||
DontCatchExceptions();
|
||||
Options(x => x.RequireRateLimiting("authenticationPolicy"));
|
||||
Options(x => x.RequireRateLimiting(PresentationApiLayerServices.AUTHENTICATION_RATE_LIMIT_POLICY));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUs
|
||||
using Lumina.Contracts.Requests.Authentication;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.DependencyInjection;
|
||||
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@@ -41,7 +42,7 @@ public class RegisterEndpoint : BaseEndpoint<RegistrationRequest, IResult>
|
||||
Version(1);
|
||||
AllowAnonymous();
|
||||
DontCatchExceptions();
|
||||
Options(x => x.RequireRateLimiting("authenticationPolicy"));
|
||||
Options(x => x.RequireRateLimiting(PresentationApiLayerServices.AUTHENTICATION_RATE_LIMIT_POLICY));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user