C# formatting
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using GameList.Data;
|
||||
using GameList.Endpoints;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace GameList.Infrastructure;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user