@@ -21,15 +21,15 @@ import {
2121 */
2222export default class SecureJWT {
2323 /** Secret key for encryption */
24- private readonly secret : Buffer
24+ readonly # secret: Buffer
2525 /** Expiration time in milliseconds */
26- private readonly expireInMs : number
26+ readonly # expireInMs: number
2727 /** Token version */
28- private readonly version : string
28+ readonly # version: string
2929 /** Cache for decrypted payload data */
30- private readonly payloadCache : Cache < unknown >
30+ readonly # payloadCache: Cache < unknown >
3131 /** Cache for token verification results */
32- private readonly verifyCache : Cache < boolean >
32+ readonly # verifyCache: Cache < boolean >
3333
3434 /**
3535 * Creates a new SecureJWT instance
@@ -44,11 +44,11 @@ export default class SecureJWT {
4444 if ( options . version !== undefined ) {
4545 ErrorHandler . validateVersion ( options . version )
4646 }
47- this . secret = this . generateSecret ( options . secret )
48- this . expireInMs = parsetimeToMs ( options . expireIn )
49- this . version = options . version ?? '1.0.0'
50- this . payloadCache = new Cache < unknown > ( options . cached ?? 1000 , this . expireInMs )
51- this . verifyCache = new Cache < boolean > ( options . cached ?? 1000 , this . expireInMs )
47+ this . # secret = this . generateSecret ( options . secret )
48+ this . # expireInMs = parsetimeToMs ( options . expireIn )
49+ this . # version = options . version ?? '1.0.0'
50+ this . # payloadCache = new Cache < unknown > ( options . cached ?? 1000 , this . # expireInMs)
51+ this . # verifyCache = new Cache < boolean > ( options . cached ?? 1000 , this . # expireInMs)
5252 }
5353
5454 /**
@@ -72,10 +72,10 @@ export default class SecureJWT {
7272 private encrypt ( data : string ) : TokenEncrypted {
7373 ErrorHandler . validateEncryptionData ( data )
7474 const iv = randomBytes ( 16 )
75- const key = this . secret . subarray ( 0 , 32 )
75+ const key = this . # secret. subarray ( 0 , 32 )
7676 ErrorHandler . validateKeyLength ( key )
7777 const cipher = createCipheriv ( 'aes-256-gcm' , key , iv )
78- cipher . setAAD ( Buffer . from ( `secure-jwt-${ this . version } ` , 'utf8' ) )
78+ cipher . setAAD ( Buffer . from ( `secure-jwt-${ this . # version} ` , 'utf8' ) )
7979 let encrypted = cipher . update ( data , 'utf8' , 'hex' )
8080 encrypted += cipher . final ( 'hex' )
8181 const tag = cipher . getAuthTag ( )
@@ -95,12 +95,12 @@ export default class SecureJWT {
9595 private decrypt ( tokenEncrypted : TokenEncrypted ) : string {
9696 try {
9797 ErrorHandler . validateTokenEncrypted ( tokenEncrypted )
98- const key = this . secret . subarray ( 0 , 32 )
98+ const key = this . # secret. subarray ( 0 , 32 )
9999 ErrorHandler . validateKeyLength ( key )
100100 ErrorHandler . validateIVFormat ( tokenEncrypted . iv )
101101 ErrorHandler . validateTagFormat ( tokenEncrypted . tag )
102102 const decipher = createDecipheriv ( 'aes-256-gcm' , key , Buffer . from ( tokenEncrypted . iv , 'hex' ) )
103- decipher . setAAD ( Buffer . from ( `secure-jwt-${ this . version } ` , 'utf8' ) )
103+ decipher . setAAD ( Buffer . from ( `secure-jwt-${ this . # version} ` , 'utf8' ) )
104104 decipher . setAuthTag ( Buffer . from ( tokenEncrypted . tag , 'hex' ) )
105105 let decrypted = decipher . update ( tokenEncrypted . encrypted , 'hex' , 'utf8' )
106106 decrypted += decipher . final ( 'utf8' )
@@ -124,14 +124,14 @@ export default class SecureJWT {
124124 try {
125125 ErrorHandler . validateData ( data )
126126 const now = Math . floor ( Date . now ( ) / 1000 )
127- const exp = now + Math . floor ( this . expireInMs / 1000 )
127+ const exp = now + Math . floor ( this . # expireInMs / 1000 )
128128 const maxExp = now + 365 * 24 * 60 * 60
129129 ErrorHandler . validateExpiration ( exp , maxExp )
130130 const payload : PayloadData = {
131131 data,
132132 exp,
133133 iat : now ,
134- version : this . version
134+ version : this . # version
135135 }
136136 const payloadString = JSON . stringify ( payload )
137137 ErrorHandler . validatePayloadSize ( payloadString )
@@ -142,7 +142,7 @@ export default class SecureJWT {
142142 tag : tokenEncrypted . tag ,
143143 exp,
144144 iat : now ,
145- version : this . version
145+ version : this . # version
146146 }
147147 const tokenString = JSON . stringify ( tokenData )
148148 return Buffer . from ( tokenString ) . toString ( 'base64' )
@@ -167,8 +167,8 @@ export default class SecureJWT {
167167 */
168168 verify ( token : string ) : boolean {
169169 try {
170- if ( this . verifyCache . has ( token ) ) {
171- const cachedResult = this . verifyCache . get ( token )
170+ if ( this . # verifyCache. has ( token ) ) {
171+ const cachedResult = this . # verifyCache. get ( token )
172172 if ( cachedResult !== undefined ) {
173173 return cachedResult
174174 }
@@ -185,10 +185,10 @@ export default class SecureJWT {
185185 )
186186 ErrorHandler . validateTokenDataIntegrity ( tokenData )
187187 if ( ! isValidTokenData ( tokenData ) ) {
188- this . verifyCache . set ( token , false , 0 )
188+ this . # verifyCache. set ( token , false , 0 )
189189 return false
190190 }
191- ErrorHandler . validateVersionCompatibility ( tokenData . version , this . version )
191+ ErrorHandler . validateVersionCompatibility ( tokenData . version , this . # version)
192192 ErrorHandler . checkTokenExpiration ( tokenData . exp )
193193 const tokenEncrypted : TokenEncrypted = {
194194 encrypted : tokenData . encrypted ,
@@ -201,16 +201,16 @@ export default class SecureJWT {
201201 getErrorMessage ( 'INVALID_PAYLOAD_STRUCTURE' )
202202 )
203203 if ( ! isValidPayloadData ( payload ) ) {
204- this . verifyCache . set ( token , false , 0 )
204+ this . # verifyCache. set ( token , false , 0 )
205205 return false
206206 }
207207 ErrorHandler . validateVersionCompatibility ( payload . version , tokenData . version )
208208 ErrorHandler . checkTokenExpiration ( payload . exp )
209209 ErrorHandler . validateTokenTimestamps ( payload . exp , tokenData . exp , payload . iat , tokenData . iat )
210- this . verifyCache . set ( token , true , Math . max ( 0 , payload . exp * 1000 - Date . now ( ) ) )
210+ this . # verifyCache. set ( token , true , Math . max ( 0 , payload . exp * 1000 - Date . now ( ) ) )
211211 return true
212212 } catch {
213- this . verifyCache . set ( token , false , 0 )
213+ this . # verifyCache. set ( token , false , 0 )
214214 return false
215215 }
216216 }
@@ -238,7 +238,7 @@ export default class SecureJWT {
238238 if ( ! isValidTokenData ( tokenData ) ) {
239239 throw new ValidationError ( getErrorMessage ( 'INVALID_TOKEN_DATA_STRUCTURE' ) )
240240 }
241- ErrorHandler . validateVersionCompatibility ( tokenData . version , this . version )
241+ ErrorHandler . validateVersionCompatibility ( tokenData . version , this . # version)
242242 ErrorHandler . checkTokenExpiration ( tokenData . exp )
243243 const tokenEncrypted : TokenEncrypted = {
244244 encrypted : tokenData . encrypted ,
@@ -269,8 +269,8 @@ export default class SecureJWT {
269269 */
270270 decode ( token : string ) : unknown {
271271 try {
272- if ( this . payloadCache . has ( token ) ) {
273- const cachedResult = this . payloadCache . get ( token )
272+ if ( this . # payloadCache. has ( token ) ) {
273+ const cachedResult = this . # payloadCache. get ( token )
274274 if ( cachedResult !== undefined ) {
275275 return cachedResult
276276 }
@@ -289,7 +289,7 @@ export default class SecureJWT {
289289 if ( ! isValidTokenData ( tokenData ) ) {
290290 throw new ValidationError ( getErrorMessage ( 'INVALID_TOKEN_DATA_STRUCTURE' ) )
291291 }
292- ErrorHandler . validateVersionCompatibility ( tokenData . version , this . version )
292+ ErrorHandler . validateVersionCompatibility ( tokenData . version , this . # version)
293293 ErrorHandler . checkTokenExpiration ( tokenData . exp )
294294 const tokenEncrypted : TokenEncrypted = {
295295 encrypted : tokenData . encrypted ,
@@ -307,7 +307,7 @@ export default class SecureJWT {
307307 ErrorHandler . validateVersionCompatibility ( payload . version , tokenData . version )
308308 ErrorHandler . checkTokenExpiration ( payload . exp )
309309 ErrorHandler . validateTokenTimestamps ( payload . exp , tokenData . exp , payload . iat , tokenData . iat )
310- this . payloadCache . set ( token , payload . data , Math . max ( 0 , payload . exp * 1000 - Date . now ( ) ) )
310+ this . # payloadCache. set ( token , payload . data , Math . max ( 0 , payload . exp * 1000 - Date . now ( ) ) )
311311 return payload . data
312312 } catch ( error ) {
313313 if (
0 commit comments