@guarani/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
81 lines (80 loc) • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebSignatureHeader = 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 jsonwebsignature_algorithms_registry_1 = require("./algorithms/jsonwebsignature-algorithms-registry");
/**
* Implementation of {@link https://www.rfc-editor.org/rfc/rfc7515.html#section-4 RFC 7515 Section 4}.
*/
class JsonWebSignatureHeader {
/**
* Instantiates a JSON Web Signature Header for Compact Serialization.
*
* @param params Parameters of the JSON Web Signature Header.
*/
constructor(params) {
if (params instanceof JsonWebSignatureHeader) {
return params;
}
if (typeof params.alg !== 'string') {
throw new invalid_jose_header_exception_1.InvalidJoseHeaderException('Invalid parameter "alg".');
}
if (jsonwebsignature_algorithms_registry_1.JSON_WEB_SIGNATURE_ALGORITHMS_REGISTRY[params.alg] === undefined) {
throw new unsupported_algorithm_exception_1.UnsupportedAlgorithmException(`Unsupported JSON Web Signature Algorithm "${params.alg}".`);
}
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 Signature Header Specification.
*
* @param data Object to be inspected.
*/
static isJsonWebSignatureHeader(data) {
const algs = Object.keys(jsonwebsignature_algorithms_registry_1.JSON_WEB_SIGNATURE_ALGORITHMS_REGISTRY);
const hasAlg = algs.includes(data.alg);
return hasAlg;
}
/**
* Returns the JSON Encoded String representation of the JSON Web Signature Header.
*/
toString() {
return JSON.stringify(this);
}
}
exports.JsonWebSignatureHeader = JsonWebSignatureHeader;