fix(auth): add JWT__RefreshKey config and env var fallback in JwtService

- Add JWT__RefreshKey to .env.example with documentation
- Add JWT__RefreshKey to docker-compose.yml and docker-compose.prod.yml
- Fix JwtService to check both IConfiguration and env vars (JWT__REFRESHKEY)
  for access token key and refresh token key, matching Program.cs pattern
- Fixes 'Jwt:RefreshKey is not configured' 500 error on register/login
This commit is contained in:
Arnaud Dartois committed 2026-07-25 18:02:43 +00:00
1 parent f6fbc082a1
commit 810900cd3e
4 files changed
+22 -8

No files matched your search

@@ -23,8 +23,7 @@ public class JwtService : IJwtService
public (string token, DateTime expiresAt) GenerateAccessToken(AppUser user, IList<string> roles, IList<string> permissions)
{
var key = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]
?? throw new InvalidOperationException("JWT key is not configured")));
Encoding.UTF8.GetBytes(GetJwtKey()));
var expiresAt = DateTime.UtcNow.AddMinutes(
double.TryParse(_configuration["Jwt:AccessTokenExpiryMinutes"], out var minutes) ? minutes : 15);
@@ -57,8 +56,7 @@ public class JwtService : IJwtService
public (string token, DateTime expiresAt) GenerateRefreshToken(AppUser user)
{
var refreshKeyValue = _configuration["Jwt:RefreshKey"]
?? throw new InvalidOperationException("Jwt:RefreshKey is not configured");
var refreshKeyValue = GetJwtRefreshKey();
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(refreshKeyValue));
var expiresAt = DateTime.UtcNow.AddDays(
@@ -84,10 +82,7 @@ public class JwtService : IJwtService
public ClaimsPrincipal? ValidateRefreshToken(string refreshToken)
{
var refreshKeyValue = _configuration["Jwt:RefreshKey"]
?? throw new InvalidOperationException(
"Jwt:RefreshKey is not configured. " +
"It must be set separately from Jwt:Key (security requirement).");
var refreshKeyValue = GetJwtRefreshKey();
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(refreshKeyValue));
try
@@ -116,6 +111,20 @@ public class JwtService : IJwtService
}
}
private string GetJwtKey()
{
return _configuration["Jwt:Key"]
?? Environment.GetEnvironmentVariable("JWT__KEY")
?? throw new InvalidOperationException("Jwt:Key is not configured (set JWT__KEY env var)");
}
private string GetJwtRefreshKey()
{
return _configuration["Jwt:RefreshKey"]
?? Environment.GetEnvironmentVariable("JWT__REFRESHKEY")
?? throw new InvalidOperationException("Jwt:RefreshKey is not configured (set JWT__REFRESHKEY env var)");
}
public string? GetUserIdFromExpiredToken(string token)
{
try