File size: 4,674 Bytes
857cdcf | 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | /**
* Chahuadev Framework - Key System Module (Testing Only)
* JWT Key Management + Security System
*
* DEPRECATED: This module is used for testing purposes only.
* For license validation, use: ./chahua-license-system
*
* Chahua Development Thailand
* CEO: Saharath C.
* www.chahuadev.com
*
* @deprecated Use Chahua License System for production license validation
* @usecase Testing and JWT token generation for test scenarios
*/
const crypto = require('crypto');
const jwt = require('jsonwebtoken');
class KeySystem {
constructor() {
// JWT Secret from environment variable (required for security)
this.jwtSecret = process.env.JWT_SECRET || 'chahuadev-framework-super-secret-key-for-development-2025-v1.0.0-thailand';
this.algorithm = 'HS256';
this.defaultExpiry = '1h';
console.log(' Key System initialized');
if (!process.env.JWT_SECRET) {
console.warn(' Warning: Using fallback JWT secret. Set JWT_SECRET environment variable for production!');
}
}
/**
* Generate JWT token for authentication
* @param {Object} payload - Data to encode in token
* @param {string} expiresIn - Token expiry time
* @returns {string} JWT token
*/
generateToken(payload = {}, expiresIn = this.defaultExpiry) {
try {
const tokenPayload = {
...payload,
iat: Math.floor(Date.now() / 1000),
iss: 'chahuadev-framework'
};
const token = jwt.sign(tokenPayload, this.jwtSecret, {
algorithm: this.algorithm,
expiresIn: expiresIn
});
console.log(' JWT token generated successfully');
return token;
} catch (error) {
console.error(' JWT generation failed:', error.message);
throw new Error('Failed to generate JWT token');
}
}
/**
* Verify and decode JWT token
* @param {string} token - JWT token to verify
* @returns {Object} Decoded payload
*/
verifyToken(token) {
try {
if (!token) {
throw new Error('Token is required');
}
const decoded = jwt.verify(token, this.jwtSecret, {
algorithms: [this.algorithm]
});
console.log(' JWT token verified successfully');
return decoded;
} catch (error) {
console.error(' JWT verification failed:', error.message);
throw new Error(`Invalid token: ${error.message}`);
}
}
/**
* Generate API key for bridge communication
* @returns {string} API key
*/
generateApiKey() {
const apiKey = crypto.randomBytes(32).toString('hex');
console.log(' API key generated');
return apiKey;
}
/**
* Hash sensitive data
* @param {string} data - Data to hash
* @returns {string} Hashed data
*/
hashData(data) {
if (!data) {
throw new Error('Data is required for hashing');
}
const hash = crypto.createHash('sha256')
.update(data)
.digest('hex');
return hash;
}
/**
* Generate secure random string
* @param {number} length - Length of random string
* @returns {string} Random string
*/
generateSecureRandom(length = 16) {
return crypto.randomBytes(Math.ceil(length / 2))
.toString('hex')
.slice(0, length);
}
/**
* Check if token is expired (without throwing error)
* @param {string} token - JWT token to check
* @returns {boolean} True if expired
*/
isTokenExpired(token) {
try {
this.verifyToken(token);
return false;
} catch (error) {
return error.message.includes('expired');
}
}
/**
* Extract payload without verification (for debugging)
* @param {string} token - JWT token
* @returns {Object} Decoded payload (without verification)
*/
decodeTokenUnsafe(token) {
try {
return jwt.decode(token);
} catch (error) {
return null;
}
}
/**
* Get key system status
* @returns {Object} Status information
*/
getStatus() {
return {
algorithm: this.algorithm,
hasJwtSecret: !!process.env.JWT_SECRET,
defaultExpiry: this.defaultExpiry,
ready: true
};
}
}
// Export class (not singleton)
module.exports = KeySystem; |