Skip to content

Commit 2ae8658

Browse files
feat: Replace jsonwebtoken with jose (box/box-codegen#444) (#85)
1 parent fb6c4f7 commit 2ae8658

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "a8b56d7", "specHash": "b2f7568", "version": "0.5.0" }
1+
{ "engineHash": "c714d1b", "specHash": "b2f7568", "version": "0.5.0" }

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"form-data": "^4.0.0",
3030
"node-fetch": "^2.6.3",
3131
"buffer": "^6.0.3",
32-
"jsonwebtoken": "^9.0.0",
32+
"jose": "^5.2.2",
3333
"tslib": "^2.6.2",
3434
"uuid": "^9.0.0"
3535
},
@@ -38,7 +38,6 @@
3838
"@rollup/plugin-node-resolve": "^15.2.3",
3939
"@rollup/plugin-typescript": "^11.1.5",
4040
"@types/jest": "^29.5.1",
41-
"@types/jsonwebtoken": "^9.0.1",
4241
"@types/node": "*",
4342
"@types/node-fetch": "2.6.3",
4443
"@types/uuid": "^9.0.1",

src/box/jwtAuth.generated.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ export class BoxJwtAuth implements Authentication {
159159
key: this.config.privateKey,
160160
passphrase: this.config.privateKeyPassphrase,
161161
} satisfies JwtKey;
162-
const assertion: string = createJwtAssertion(claims, jwtKey, jwtOptions);
162+
const assertion: string = await createJwtAssertion(
163+
claims,
164+
jwtKey,
165+
jwtOptions
166+
);
163167
const authManager: AuthorizationManager = !(networkSession == void 0)
164168
? new AuthorizationManager({ networkSession: networkSession })
165169
: new AuthorizationManager({});

src/internal/utils.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Buffer } from 'buffer';
22
import type { Readable } from 'stream';
33
import { v4 as uuidv4 } from 'uuid';
4+
import { SignJWT, importPKCS8 } from 'jose';
45

56
export function isBrowser() {
67
return (
@@ -266,11 +267,6 @@ export type JwtSignOptions = {
266267
subject?: string | undefined;
267268
issuer?: string | undefined;
268269
jwtid?: string | undefined;
269-
mutatePayload?: boolean | undefined;
270-
noTimestamp?: boolean | undefined;
271-
encoding?: string | undefined;
272-
allowInsecureKeySizes?: boolean | undefined;
273-
allowInvalidAsymmetricKeyTypes?: boolean | undefined;
274270
};
275271

276272
/**
@@ -281,15 +277,36 @@ export type JwtSignOptions = {
281277
* @param options
282278
* @returns
283279
*/
284-
export function createJwtAssertion(
280+
export async function createJwtAssertion(
285281
claims: {
286282
readonly [key: string]: any;
287283
},
288284
key: JwtKey,
289285
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);
293310
}
294311

295312
/**

0 commit comments

Comments
 (0)