@unvision/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
88 lines • 3.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebKeySet = void 0;
const invalid_jsonwebkeyset_exception_1 = require("../exceptions/invalid-jsonwebkeyset.exception");
const jsonwebkey_1 = require("../jwk/jsonwebkey");
/**
* Implementation of a JSON Web Key Set.
*
* @see https://www.rfc-editor.org/rfc/rfc7517.html#section-5
*/
class JsonWebKeySet {
/**
* Instantiates a new JSON Web Key Set based on the provided JSON Web Keys.
*
* @param keys JSON Web Keys to be registered at the JSON Web Key Set.
*/
constructor(keys) {
if (!Array.isArray(keys) || keys.length === 0 || keys.some((key) => !(key instanceof jsonwebkey_1.JsonWebKey))) {
throw new TypeError('Invalid parameter "keys".');
}
const identifiers = keys.map((key) => key.kid);
identifiers.forEach((identifier, index) => {
if (identifier === undefined) {
throw new invalid_jsonwebkeyset_exception_1.InvalidJsonWebKeySetException(`The JSON Web Key at position #${index} does not have an Identifier.`);
}
});
if (new Set(identifiers).size !== identifiers.length) {
throw new invalid_jsonwebkeyset_exception_1.InvalidJsonWebKeySetException('The use of duplicate Key Identifiers is forbidden.');
}
this.keys = keys;
}
/**
* Loads the provided Parameters into a JSON Web Key Set.
*
* @param parameters Parameters of the JSON Web Key Set.
* @returns JSON Web Key Set based on the provided Parameters.
*/
static load(parameters) {
if (typeof parameters !== 'object' || parameters === null) {
throw new invalid_jsonwebkeyset_exception_1.InvalidJsonWebKeySetException();
}
if (!Array.isArray(parameters.keys) || parameters.keys.length === 0) {
throw new invalid_jsonwebkeyset_exception_1.InvalidJsonWebKeySetException('Invalid JSON Web Key Set parameter "keys".');
}
const keys = parameters.keys.map((keyParameters, index) => {
try {
return new jsonwebkey_1.JsonWebKey(keyParameters);
}
catch (exc) {
throw new invalid_jsonwebkeyset_exception_1.InvalidJsonWebKeySetException(`The item at position #${index} is not a valid JSON Web Key.`, exc);
}
});
return new JsonWebKeySet(keys);
}
/**
* Parses a JSON String into a JSON Web Key Set.
*
* @param data JSON String representation of the JSON Web Key Set to be parsed.
* @returns Instance of a JSON Web Key Set based on the provided JSON String.
*/
static parse(data) {
try {
return this.load(JSON.parse(data));
}
catch (exc) {
throw new invalid_jsonwebkeyset_exception_1.InvalidJsonWebKeySetException(null, exc);
}
}
/**
* Finds and returns a JSON Web Key that satisfies the provided predicate.
*
* @param predicate Predicate used to locate the requested JSON Web Key.
* @returns JSON Web Key that satisfies the provided predicate.
*/
find(predicate) {
return this.keys.find(predicate) ?? null;
}
/**
* Returns the Parameters of the JSON Web Key Set.
*
* @param exportPublic Exports only the Public Parameters of the JSON Web Keys.
*/
toJSON(exportPublic = true) {
return { keys: this.keys.map((key) => key.toJSON(exportPublic)) };
}
}
exports.JsonWebKeySet = JsonWebKeySet;
//# sourceMappingURL=jsonwebkeyset.js.map