This repository was archived by the owner on Apr 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-jwt.js
More file actions
46 lines (37 loc) · 1.71 KB
/
test-jwt.js
File metadata and controls
46 lines (37 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Test script for JWT secret handling
process.env.JWT_SECRET = 'test_secure_jwt_key';
process.env.JWT_EXPIRES_IN = '1h';
const jwt = require('jsonwebtoken');
// Test JWT sign/verify with environment variable
const payload = { userId: '123', role: 'user' };
try {
// Sign with environment variable
const token = jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRES_IN });
console.log('Token created successfully:', token);
// Verify with environment variable
const decoded = jwt.verify(token, process.env.JWT_SECRET);
console.log('Token verified successfully:', decoded);
// Security test: try to use fallback secret (should fail)
try {
const badToken = jwt.sign(payload, 'fallback_secret', { expiresIn: '1h' });
const shouldFail = jwt.verify(badToken, process.env.JWT_SECRET);
console.error('❌ SECURITY ISSUE: Fallback secret token verified with real secret');
} catch (err) {
console.log('✅ Security check passed: Tokens signed with different secrets are rejected');
}
// Clear environment variable to test our error handling
const originalSecret = process.env.JWT_SECRET;
delete process.env.JWT_SECRET;
try {
// Load our config module which should check for required environment variables
console.log('Testing config validation without JWT_SECRET...');
const { JWT_CONFIG } = require('./dist/config');
console.error('❌ FAILED: Config did not throw error for missing JWT_SECRET');
} catch (err) {
console.log('✅ Validation check passed: Config properly detected missing JWT_SECRET:', err.message);
}
// Restore environment variable
process.env.JWT_SECRET = originalSecret;
} catch (error) {
console.error('Test failed:', error);
}