UNPKG

@atproto/jwk

Version:

A library for working with JSON Web Keys (JWKs) in TypeScript. This is meant to be extended by environment-specific libraries like @atproto/jwk-jose.

92 lines 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.jwkAlgorithms = void 0; const errors_js_1 = require("./errors.js"); // Copy variable to prevent bundlers from automatically polyfilling "process" (e.g. parcel) const { process } = globalThis; const IS_NODE_RUNTIME = typeof process !== 'undefined' && typeof process?.versions?.node === 'string'; function* jwkAlgorithms(jwk) { // Ed25519, Ed448, and secp256k1 always have "alg" // OKP always has "use" if (jwk.alg) { yield jwk.alg; return; } switch (jwk.kty) { case 'EC': { if (jwk.use === 'enc' || jwk.use === undefined) { yield 'ECDH-ES'; yield 'ECDH-ES+A128KW'; yield 'ECDH-ES+A192KW'; yield 'ECDH-ES+A256KW'; } if (jwk.use === 'sig' || jwk.use === undefined) { const crv = 'crv' in jwk ? jwk.crv : undefined; switch (crv) { case 'P-256': case 'P-384': yield `ES${crv.slice(-3)}`; break; case 'P-521': yield 'ES512'; break; case 'secp256k1': if (IS_NODE_RUNTIME) yield 'ES256K'; break; default: throw new errors_js_1.JwkError(`Unsupported crv "${crv}"`); } } return; } case 'OKP': { if (!jwk.use) throw new errors_js_1.JwkError('Missing "use" Parameter value'); yield 'ECDH-ES'; yield 'ECDH-ES+A128KW'; yield 'ECDH-ES+A192KW'; yield 'ECDH-ES+A256KW'; return; } case 'RSA': { if (jwk.use === 'enc' || jwk.use === undefined) { yield 'RSA-OAEP'; yield 'RSA-OAEP-256'; yield 'RSA-OAEP-384'; yield 'RSA-OAEP-512'; if (IS_NODE_RUNTIME) yield 'RSA1_5'; } if (jwk.use === 'sig' || jwk.use === undefined) { yield 'PS256'; yield 'PS384'; yield 'PS512'; yield 'RS256'; yield 'RS384'; yield 'RS512'; } return; } case 'oct': { if (jwk.use === 'enc' || jwk.use === undefined) { yield 'A128GCMKW'; yield 'A192GCMKW'; yield 'A256GCMKW'; yield 'A128KW'; yield 'A192KW'; yield 'A256KW'; } if (jwk.use === 'sig' || jwk.use === undefined) { yield 'HS256'; yield 'HS384'; yield 'HS512'; } return; } default: throw new errors_js_1.JwkError(`Unsupported kty "${jwk.kty}"`); } } exports.jwkAlgorithms = jwkAlgorithms; //# sourceMappingURL=alg.js.map