refactor(backend): use explicit types instead of var by default
This commit is contained in:
parent
bc7fd6d804
commit
649988db25
52 changed files with 506 additions and 420 deletions
|
@ -8,8 +8,8 @@ public class AuthenticationMiddleware(DatabaseContext db) : IMiddleware
|
|||
{
|
||||
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
|
||||
{
|
||||
var endpoint = ctx.GetEndpoint();
|
||||
var metadata = endpoint?.Metadata.GetMetadata<AuthenticateAttribute>();
|
||||
Endpoint? endpoint = ctx.GetEndpoint();
|
||||
AuthenticateAttribute? metadata = endpoint?.Metadata.GetMetadata<AuthenticateAttribute>();
|
||||
|
||||
if (metadata == null)
|
||||
{
|
||||
|
@ -18,14 +18,17 @@ public class AuthenticationMiddleware(DatabaseContext db) : IMiddleware
|
|||
}
|
||||
|
||||
if (
|
||||
!AuthUtils.TryParseToken(ctx.Request.Headers.Authorization.ToString(), out var rawToken)
|
||||
!AuthUtils.TryParseToken(
|
||||
ctx.Request.Headers.Authorization.ToString(),
|
||||
out byte[]? rawToken
|
||||
)
|
||||
)
|
||||
{
|
||||
await next(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
var oauthToken = await db.GetToken(rawToken);
|
||||
Token? oauthToken = await db.GetToken(rawToken);
|
||||
if (oauthToken == null)
|
||||
{
|
||||
await next(ctx);
|
||||
|
@ -50,7 +53,7 @@ public static class HttpContextExtensions
|
|||
|
||||
public static Token? GetToken(this HttpContext ctx)
|
||||
{
|
||||
if (ctx.Items.TryGetValue(Key, out var token))
|
||||
if (ctx.Items.TryGetValue(Key, out object? token))
|
||||
return token as Token;
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ public class AuthorizationMiddleware : IMiddleware
|
|||
{
|
||||
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
|
||||
{
|
||||
var endpoint = ctx.GetEndpoint();
|
||||
var attribute = endpoint?.Metadata.GetMetadata<AuthorizeAttribute>();
|
||||
Endpoint? endpoint = ctx.GetEndpoint();
|
||||
AuthorizeAttribute? attribute = endpoint?.Metadata.GetMetadata<AuthorizeAttribute>();
|
||||
|
||||
if (attribute == null)
|
||||
{
|
||||
|
@ -16,21 +16,27 @@ public class AuthorizationMiddleware : IMiddleware
|
|||
return;
|
||||
}
|
||||
|
||||
var token = ctx.GetToken();
|
||||
Token? token = ctx.GetToken();
|
||||
if (token == null)
|
||||
{
|
||||
throw new ApiError.Unauthorized(
|
||||
"This endpoint requires an authenticated user.",
|
||||
ErrorCode.AuthenticationRequired
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
attribute.Scopes.Length > 0
|
||||
&& attribute.Scopes.Except(token.Scopes.ExpandScopes()).Any()
|
||||
)
|
||||
{
|
||||
throw new ApiError.Forbidden(
|
||||
"This endpoint requires ungranted scopes.",
|
||||
attribute.Scopes.Except(token.Scopes.ExpandScopes()),
|
||||
ErrorCode.MissingScopes
|
||||
);
|
||||
}
|
||||
|
||||
if (attribute.RequireAdmin && token.User.Role != UserRole.Admin)
|
||||
throw new ApiError.Forbidden("This endpoint can only be used by admins.");
|
||||
if (
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Net;
|
||||
using Foxnouns.Backend.Database.Models;
|
||||
using Foxnouns.Backend.Utils;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
@ -14,9 +15,9 @@ public class ErrorHandlerMiddleware(ILogger baseLogger, IHub sentry) : IMiddlewa
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var type = e.TargetSite?.DeclaringType ?? typeof(ErrorHandlerMiddleware);
|
||||
var typeName = e.TargetSite?.DeclaringType?.FullName ?? "<unknown>";
|
||||
var logger = baseLogger.ForContext(type);
|
||||
Type type = e.TargetSite?.DeclaringType ?? typeof(ErrorHandlerMiddleware);
|
||||
string typeName = e.TargetSite?.DeclaringType?.FullName ?? "<unknown>";
|
||||
ILogger logger = baseLogger.ForContext(type);
|
||||
|
||||
if (ctx.Response.HasStarted)
|
||||
{
|
||||
|
@ -31,13 +32,15 @@ public class ErrorHandlerMiddleware(ILogger baseLogger, IHub sentry) : IMiddlewa
|
|||
e,
|
||||
scope =>
|
||||
{
|
||||
var user = ctx.GetUser();
|
||||
User? user = ctx.GetUser();
|
||||
if (user != null)
|
||||
{
|
||||
scope.User = new SentryUser
|
||||
{
|
||||
Id = user.Id.ToString(),
|
||||
Username = user.Username,
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -98,17 +101,19 @@ public class ErrorHandlerMiddleware(ILogger baseLogger, IHub sentry) : IMiddlewa
|
|||
logger.Error(e, "Exception in {ClassName} ({Path})", typeName, ctx.Request.Path);
|
||||
}
|
||||
|
||||
var errorId = sentry.CaptureException(
|
||||
SentryId errorId = sentry.CaptureException(
|
||||
e,
|
||||
scope =>
|
||||
{
|
||||
var user = ctx.GetUser();
|
||||
User? user = ctx.GetUser();
|
||||
if (user != null)
|
||||
{
|
||||
scope.User = new SentryUser
|
||||
{
|
||||
Id = user.Id.ToString(),
|
||||
Username = user.Username,
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue