Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"module": {
"type": "es6"
},
"sourceMaps": true,
"sourceMaps": false,
"exclude": [
"tests",
".*.test.ts$",
Expand Down
1 change: 0 additions & 1 deletion package/esm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ declare const jwtGenCore: {
token: typeof token;
};
export default jwtGenCore;
//# sourceMappingURL=index.d.ts.map
1 change: 0 additions & 1 deletion package/esm/index.d.ts.map

This file was deleted.

25 changes: 18 additions & 7 deletions package/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,28 @@ const payload = (issuerId, duration)=>({
* @param duration
* @returns
*/ export async function token(privateKey, issuerId, privateKeyId, duration = 500) {
const key = await importPKCS8(privateKey.toString(), 'ES256');
return new SignJWT(payload(issuerId, duration)).setProtectedHeader({
alg: 'ES256',
kid: privateKeyId
}).sign(key);
try {
const key = await importPKCS8(privateKey.toString(), 'ES256');
return new SignJWT(payload(issuerId, duration)).setProtectedHeader({
alg: 'ES256',
kid: privateKeyId
}).sign(key);
} catch (error) {
if (error instanceof Error) {
// Use predefined error messages to avoid information leakage
if (error.message.includes('PKCS8')) {
throw new Error('JWT token generation failed: Invalid key format');
} else if (error.message.includes('sign')) {
throw new Error('JWT token generation failed: Signing operation failed');
}
throw new Error('JWT token generation failed: Internal error');
}
throw new Error('JWT token generation failed: Unknown error occurred');
}
}
const jwtGenCore = {
tokenSync,
token
};
export default jwtGenCore;


//# sourceMappingURL=index.js.map
1 change: 0 additions & 1 deletion package/esm/index.js.map

This file was deleted.

21 changes: 17 additions & 4 deletions package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,23 @@ export async function token(
privateKeyId: string,
duration = 500,
): Promise<string> {
const key = await importPKCS8(privateKey.toString(), 'ES256');
return new SignJWT(payload(issuerId, duration))
.setProtectedHeader({ alg: 'ES256', kid: privateKeyId })
.sign(key);
try {
const key = await importPKCS8(privateKey.toString(), 'ES256');
return new SignJWT(payload(issuerId, duration))
.setProtectedHeader({ alg: 'ES256', kid: privateKeyId })
.sign(key);
} catch (error) {
if (error instanceof Error) {
// Use predefined error messages to avoid information leakage
if (error.message.includes('PKCS8')) {
throw new Error('JWT token generation failed: Invalid key format');
} else if (error.message.includes('sign')) {
throw new Error('JWT token generation failed: Signing operation failed');
}
throw new Error('JWT token generation failed: Internal error');
}
throw new Error('JWT token generation failed: Unknown error occurred');
}
};

const jwtGenCore = {
Expand Down
4 changes: 2 additions & 2 deletions package/tsconfig-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"outDir": "./@types",
"moduleResolution": "bundler",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"declarationMap": false,
"sourceMap": false,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
Expand Down
4 changes: 2 additions & 2 deletions package/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"moduleResolution": "bundler",
"declaration": true,
"declarationDir": "./esm",
"declarationMap": true,
"sourceMap": true,
"declarationMap": false,
"sourceMap": false,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
Expand Down