/** * 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;