Locked certificates under a symbol with a setter to validate them first

This commit is contained in:
Alexander Cerutti
2021-10-03 18:20:51 +02:00
parent e0d2db9fc4
commit 40f2b0ea90

View File

@@ -18,9 +18,10 @@ const importMetadataSymbol = Symbol("import.pass.metadata");
const createManifestSymbol = Symbol("pass.manifest");
const closePassSymbol = Symbol("pass.close");
const passTypeSymbol = Symbol("pass.type");
const certificatesSymbol = Symbol("pass.certificates");
export default class PKPass extends Bundle {
private certificates: Schemas.CertificatesSchema;
private [certificatesSymbol]: Schemas.CertificatesSchema;
private [fieldKeysPoolSymbol] = new Set<string>();
private [propsSymbol]: Schemas.PassProps = {};
private [localizationSymbol]: {
@@ -56,7 +57,7 @@ export default class PKPass extends Bundle {
if (source instanceof PKPass) {
/** Cloning is happening here */
certificates = source.certificates;
certificates = source[certificatesSymbol];
buffers = {};
const buffersEntries = Object.entries(source[filesSymbol]);
@@ -167,10 +168,26 @@ export default class PKPass extends Bundle {
);
Object.assign(this[propsSymbol], overridesValidation);
this.certificates = certificates;
}
/**
* Allows changing the certificates, if needed.
* They are actually expected to be received in
* the constructor, but they can get overridden
* here for whatever purpose.
*
* @param certs
*/
public set certificates(certs: Schemas.CertificatesSchema) {
if (!Schemas.isValid(certs, Schemas.CertificatesSchema)) {
throw new TypeError("Cannot set certificates: invalid");
}
this[certificatesSymbol] = certs;
}
/**
* Allows getting an image of the props
* that are composing your pass instance.
@@ -585,7 +602,7 @@ export default class PKPass extends Bundle {
const signatureBuffer = Signature.create(
manifestBuffer,
this.certificates,
this[certificatesSymbol],
);
super.addBuffer("signature", signatureBuffer);
}