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 838b37f
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions 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');

Check failure on line 2 in src/utils.js

View workflow job for this annotation

GitHub Actions / Lint Code

'crypto' is assigned a value but never used
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 15 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L14-L15

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

Check warning on line 17 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L17

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

Check warning on line 19 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L19

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

Check warning on line 21 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L21

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

Check warning on line 23 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L23

Added line #L23 was not covered by tests
}
}

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

Check warning on line 28 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L27-L28

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

Check warning on line 31 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L31

Added line #L31 was not covered by tests
}
}

throw new JwksError('Unsupported JWK');

Check warning on line 35 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L35

Added line #L35 was not covered by tests
}

async function retrieveSigningKeys(jwks) {
const results = [];
Expand All @@ -10,14 +44,24 @@ 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 'KeyObject': {
getSpki = () => key.export({ format: 'pem', type: 'spki' });
break;
}
case 'CryptoKey': {
const spki = await jose.exportSPKI(key);
getSpki = () => spki;
break;

Check warning on line 60 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L58-L60

Added lines #L58 - L60 were not covered by tests
}
default:
continue;

Check warning on line 63 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L63

Added line #L63 was not covered by tests
}
results.push({
get publicKey() { return getSpki(); },
get rsaPublicKey() { return getSpki(); },
Expand Down

0 comments on commit 838b37f

Please sign in to comment.