@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
79 lines • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const base64url_1 = require("base64url");
const JoseConstants_1 = require("./JoseConstants");
const CryptoProtocolError_1 = require("../CryptoProtocolError");
const KeyTypeFactory_1 = require("../../keys/KeyTypeFactory");
const CryptoHelpers_1 = require("../../utilities/CryptoHelpers");
const KeyUseFactory_1 = require("../../keys/KeyUseFactory");
/**
* Crypto helpers support for plugable crypto layer
*/
class JoseHelpers {
/**
* Return true if the header has elements
* @param header to test
*/
static headerHasElements(header) {
if (!header) {
return false;
}
if (header.length !== undefined) {
return header.length > 0;
}
return Object.keys(header).length > 0;
}
/**
* Encode the header to JSON and base 64 url.
* The Typescript Map construct does not allow for JSON.stringify returning {}.
* TSMap.toJSON prepares a map so it can be serialized as a dictionary.
* @param header to encode
* @param toBase64Url is true when result needs to be base 64 url
*/
static encodeHeader(header, toBase64Url = true) {
const serializedHeader = JSON.stringify(header.toJSON());
if (toBase64Url) {
return base64url_1.default.encode(serializedHeader);
}
return serializedHeader;
}
/**
* Get the Protected to be used from the options
* @param propertyName Property name in options
* @param [initialOptions] The initial set of options
* @param [overrideOptions] Options passed in after the constructure
* @param [mandatory] True if property is required
*/
static getOptionsProperty(propertyName, initialOptions, overrideOptions, mandatory = true) {
let overrideOption;
let initialOption;
if (overrideOptions) {
overrideOption = overrideOptions[propertyName];
}
if (initialOptions) {
initialOption = initialOptions[propertyName];
}
if (mandatory && !overrideOption && !initialOption) {
throw new CryptoProtocolError_1.default(JoseConstants_1.default.Jose, `The property '${propertyName}' is missing from options`);
}
return overrideOption || initialOption;
}
/**
* Create the key use according to the selected algorithm.
* @param algorithm JWA algorithm constant
*/
static createTypeViaJwa(algorithm) {
const alg = CryptoHelpers_1.default.jwaToWebCrypto(algorithm);
return KeyTypeFactory_1.default.createViaWebCrypto(alg);
}
/**
* Create the key use according to the selected algorithm.
* @param algorithm JWA algorithm constant
*/
static createUseViaJwa(algorithm) {
const alg = CryptoHelpers_1.default.jwaToWebCrypto(algorithm);
return KeyUseFactory_1.default.createViaWebCrypto(alg);
}
}
exports.default = JoseHelpers;
//# sourceMappingURL=JoseHelpers.js.map