Skip to content

Commit 1639d16

Browse files
committed
refactor(auth): replace hardcoded secret key with environment variable
- Remove hardcoded secret key from JwtAuthTokenService - Use EnvironmentConfig.jwtSecretKey for token signing and verification - Import EnvironmentConfig from ht_api package
1 parent f0b2068 commit 1639d16

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

lib/src/services/jwt_auth_token_service.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
2+
import 'package:ht_api/src/config/environment_config.dart';
23
import 'package:ht_api/src/services/auth_token_service.dart';
34
import 'package:ht_api/src/services/token_blacklist_service.dart';
45
import 'package:ht_data_repository/ht_data_repository.dart';
@@ -33,11 +34,6 @@ class JwtAuthTokenService implements AuthTokenService {
3334

3435
// --- Configuration ---
3536

36-
// WARNING: Hardcoding secrets is insecure. Use environment variables
37-
// or a proper secrets management solution in production.
38-
static const String _secretKey =
39-
'your-very-hardcoded-super-secret-key-replace-this-in-prod';
40-
4137
// Define token issuer and default expiry duration
4238
static const String _issuer = 'http://localhost:8080';
4339
static const Duration _tokenExpiryDuration = Duration(hours: 1);
@@ -71,7 +67,7 @@ class JwtAuthTokenService implements AuthTokenService {
7167

7268
// Sign the token using HMAC-SHA256
7369
final token = jwt.sign(
74-
SecretKey(_secretKey),
70+
SecretKey(EnvironmentConfig.jwtSecretKey),
7571
algorithm: JWTAlgorithm.HS256,
7672
expiresIn: _tokenExpiryDuration, // Redundant but safe
7773
);
@@ -93,7 +89,7 @@ class JwtAuthTokenService implements AuthTokenService {
9389
try {
9490
// Verify the token's signature and expiry
9591
_log.finer('[validateToken] Verifying token signature and expiry...');
96-
final jwt = JWT.verify(token, SecretKey(_secretKey));
92+
final jwt = JWT.verify(token, SecretKey(EnvironmentConfig.jwtSecretKey));
9793
_log.finer('[validateToken] Token verified. Payload: ${jwt.payload}');
9894

9995
// --- Blacklist Check ---
@@ -216,7 +212,7 @@ class JwtAuthTokenService implements AuthTokenService {
216212
_log.finer('[invalidateToken] Verifying signature (ignoring expiry)...');
217213
final jwt = JWT.verify(
218214
token,
219-
SecretKey(_secretKey),
215+
SecretKey(EnvironmentConfig.jwtSecretKey),
220216
checkExpiresIn: false, // IMPORTANT: Don't fail if expired here
221217
checkHeaderType: true, // Keep other standard checks
222218
);

0 commit comments

Comments
 (0)