@guarani/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
93 lines (92 loc) • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebEncryptionHeader = void 0;
const objects_1 = require("@guarani/objects");
const invalid_jose_header_exception_1 = require("../exceptions/invalid-jose-header.exception");
const unsupported_algorithm_exception_1 = require("../exceptions/unsupported-algorithm.exception");
const jsonwebencryption_keywrap_algorithms_registry_1 = require("./algorithms/alg/jsonwebencryption-keywrap-algorithms-registry");
const jsonwebencryption_contentencryption_algorithms_registry_1 = require("./algorithms/enc/jsonwebencryption-contentencryption-algorithms-registry");
const jsonwebencryption_compression_algorithms_registry_1 = require("./algorithms/zip/jsonwebencryption-compression-algorithms-registry");
/**
* Implementation of {@link https://www.rfc-editor.org/rfc/rfc7516.html#section-4 RFC 7516 Section 4}.
*/
class JsonWebEncryptionHeader {
/**
* Instantiates a JSON Web Encryption Header for Compact Serialization.
*
* @param params Parameters of the JSON Web Encryption Header.
*/
constructor(params) {
if (params instanceof JsonWebEncryptionHeader) {
return params;
}
if (typeof params.alg !== 'string') {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid parameter "alg".');
}
if (typeof params.enc !== 'string') {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid parameter "enc".');
}
if (params.zip !== undefined && typeof params.zip !== 'string') {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid parameter "zip".');
}
if (jsonwebencryption_keywrap_algorithms_registry_1.JSON_WEB_ENCRYPTION_KEY_WRAP_ALGORITHMS_REGISTRY[params.alg] === undefined) {
throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Encryption Key Wrap Algorithm "${params.alg}".`);
}
if (jsonwebencryption_contentencryption_algorithms_registry_1.JSON_WEB_ENCRYPTION_CONTENT_ENCRYPTION_ALGORITHMS_REGISTRY[params.enc] === undefined) {
throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Encryption Content Encryption Algorithm "${params.alg}".`);
}
if (params.zip !== undefined) {
if (jsonwebencryption_compression_algorithms_registry_1.JSON_WEB_ENCRYPTION_COMPRESSION_ALGORITHMS_REGISTRY[params.zip] === undefined) {
throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Encryption Compression Algorithm "${params.zip}".`);
}
}
if (params.jku !== undefined) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported parameter "jku".');
}
if (params.jwk !== undefined) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported parameter "jwk".');
}
if (params.kid !== undefined && typeof params.kid !== 'string') {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid parameter "kid".');
}
if (params.x5u !== undefined) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported parameter "x5u".');
}
if (params.x5c !== undefined) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported parameter "x5c".');
}
if (params.x5t !== undefined) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported parameter "x5t".');
}
if (params['x5t#S256'] !== undefined) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Unsupported parameter "x5t#S256".');
}
if (params.crit !== undefined) {
if (!Array.isArray(params.crit) || params.crit.length === 0) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid parameter "crit".');
}
if (params.crit.some((criticalParam) => typeof criticalParam !== 'string' || criticalParam.length === 0)) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid parameter "crit".');
}
params.crit.forEach((criticalParam) => {
if (params[criticalParam] === undefined) {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException(`Missing required parameter "${criticalParam}".`);
}
});
}
Object.assign(this, (0, objects_1.removeNullishValues)(params));
}
/**
* Checks if the provided object conforms to the JSON Web Encryption Header Specification.
*
* @param data Object to be inspected.
*/
static isJsonWebEncryptionHeader(data) {
const algs = Object.keys(jsonwebencryption_keywrap_algorithms_registry_1.JSON_WEB_ENCRYPTION_KEY_WRAP_ALGORITHMS_REGISTRY);
const encs = Object.keys(jsonwebencryption_contentencryption_algorithms_registry_1.JSON_WEB_ENCRYPTION_CONTENT_ENCRYPTION_ALGORITHMS_REGISTRY);
const hasAlg = algs.includes(data.alg);
const hasEnc = encs.includes(data.enc);
return hasAlg && hasEnc;
}
}
exports.JsonWebEncryptionHeader = JsonWebEncryptionHeader;