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:
1 parent
f6fbc082a1
commit
810900cd3e
4 files changed
+22
-8
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user