From 810900cd3e8fa657cd8cc1a6d9888f009a37cd86 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Sat, 25 Jul 2026 18:02:43 +0000 Subject: [PATCH] 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 --- .env.example | 3 +++ docker/docker-compose.prod.yml | 1 + docker/docker-compose.yml | 1 + .../Security/JwtService.cs | 25 +++++++++++++------ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index ba7bc71..5067564 100644 --- a/.env.example +++ b/.env.example @@ -8,7 +8,10 @@ DATABASE_URL=Host=postgresql;Port=5432;Database=heimdall;Username=heimdall;Password=heimdall # JWT (change in production!) +# JWT__Key is used for access tokens, JWT__RefreshKey for refresh tokens. +# Both must be at least 32 bytes (256 bits) — use different values for security. JWT__KEY=ChangeMeToAtLeast32BytesLongKeyInProd! +JWT__REFRESHKEY=ChangeMeToAtLeast32BytesRefreshKeyInProd! JWT__ISSUER=heimdall JWT__AUDIENCE=heimdall-api diff --git a/docker/docker-compose.prod.yml b/docker/docker-compose.prod.yml index 8674750..56e55bd 100644 --- a/docker/docker-compose.prod.yml +++ b/docker/docker-compose.prod.yml @@ -79,6 +79,7 @@ services: DATABASE_URL: ${DATABASE_URL:?DATABASE_URL is required} # JWT JWT__Key: ${JWT__Key:?JWT__Key is required} + JWT__RefreshKey: ${JWT__RefreshKey:?JWT__RefreshKey is required} JWT__Issuer: ${JWT__Issuer:-heimdall} JWT__Audience: ${JWT__Audience:-heimdall-api} # ASP.NET diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index a07124c..fc1c9ed 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -79,6 +79,7 @@ services: DATABASE_URL: ${DATABASE_URL:-Host=postgresql;Port=5432;Database=heimdall;Username=heimdall;Password=heimdall} # JWT JWT__Key: ${JWT__Key} + JWT__RefreshKey: ${JWT__RefreshKey} JWT__Issuer: ${JWT__Issuer:-heimdall} JWT__Audience: ${JWT__Audience:-heimdall-api} # ASP.NET diff --git a/src/Heimdall.Infrastructure/Security/JwtService.cs b/src/Heimdall.Infrastructure/Security/JwtService.cs index 251ff34..8fe6046 100644 --- a/src/Heimdall.Infrastructure/Security/JwtService.cs +++ b/src/Heimdall.Infrastructure/Security/JwtService.cs @@ -23,8 +23,7 @@ public class JwtService : IJwtService public (string token, DateTime expiresAt) GenerateAccessToken(AppUser user, IList roles, IList 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