C# formatting

This commit is contained in:
2026-02-05 20:39:12 +01:00
parent 78cdbfe51e
commit c0756ff2c6
34 changed files with 830 additions and 582 deletions

View File

@@ -1,6 +1,5 @@
using GameList.Data;
using GameList.Endpoints;
using Microsoft.AspNetCore.Authorization;
namespace GameList.Infrastructure;

View File

@@ -3,18 +3,11 @@ using Microsoft.AspNetCore.Authentication;
namespace GameList.Infrastructure;
public class EnsurePlayerExistsMiddleware
public class EnsurePlayerExistsMiddleware(RequestDelegate next)
{
private readonly RequestDelegate _next;
public EnsurePlayerExistsMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context, AppDbContext db)
{
if (context.User?.Identity?.IsAuthenticated == true)
if (context.User.Identity?.IsAuthenticated == true)
{
var id = context.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrWhiteSpace(id) || !Guid.TryParse(id, out var playerId) || await db.Players.FindAsync(playerId) is null)
@@ -25,6 +18,6 @@ public class EnsurePlayerExistsMiddleware
}
}
await _next(context);
await next(context);
}
}

View File

@@ -21,18 +21,15 @@ public static class PasswordHasher
public static bool Verify(string password, byte[] hash, byte[] salt)
{
if (hash is null || salt is null || hash.Length == 0 || salt.Length == 0) return false;
if (hash.Length == 0 || salt.Length == 0)
return false;
var computed = PBKDF2(password, salt);
return CryptographicOperations.FixedTimeEquals(computed, hash);
}
private static byte[] PBKDF2(string password, byte[] salt)
{
return Rfc2898DeriveBytes.Pbkdf2(
Encoding.UTF8.GetBytes(password),
salt,
Iterations,
HashAlgorithmName.SHA256,
KeySize);
return Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, Iterations, HashAlgorithmName.SHA256, KeySize);
}
}

View File

@@ -15,7 +15,8 @@ public class PhaseOrJokerFilter : IEndpointFilter
var httpContext = context.HttpContext;
var db = httpContext.RequestServices.GetRequiredService<AppDbContext>();
var player = await EndpointHelpers.GetAuthenticatedPlayer(httpContext, db);
if (player is null) return Results.Unauthorized();
if (player is null)
return Results.Unauthorized();
var phase = await EndpointHelpers.GetPhase(db, player.Id);
var allow = phase == Phase.Suggest || (phase == Phase.Vote && player.HasJoker);

View File

@@ -4,28 +4,20 @@ using GameList.Endpoints;
namespace GameList.Infrastructure;
public class PhaseRequirementFilter : IEndpointFilter
public class PhaseRequirementFilter(Phase required, bool allowAdminOverride = false) : IEndpointFilter
{
private readonly Phase _required;
private readonly bool _allowAdminOverride;
public PhaseRequirementFilter(Phase required, bool allowAdminOverride = false)
{
_required = required;
_allowAdminOverride = allowAdminOverride;
}
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
var httpContext = context.HttpContext;
var db = httpContext.RequestServices.GetRequiredService<AppDbContext>();
var player = await EndpointHelpers.GetAuthenticatedPlayer(httpContext, db);
if (player is null) return Results.Unauthorized();
if (player is null)
return Results.Unauthorized();
var phase = await EndpointHelpers.GetPhase(db, player.Id);
if (phase != _required && !(_allowAdminOverride && player.IsAdmin))
if (phase != required && !(allowAdminOverride && player.IsAdmin))
{
return EndpointHelpers.PhaseMismatch(_required, phase);
return EndpointHelpers.PhaseMismatch(required, phase);
}
return await next(context);

View File

@@ -27,8 +27,7 @@ public static class PlayerIdentityExtensions
await ctx.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
}
public static Task SignOutPlayerAsync(HttpContext ctx)
=> ctx.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
public static Task SignOutPlayerAsync(HttpContext ctx) => ctx.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
public static IApplicationBuilder UseGlobalExceptionLogging(this IApplicationBuilder app)
{