@@ -16,8 +16,8 @@ import {
1616} from '@utils/index'
1717
1818/**
19- * Secure JWT implementation with encryption support
20- * Handles token creation, verification , and data extraction
19+ * JWT implementation with encryption
20+ * Creates, verifies , and extracts data from tokens
2121 */
2222export default class SecureJWT {
2323 /** Secret key for encryption */
@@ -33,17 +33,18 @@ export default class SecureJWT {
3333
3434 /**
3535 * Creates a new SecureJWT instance
36- * @param options - The configuration for token expiration, secret key, and version
36+ * @param options - Configuration for token expiration, secret key, and version
3737 */
3838 constructor ( options : JWTOptions ) {
3939 ErrorHandler . validateOptions ( options )
4040 ErrorHandler . validateExpireIn ( options . expireIn )
41- if ( options . secret !== undefined ) {
42- ErrorHandler . validateSecret ( options . secret )
43- }
41+ ErrorHandler . validateSecret ( options . secret )
4442 if ( options . version !== undefined ) {
4543 ErrorHandler . validateVersion ( options . version )
4644 }
45+ if ( options . cached !== undefined ) {
46+ ErrorHandler . validateCacheSize ( options . cached )
47+ }
4748 this . #secret = this . generateSecret ( options . secret )
4849 this . #expireInMs = parsetimeToMs ( options . expireIn )
4950 this . #version = options . version ?? '1.0.0'
@@ -52,22 +53,19 @@ export default class SecureJWT {
5253 }
5354
5455 /**
55- * Creates a secret key from the provided secret or generates a random one
56- * @param secret - The optional secret string for key creation
57- * @returns The Buffer containing the secret key
56+ * Creates a secret key from the provided secret
57+ * @param secret - Secret string for key creation
58+ * @returns Buffer containing the secret key
5859 */
59- private generateSecret ( secret ?: string ) : Buffer {
60- if ( secret != null && secret . length > 0 ) {
61- const salt = randomBytes ( 32 )
62- return Buffer . concat ( [ salt , Buffer . from ( secret , 'utf8' ) ] )
63- }
64- return randomBytes ( 32 )
60+ private generateSecret ( secret : string ) : Buffer {
61+ const salt = randomBytes ( 32 )
62+ return Buffer . concat ( [ salt , Buffer . from ( secret , 'utf8' ) ] )
6563 }
6664
6765 /**
6866 * Encrypts data using AES-256-GCM encryption
69- * @param data - The string data to encrypt
70- * @returns The object with encrypted data, initialization vector, and authentication tag
67+ * @param data - String data to encrypt
68+ * @returns Object with encrypted data, initialization vector, and authentication tag
7169 */
7270 private encrypt ( data : string ) : TokenEncrypted {
7371 ErrorHandler . validateEncryptionData ( data )
@@ -89,8 +87,8 @@ export default class SecureJWT {
8987
9088 /**
9189 * Decrypts data using AES-256-GCM decryption
92- * @param tokenEncrypted - The object with encrypted data, initialization vector, and authentication tag
93- * @returns The decrypted data as string
90+ * @param tokenEncrypted - Object with encrypted data, initialization vector, and authentication tag
91+ * @returns Decrypted data as string
9492 */
9593 private decrypt ( tokenEncrypted : TokenEncrypted ) : string {
9694 try {
@@ -116,9 +114,9 @@ export default class SecureJWT {
116114 }
117115
118116 /**
119- * Creates a secure JWT token from data
120- * @param data - The data to sign (will be JSON stringified)
121- * @returns The JWT token string
117+ * Creates a JWT token from data
118+ * @param data - Data to sign (will be JSON stringified)
119+ * @returns JWT token string
122120 */
123121 sign ( data : unknown ) : string {
124122 try {
@@ -162,8 +160,8 @@ export default class SecureJWT {
162160
163161 /**
164162 * Validates if a JWT token is valid
165- * @param token - The Base64 encoded token to check
166- * @returns The boolean indicating if token is valid and not expired
163+ * @param token - Base64 encoded token to check
164+ * @returns Boolean indicating if token is valid and not expired
167165 */
168166 verify ( token : string ) : boolean {
169167 try {
@@ -217,7 +215,7 @@ export default class SecureJWT {
217215
218216 /**
219217 * Validates a JWT token and throws specific errors
220- * @param token - The Base64 encoded token to validate
218+ * @param token - Base64 encoded token to validate
221219 * @throws {ValidationError } When token format is invalid
222220 * @throws {TokenExpiredError } When token has expired
223221 * @throws {DecryptionError } When token decryption fails
@@ -260,8 +258,8 @@ export default class SecureJWT {
260258
261259 /**
262260 * Extracts data from a JWT token
263- * @param token - The Base64 encoded token to decode
264- * @returns The decoded payload data
261+ * @param token - Base64 encoded token to decode
262+ * @returns Decoded payload data
265263 * @throws {ValidationError } When token format is invalid
266264 * @throws {TokenExpiredError } When token has expired
267265 * @throws {DecryptionError } When token decryption fails
0 commit comments