55 CommonAccessTokenFactory
66} from './cat' ;
77import { KeyNotFoundError } from './errors' ;
8+ import { generateRandomHex , toBase64 } from './util' ;
89
910export { CommonAccessToken } from './cat' ;
1011export { CommonAccessTokenRenewal } from './catr' ;
@@ -225,7 +226,7 @@ export class CAT {
225226 opts ?: CatGenerateOptions
226227 ) {
227228 if ( opts ?. generateCwtId ) {
228- claims [ 'cti' ] = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
229+ claims [ 'cti' ] = generateRandomHex ( 16 ) ;
229230 }
230231 const cat = new CommonAccessToken ( claims ) ;
231232 if ( opts && opts . type == 'mac' ) {
@@ -239,7 +240,7 @@ export class CAT {
239240 if ( ! cat . raw ) {
240241 throw new Error ( 'Failed to MAC token' ) ;
241242 }
242- return cat . raw . toString ( 'base64' ) ;
243+ return toBase64 ( cat . raw ) ;
243244 }
244245 }
245246
@@ -281,7 +282,7 @@ export class CAT {
281282 opts ?: CatGenerateOptions
282283 ) {
283284 if ( opts ?. generateCwtId ) {
284- dict [ 'cti' ] = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
285+ dict [ 'cti' ] = generateRandomHex ( 16 ) ;
285286 }
286287 const cat = CommonAccessTokenFactory . fromDict ( dict ) ;
287288 if ( opts && opts . type == 'mac' ) {
@@ -295,7 +296,7 @@ export class CAT {
295296 if ( ! cat . raw ) {
296297 throw new Error ( 'Failed to MAC token' ) ;
297298 }
298- return cat . raw . toString ( 'base64' ) ;
299+ return toBase64 ( cat . raw ) ;
299300 }
300301 }
301302
@@ -313,7 +314,7 @@ export class CAT {
313314 opts : CatRenewOptions
314315 ) : Promise < string > {
315316 const newClaims = cat . claims ;
316- newClaims [ 'cti' ] = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
317+ newClaims [ 'cti' ] = generateRandomHex ( 16 ) ;
317318 newClaims [ 'iat' ] = Math . floor ( Date . now ( ) / 1000 ) ;
318319 newClaims [ 'iss' ] = opts . issuer ;
319320 newClaims [ 'exp' ] = newClaims [ 'iat' ] + ( newClaims [ 'catr' ] as any ) [ 'expadd' ] ;
@@ -329,6 +330,6 @@ export class CAT {
329330 if ( ! newCat . raw ) {
330331 throw new Error ( 'Failed to MAC token' ) ;
331332 }
332- return newCat . raw . toString ( 'base64' ) ;
333+ return toBase64 ( newCat . raw ) ;
333334 }
334335}
0 commit comments