Add JWT_SECRET environment variable to Docker configurations and update JwtService to use it

This commit is contained in:
Aaron Po
2026-02-07 20:04:19 -05:00
parent 6d812638ba
commit a1ea6391bc
4 changed files with 9 additions and 5 deletions

View File

@@ -79,6 +79,7 @@ services:
ASPNETCORE_URLS: "http://0.0.0.0:8080" ASPNETCORE_URLS: "http://0.0.0.0:8080"
DOTNET_RUNNING_IN_CONTAINER: "true" DOTNET_RUNNING_IN_CONTAINER: "true"
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}" DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
JWT_SECRET: "${JWT_SECRET}"
restart: unless-stopped restart: unless-stopped
networks: networks:
- devnet - devnet

View File

@@ -59,6 +59,7 @@ services:
DOTNET_RUNNING_IN_CONTAINER: "true" DOTNET_RUNNING_IN_CONTAINER: "true"
MASTER_DB_CONNECTION_STRING: "${MASTER_DB_CONNECTION_STRING}" MASTER_DB_CONNECTION_STRING: "${MASTER_DB_CONNECTION_STRING}"
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}" DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
JWT_SECRET: "${JWT_SECRET}"
restart: unless-stopped restart: unless-stopped
networks: networks:
- prodnet - prodnet

View File

@@ -73,6 +73,7 @@ services:
environment: environment:
DOTNET_RUNNING_IN_CONTAINER: "true" DOTNET_RUNNING_IN_CONTAINER: "true"
DB_CONNECTION_STRING: "${TEST_DB_CONNECTION_STRING}" DB_CONNECTION_STRING: "${TEST_DB_CONNECTION_STRING}"
JWT_SECRET: "${JWT_SECRET}"
volumes: volumes:
- ./test-results:/app/test-results - ./test-results:/app/test-results
restart: "no" restart: "no"
@@ -93,6 +94,7 @@ services:
environment: environment:
DOTNET_RUNNING_IN_CONTAINER: "true" DOTNET_RUNNING_IN_CONTAINER: "true"
DB_CONNECTION_STRING: "${TEST_DB_CONNECTION_STRING}" DB_CONNECTION_STRING: "${TEST_DB_CONNECTION_STRING}"
JWT_SECRET: "${JWT_SECRET}"
volumes: volumes:
- ./test-results:/app/test-results - ./test-results:/app/test-results
restart: "no" restart: "no"

View File

@@ -1,3 +1,4 @@
using System;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@@ -6,14 +7,13 @@ using Microsoft.IdentityModel.Tokens;
using JwtRegisteredClaimNames = System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames; using JwtRegisteredClaimNames = System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames;
namespace ServiceCore.Services; namespace ServiceCore.Services;
public class JwtService(IConfiguration config) : IJwtService public class JwtService : IJwtService
{ {
// private readonly string? _secret = config["Jwt:Secret"]; private readonly string? _secret = Environment.GetEnvironmentVariable("JWT_SECRET");
private readonly string? _secret = "128490218jfklsdajfdsa90f8sd0fid0safasr31jl2k1j4AFSDR!@#$fdsafjdslajfl";
public string GenerateJwt(Guid userId, string username, DateTime expiry) public string GenerateJwt(Guid userId, string username, DateTime expiry)
{ {
var handler = new JsonWebTokenHandler(); var handler = new JsonWebTokenHandler();
var key = Encoding.UTF8.GetBytes(_secret ?? throw new InvalidOperationException("secret not set")); var key = Encoding.UTF8.GetBytes(_secret ?? throw new InvalidOperationException("secret not set"));
// Base claims (always present) // Base claims (always present)
@@ -35,4 +35,4 @@ public class JwtService(IConfiguration config) : IJwtService
return handler.CreateToken(tokenDescriptor); return handler.CreateToken(tokenDescriptor);
} }
} }