fix: add class context to all loggers, format

This commit is contained in:
sam 2024-09-04 14:25:44 +02:00
parent fb324e7576
commit 6c9d1c328b
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
16 changed files with 54 additions and 37 deletions

View file

@ -9,11 +9,13 @@ namespace Foxnouns.Backend.Controllers.Authentication;
[Route("/api/v2/auth")]
public class AuthController(Config config, KeyCacheService keyCacheSvc, ILogger logger) : ApiControllerBase
{
private readonly ILogger _logger = logger.ForContext<AuthController>();
[HttpPost("urls")]
[ProducesResponseType<UrlsResponse>(StatusCodes.Status200OK)]
public async Task<IActionResult> UrlsAsync()
{
logger.Debug("Generating auth URLs for Discord: {Discord}, Google: {Google}, Tumblr: {Tumblr}",
_logger.Debug("Generating auth URLs for Discord: {Discord}, Google: {Google}, Tumblr: {Tumblr}",
config.DiscordAuth.Enabled,
config.GoogleAuth.Enabled,
config.TumblrAuth.Enabled);

View file

@ -20,6 +20,8 @@ public class DiscordAuthController(
RemoteAuthService remoteAuthSvc,
UserRendererService userRendererSvc) : ApiControllerBase
{
private readonly ILogger _logger = logger.ForContext<DiscordAuthController>();
[HttpPost("callback")]
// TODO: duplicating attribute doesn't work, find another way to mark both as possible response
// leaving it here for documentation purposes
@ -34,7 +36,7 @@ public class DiscordAuthController(
var user = await authSvc.AuthenticateUserAsync(AuthType.Discord, remoteUser.Id);
if (user != null) return Ok(await GenerateUserTokenAsync(user));
logger.Debug("Discord user {Username} ({Id}) authenticated with no local account", remoteUser.Username,
_logger.Debug("Discord user {Username} ({Id}) authenticated with no local account", remoteUser.Username,
remoteUser.Id);
var ticket = AuthUtils.RandomToken();
@ -51,7 +53,7 @@ public class DiscordAuthController(
if (remoteUser == null) throw new ApiError.BadRequest("Invalid ticket", "ticket", req.Ticket);
if (await db.AuthMethods.AnyAsync(a => a.AuthType == AuthType.Discord && a.RemoteId == remoteUser.Id))
{
logger.Error("Discord user {Id} has valid ticket but is already linked to an existing account",
_logger.Error("Discord user {Id} has valid ticket but is already linked to an existing account",
remoteUser.Id);
throw new FoxnounsError("Discord ticket was issued for user with existing link");
}
@ -65,13 +67,13 @@ public class DiscordAuthController(
private async Task<AuthController.AuthResponse> GenerateUserTokenAsync(User user)
{
var frontendApp = await db.GetFrontendApplicationAsync();
logger.Debug("Logging user {Id} in with Discord", user.Id);
_logger.Debug("Logging user {Id} in with Discord", user.Id);
var (tokenStr, token) =
authSvc.GenerateToken(user, frontendApp, ["*"], clock.GetCurrentInstant() + Duration.FromDays(365));
db.Add(token);
logger.Debug("Generated token {TokenId} for {UserId}", user.Id, token.Id);
_logger.Debug("Generated token {TokenId} for {UserId}", user.Id, token.Id);
await db.SaveChangesAsync();

View file

@ -13,6 +13,8 @@ public class EmailAuthController(
IClock clock,
ILogger logger) : ApiControllerBase
{
private readonly ILogger _logger = logger.ForContext<EmailAuthController>();
[HttpPost("login")]
[ProducesResponseType<AuthController.AuthResponse>(StatusCodes.Status200OK)]
public async Task<IActionResult> LoginAsync([FromBody] LoginRequest req)
@ -23,13 +25,13 @@ public class EmailAuthController(
var frontendApp = await db.GetFrontendApplicationAsync();
logger.Debug("Logging user {Id} in with email and password", user.Id);
_logger.Debug("Logging user {Id} in with email and password", user.Id);
var (tokenStr, token) =
authSvc.GenerateToken(user, frontendApp, ["*"], clock.GetCurrentInstant() + Duration.FromDays(365));
db.Add(token);
logger.Debug("Generated token {TokenId} for {UserId}", user.Id, token.Id);
_logger.Debug("Generated token {TokenId} for {UserId}", token.Id, user.Id);
await db.SaveChangesAsync();

View file

@ -14,11 +14,13 @@ public class DebugController(
IClock clock,
ILogger logger) : ApiControllerBase
{
private readonly ILogger _logger = logger.ForContext<DebugController>();
[HttpPost("users")]
[ProducesResponseType<AuthController.AuthResponse>(StatusCodes.Status200OK)]
public async Task<IActionResult> CreateUserAsync([FromBody] CreateUserRequest req)
{
logger.Debug("Creating user with username {Username} and email {Email}", req.Username, req.Email);
_logger.Debug("Creating user with username {Username} and email {Email}", req.Username, req.Email);
var user = await authSvc.CreateUserWithPasswordAsync(req.Username, req.Email, req.Password);
var frontendApp = await db.GetFrontendApplicationAsync();