Skip to content

Commit

Permalink
feat: resolve bun compat issues
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Sep 22, 2023
1 parent 2d7af05 commit 1661d00
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
const jose = require('jose');
const crypto = require('crypto');
const JwksError = require('./errors/JwksError');

function resolveAlg(jwk) {
if (jwk.alg) {
return jwk.alg;
}

if (jwk.kty === 'RSA') {
return 'RS256';
}

if (jwk.kty === 'EC') {
switch (jwk.crv) {
case 'P-256':
return 'ES256';
case 'secp256k1':
return 'ES256K';
case 'P-384':
return 'ES384';
case 'P-521':
return 'ES512';
}
}

if (jwk.kty === 'OKP') {
switch (jwk.crv) {
case 'Ed25519':
case 'Ed448':
return 'EdDSA';
}
}

throw new JwksError('Unsupported JWK');
}

async function retrieveSigningKeys(jwks) {
const results = [];
Expand All @@ -13,7 +47,7 @@ async function retrieveSigningKeys(jwks) {
// The algorithm is actually not used in the Node.js KeyObject-based runtime
// passing an arbitrary value here and checking that KeyObject was returned
// later
const keyObject = await jose.importJWK(jwk, 'RS256');
const keyObject = await jose.importJWK(jwk, resolveAlg(jwk));
if (!(keyObject instanceof crypto.KeyObject) || keyObject.type !== 'public') {
continue;
}
Expand Down

0 comments on commit 1661d00

Please sign in to comment.