UNPKG

@unvision/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

88 lines 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonWebSignatureHeader = void 0; const invalid_jose_header_exception_1 = require("../exceptions/invalid-jose-header.exception"); const unsupported_algorithm_exception_1 = require("../exceptions/unsupported-algorithm.exception"); const ecdsa_algorithm_1 = require("./algorithms/ecdsa.algorithm"); const hmac_algorithm_1 = require("./algorithms/hmac.algorithm"); const none_algorithm_1 = require("./algorithms/none.algorithm"); const rsassa_algorithm_1 = require("./algorithms/rsassa.algorithm"); /** * Implementation of the JSON Web Signature Header. * * @see https://www.rfc-editor.org/rfc/rfc7515.html#section-4 */ class JsonWebSignatureHeader { /** * Instantiates a new JSON Web Signature Header based on the provided Parameters. * * @param parameters JSON Web Signature Header Parameters. */ constructor(parameters) { if (parameters instanceof JsonWebSignatureHeader) { return parameters; } if (typeof parameters.alg !== 'string') { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid header parameter "alg".'); } if (JsonWebSignatureHeader.algorithms[parameters.alg] === undefined) { throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Signature Algorithm "${parameters.alg}".`); } if (parameters.jku !== undefined) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported header parameter "jku".'); } if (parameters.jwk !== undefined) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported header parameter "jwk".'); } if (parameters.kid !== undefined && typeof parameters.kid !== 'string') { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid header parameter "kid".'); } if (parameters.x5u !== undefined) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported header parameter "x5u".'); } if (parameters.x5c !== undefined) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported header parameter "x5c".'); } if (parameters.x5t !== undefined) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported header parameter "x5t".'); } if (parameters['x5t#S256'] !== undefined) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported header parameter "x5t#S256".'); } if (parameters.crit !== undefined) { if (!Array.isArray(parameters.crit) || parameters.crit.length === 0) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid header parameter "crit".'); } if (parameters.crit.some((criticalParam) => typeof criticalParam !== 'string' || criticalParam.length === 0)) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid header parameter "crit".'); } parameters.crit.forEach((criticalParam) => { if (parameters[criticalParam] === undefined) { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException(`Missing required header parameter "${criticalParam}".`); } }); } Object.defineProperty(this, 'algorithm', { value: JsonWebSignatureHeader.algorithms[parameters.alg] }); Object.assign(this, parameters); } } exports.JsonWebSignatureHeader = JsonWebSignatureHeader; /** * Supported JSON Web Signature Algorithms. */ JsonWebSignatureHeader.algorithms = { ES256: ecdsa_algorithm_1.ES256, ES384: ecdsa_algorithm_1.ES384, ES512: ecdsa_algorithm_1.ES512, HS256: hmac_algorithm_1.HS256, HS384: hmac_algorithm_1.HS384, HS512: hmac_algorithm_1.HS512, none: none_algorithm_1.none, PS256: rsassa_algorithm_1.PS256, PS384: rsassa_algorithm_1.PS384, PS512: rsassa_algorithm_1.PS512, RS256: rsassa_algorithm_1.RS256, RS384: rsassa_algorithm_1.RS384, RS512: rsassa_algorithm_1.RS512, }; //# sourceMappingURL=jsonwebsignature.header.js.map