UNPKG

@unvision/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

129 lines 6.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonWebEncryptionHeader = 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 aes_algorithm_1 = require("./algorithms/alg/aes.algorithm"); const dir_algorithm_1 = require("./algorithms/alg/dir.algorithm"); const gcm_algorithm_1 = require("./algorithms/alg/gcm.algorithm"); const rsa_algorithm_1 = require("./algorithms/alg/rsa.algorithm"); const cbc_algorithm_1 = require("./algorithms/enc/cbc.algorithm"); const gcm_algorithm_2 = require("./algorithms/enc/gcm.algorithm"); const def_algorithm_1 = require("./algorithms/zip/def.algorithm"); /** * Implementation of the JSON Web Encryption Header. * * @see https://www.rfc-editor.org/rfc/rfc7516.html#section-4 */ class JsonWebEncryptionHeader { /** * Instantiates a new JSON Web Encryption Header based on the provided Parameters. * * @param parameters JSON Web Encryption Header Parameters. */ constructor(parameters) { if (parameters instanceof JsonWebEncryptionHeader) { return parameters; } if (typeof parameters.alg !== 'string') { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid header parameter "alg".'); } if (typeof parameters.enc !== 'string') { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid header parameter "enc".'); } if (parameters.zip !== undefined && typeof parameters.zip !== 'string') { throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid header parameter "zip".'); } if (JsonWebEncryptionHeader.keyWrapAlgorithms[parameters.alg] === undefined) { throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Encryption Key Wrap Algorithm "${parameters.alg}".`); } if (JsonWebEncryptionHeader.contentEncryptionAlgorithms[parameters.enc] === undefined) { throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Encryption Content Encryption Algorithm "${parameters.enc}".`); } if (parameters.zip !== undefined) { if (JsonWebEncryptionHeader.compressionAlgorithms[parameters.zip] === undefined) { throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Encryption Compression Algorithm "${parameters.zip}".`); } } 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, 'keyWrapAlgorithm', { value: JsonWebEncryptionHeader.keyWrapAlgorithms[parameters.alg], }); Object.defineProperty(this, 'contentEncryptionAlgorithm', { value: JsonWebEncryptionHeader.contentEncryptionAlgorithms[parameters.enc], }); Object.defineProperty(this, 'compressionAlgorithm', { value: parameters.zip !== undefined ? JsonWebEncryptionHeader.compressionAlgorithms[parameters.zip] : null, }); Object.assign(this, parameters); } } exports.JsonWebEncryptionHeader = JsonWebEncryptionHeader; /** * Supported JSON Web Encryption Key Wrap Algorithms. */ JsonWebEncryptionHeader.keyWrapAlgorithms = { 'RSA-OAEP': rsa_algorithm_1.RSA_OAEP, 'RSA-OAEP-256': rsa_algorithm_1.RSA_OAEP_256, 'RSA-OAEP-384': rsa_algorithm_1.RSA_OAEP_384, 'RSA-OAEP-512': rsa_algorithm_1.RSA_OAEP_512, A128GCMKW: gcm_algorithm_1.A128GCMKW, A128KW: aes_algorithm_1.A128KW, A192GCMKW: gcm_algorithm_1.A192GCMKW, A192KW: aes_algorithm_1.A192KW, A256GCMKW: gcm_algorithm_1.A256GCMKW, A256KW: aes_algorithm_1.A256KW, dir: dir_algorithm_1.dir, RSA1_5: rsa_algorithm_1.RSA1_5, }; /** * Supported JSON Web Encryption Content Encryption Algorithms. */ JsonWebEncryptionHeader.contentEncryptionAlgorithms = { 'A128CBC-HS256': cbc_algorithm_1.A128CBC_HS256, 'A192CBC-HS384': cbc_algorithm_1.A192CBC_HS384, 'A256CBC-HS512': cbc_algorithm_1.A256CBC_HS512, A128GCM: gcm_algorithm_2.A128GCM, A192GCM: gcm_algorithm_2.A192GCM, A256GCM: gcm_algorithm_2.A256GCM, }; /** * Supported JSON Web Encryption Compression Algorithms. */ JsonWebEncryptionHeader.compressionAlgorithms = { DEF: def_algorithm_1.DEF, }; //# sourceMappingURL=jsonwebencryption.header.js.map