Slight refactor and polish

This commit is contained in:
FeTetra 2025-02-16 21:51:56 -05:00
commit 0c8ca731a4
6 changed files with 127 additions and 115 deletions

View file

@ -3,46 +3,12 @@ using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Middlewares;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Logging;
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Middlewares;
public class EmailEnforcementMiddleware : MiddlewareDBContext
{
private static readonly HashSet<string> enforcedPaths = new()
{
"showModerated",
"rateUserComment",
"rateComment",
"comments",
"userComments",
"postUserComment",
"postComment",
"deleteUserComment",
"deleteComment",
"slots",
"upload",
"r",
"s",
"uploadPhoto",
"photos",
"deletePhoto",
"match",
"play",
"enterLevel",
"user",
"users",
"updateUser",
"update_my_pins",
"startPublish",
"publish",
"unpublish",
"playlists",
"tags",
"tag",
"searches",
"genres",
};
private static readonly HashSet<string> enforcedPaths = EmailEnforcementConfiguration.Instance.BlockedEndpoints;
public EmailEnforcementMiddleware(RequestDelegate next) : base(next)
{ }
@ -51,37 +17,41 @@ public class EmailEnforcementMiddleware : MiddlewareDBContext
{
// Split path into segments
string[] pathSegments = context.Request.Path.ToString().Split("/", StringSplitOptions.RemoveEmptyEntries);
bool emailEnforcementEnabled = EnforceEmailConfiguration.Instance.EnableEmailEnforcement;
if (pathSegments[0] == "LITTLEBIGPLANETPS3_XML")
{
// Get user via GameToken
GameTokenEntity? token = await database.GameTokenFromRequest(context.Request);
UserEntity? user = await database.UserFromGameToken(token);
// Check second part of path to see if client is within an enforced path
if (enforcedPaths.Contains(pathSegments[1]))
if (EmailEnforcementConfiguration.Instance.EnableEmailEnforcement)
{
// Check if user is valid
if (user == null)
{
// Send bad request status
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Not a valid user");
// Get user via GameToken
GameTokenEntity? token = await database.GameTokenFromRequest(context.Request);
UserEntity? user = await database.UserFromGameToken(token);
// Don't go to next in pipeline
return;
}
// Check if email is there and verified
if (emailEnforcementEnabled && (!user.EmailAddressVerified || user.EmailAddress == null))
// Check second part of path to see if client is within an enforced path
// This could probably be reworked, seeing as you may want to check for a deeper sub-path
// But it should be perfectly fine for now
if (enforcedPaths.Contains(pathSegments[1]))
{
// Send bad request status
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid user email address");
// Don't go to next in pipeline
return;
// Check if user is valid, don't want any exceptions
if (user == null)
{
// Send bad request status
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Not a valid user");
// Don't go to next in pipeline
return;
}
// Check if email is there and verified
if (!user.EmailAddressVerified || user.EmailAddress == null)
{
// Send bad request status
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid user email address");
// Don't go to next in pipeline
return;
}
}
}
}