Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resolve bun/deno compat issues #374

Merged
merged 1 commit into from
Oct 5, 2023
Merged
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
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) {

Check warning on line 14 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L13-L14

Added lines #L13 - L14 were not covered by tests
case 'P-256':
return 'ES256';

Check warning on line 16 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L16

Added line #L16 was not covered by tests
case 'secp256k1':
return 'ES256K';

Check warning on line 18 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L18

Added line #L18 was not covered by tests
case 'P-384':
return 'ES384';

Check warning on line 20 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L20

Added line #L20 was not covered by tests
case 'P-521':
return 'ES512';

Check warning on line 22 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L22

Added line #L22 was not covered by tests
}
}

if (jwk.kty === 'OKP') {
switch (jwk.crv) {

Check warning on line 27 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L26-L27

Added lines #L26 - L27 were not covered by tests
case 'Ed25519':
case 'Ed448':
return 'EdDSA';

Check warning on line 30 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L30

Added line #L30 was not covered by tests
}
}

throw new JwksError('Unsupported JWK');

Check warning on line 34 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L34

Added line #L34 was not covered by tests
}

async function retrieveSigningKeys(jwks) {
const results = [];
Expand All @@ -10,14 +43,23 @@

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;

Check warning on line 55 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L53-L55

Added lines #L53 - L55 were not covered by tests
}
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