diff --git a/src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs b/src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs
index b5a4de98..7a8823db 100644
--- a/src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs
+++ b/src/Lumina.Presentation.Api/Common/DependencyInjection/PresentationApiLayerServices.cs
@@ -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";
+
///
/// Registers the services of the Presentation Api layer into the Dependency Injection container.
///
@@ -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.
diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Login/LoginEndpoint.cs b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Login/LoginEndpoint.cs
index d3bd7ea2..e6f568d1 100644
--- a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Login/LoginEndpoint.cs
+++ b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Login/LoginEndpoint.cs
@@ -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
Version(1);
AllowAnonymous();
DontCatchExceptions();
- Options(x => x.RequireRateLimiting("authenticationPolicy"));
+ Options(x => x.RequireRateLimiting(PresentationApiLayerServices.AUTHENTICATION_RATE_LIMIT_POLICY));
}
///
diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/RecoverPassword/RecoverPasswordEndpoint.cs b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/RecoverPassword/RecoverPasswordEndpoint.cs
index 2077b918..f83a6b69 100644
--- a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/RecoverPassword/RecoverPasswordEndpoint.cs
+++ b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/RecoverPassword/RecoverPasswordEndpoint.cs
@@ -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 x.RequireRateLimiting("authenticationPolicy"));
+ Options(x => x.RequireRateLimiting(PresentationApiLayerServices.AUTHENTICATION_RATE_LIMIT_POLICY));
}
///
diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Register/RegisterEndpoint.cs b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Register/RegisterEndpoint.cs
index 0af9ce38..d74178f9 100644
--- a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Register/RegisterEndpoint.cs
+++ b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authentication/Register/RegisterEndpoint.cs
@@ -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
Version(1);
AllowAnonymous();
DontCatchExceptions();
- Options(x => x.RequireRateLimiting("authenticationPolicy"));
+ Options(x => x.RequireRateLimiting(PresentationApiLayerServices.AUTHENTICATION_RATE_LIMIT_POLICY));
}
///