1
1
import { Buffer } from 'buffer' ;
2
2
import type { Readable } from 'stream' ;
3
3
import { v4 as uuidv4 } from 'uuid' ;
4
+ import { SignJWT , importPKCS8 } from 'jose' ;
4
5
5
6
export function isBrowser ( ) {
6
7
return (
@@ -266,11 +267,6 @@ export type JwtSignOptions = {
266
267
subject ?: string | undefined ;
267
268
issuer ?: string | undefined ;
268
269
jwtid ?: string | undefined ;
269
- mutatePayload ?: boolean | undefined ;
270
- noTimestamp ?: boolean | undefined ;
271
- encoding ?: string | undefined ;
272
- allowInsecureKeySizes ?: boolean | undefined ;
273
- allowInvalidAsymmetricKeyTypes ?: boolean | undefined ;
274
270
} ;
275
271
276
272
/**
@@ -281,15 +277,36 @@ export type JwtSignOptions = {
281
277
* @param options
282
278
* @returns
283
279
*/
284
- export function createJwtAssertion (
280
+ export async function createJwtAssertion (
285
281
claims : {
286
282
readonly [ key : string ] : any ;
287
283
} ,
288
284
key : JwtKey ,
289
285
options : JwtSignOptions
290
- ) : string {
291
- const jwt = eval ( 'require' ) ( 'jsonwebtoken' ) ;
292
- return jwt . sign ( claims , key , options ) ;
286
+ ) : Promise < string > {
287
+ const crypto = eval ( 'require' ) ( 'crypto' ) ;
288
+ const privateKey = crypto . createPrivateKey ( {
289
+ key : key . key ,
290
+ format : 'pem' ,
291
+ type : 'pkcs8' ,
292
+ passphrase : key . passphrase ,
293
+ } ) ;
294
+ const pem = privateKey . export ( { type : 'pkcs8' , format : 'pem' } ) . toString ( ) ;
295
+ const pkcs8 = await importPKCS8 ( pem , options . algorithm || 'RS256' ) ;
296
+ let signer = new SignJWT ( claims ) ;
297
+ signer = options . audience ? signer . setAudience ( options . audience ) : signer ;
298
+ signer = options . expiresIn
299
+ ? signer . setExpirationTime ( options . expiresIn )
300
+ : signer ;
301
+ signer = options . issuer ? signer . setIssuer ( options . issuer ) : signer ;
302
+ signer = options . jwtid ? signer . setJti ( options . jwtid ) : signer ;
303
+ signer = options . notBefore ? signer . setNotBefore ( options . notBefore ) : signer ;
304
+ signer = options . subject ? signer . setSubject ( options . subject ) : signer ;
305
+ signer = options . algorithm
306
+ ? signer . setProtectedHeader ( { alg : options . algorithm } )
307
+ : signer ;
308
+ signer = signer . setIssuedAt ( ) ;
309
+ return await signer . sign ( pkcs8 ) ;
293
310
}
294
311
295
312
/**
0 commit comments