UNPKG

jose

Version:

Universal 'JSON Web Almost Everything' - JWA, JWS, JWE, JWT, JWK with no dependencies

34 lines (33 loc) 1.38 kB
import { decode as base64url } from '../runtime/base64url.js'; import asKeyObject from '../runtime/jwk_to_key.js'; import { JOSENotSupported } from '../util/errors.js'; import isObject from '../lib/is_object.js'; export default async function parseJwk(jwk, alg, octAsKeyObject) { if (!isObject(jwk)) { throw new TypeError('JWK must be an object'); } alg || (alg = jwk.alg); if (typeof alg !== 'string' || !alg) { throw new TypeError('"alg" argument is required when "jwk.alg" is not present'); } switch (jwk.kty) { case 'oct': if (typeof jwk.k !== 'string' || !jwk.k) { throw new TypeError('missing "k" (Key Value) Parameter value'); } octAsKeyObject !== null && octAsKeyObject !== void 0 ? octAsKeyObject : (octAsKeyObject = jwk.ext !== true); if (octAsKeyObject) { return asKeyObject({ ...jwk, alg, ext: false }); } return base64url(jwk.k); case 'RSA': if (jwk.oth !== undefined) { throw new JOSENotSupported('RSA JWK "oth" (Other Primes Info) Parameter value is unsupported'); } case 'EC': case 'OKP': return asKeyObject({ ...jwk, alg }); default: throw new JOSENotSupported('unsupported "kty" (Key Type) Parameter value'); } }