Skip to content

Commit

Permalink
fix resolve bun/deno compat issues (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath authored Oct 5, 2023
2 parents 2d7af05 + 5e9f786 commit c14d857
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
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 @@ -10,14 +43,23 @@ async function retrieveSigningKeys(jwks) {

for (const jwk of jwks) {
try {
// 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');
if (!(keyObject instanceof crypto.KeyObject) || keyObject.type !== 'public') {
const key = await jose.importJWK(jwk, resolveAlg(jwk));
if (key.type !== 'public') {
continue;
}
const getSpki = () => keyObject.export({ format: 'pem', type: 'spki' });
let getSpki;
switch (key[Symbol.toStringTag]) {
case 'CryptoKey': {
const spki = await jose.exportSPKI(key);
getSpki = () => spki;
break;
}
case 'KeyObject':
// Assume legacy Node.js version without the Symbol.toStringTag backported
// Fall through
default:
getSpki = () => key.export({ format: 'pem', type: 'spki' });
}
results.push({
get publicKey() { return getSpki(); },
get rsaPublicKey() { return getSpki(); },
Expand Down

0 comments on commit c14d857

Please sign in to comment.