fix(auth): safely report cookie sign-in failures
Heimdall Gitea / Detect CI scope (pull_request) Successful in 8s
Heimdall Gitea / Build and tests (pull_request) Failing after 15m39s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 5s

This commit is contained in:
arnaud.dartois committed 2026-11-08 18:10:25 +01:00
1 parent b3d5ea5add
commit 49b8bcc895
11 files changed
+224 -27

No files matched your search

+21 -1
View File
@@ -974,11 +974,13 @@ public partial class Program
{
if (string.IsNullOrEmpty(request.AccessToken))
return Results.BadRequest(new { error = "AccessToken is required" });
var failureStage = "token validation";
try
{
var handler = new JwtSecurityTokenHandler();
var principal = handler.ValidateToken(request.AccessToken,
GetJwtValidationParameters(jwtKey, jwtIssuer, jwtAudience), out _);
failureStage = "account session validation";
if (!await ValidateAccountSessionAsync(principal, httpContext))
return Results.Unauthorized();
var userId = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
@@ -1003,6 +1005,7 @@ public partial class Program
claims.Add(new Claim("permission", permission.Value));
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
failureStage = "cookie issuance";
await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity),
new AuthenticationProperties
{
@@ -1013,7 +1016,24 @@ public partial class Program
return Results.Ok();
}
catch (SecurityTokenExpiredException) { return Results.Unauthorized(); }
catch (Exception ex) { return Results.BadRequest(new { error = $"Token validation failed: {ex.Message}" }); }
catch (SecurityTokenException ex)
{
logger.LogWarning("Cookie sign-in rejected a token (error type {ErrorType})", ex.GetType().Name);
return Results.Unauthorized();
}
catch (ArgumentException ex) when (failureStage == "token validation")
{
logger.LogWarning("Cookie sign-in rejected a token (error type {ErrorType})", ex.GetType().Name);
return Results.Unauthorized();
}
catch (Exception ex)
{
logger.LogError("Cookie sign-in failed during {FailureStage} (error type {ErrorType})",
failureStage, ex.GetType().Name);
return Results.Problem(
statusCode: StatusCodes.Status500InternalServerError,
title: "Authentication session could not be created");
}
});
app.MapPost("/_auth/signout", async (HttpContext httpContext, ILogger<Program> logger) =>