@authing/jose
Version:
Webpack v4 compatible version of panva/jose v3.x
76 lines (75 loc) • 2.59 kB
JavaScript
import FlattenedSign from '../flattened/sign.js';
import { JWSInvalid } from '../../util/errors.js';
const signatureRef = new WeakMap();
class IndividualSignature {
setProtectedHeader(protectedHeader) {
if (this._protectedHeader) {
throw new TypeError('setProtectedHeader can only be called once');
}
this._protectedHeader = protectedHeader;
return this;
}
setUnprotectedHeader(unprotectedHeader) {
if (this._unprotectedHeader) {
throw new TypeError('setUnprotectedHeader can only be called once');
}
this._unprotectedHeader = unprotectedHeader;
return this;
}
set _protectedHeader(value) {
signatureRef.get(this).protectedHeader = value;
}
get _protectedHeader() {
return signatureRef.get(this).protectedHeader;
}
set _unprotectedHeader(value) {
signatureRef.get(this).unprotectedHeader = value;
}
get _unprotectedHeader() {
return signatureRef.get(this).unprotectedHeader;
}
}
class GeneralSign {
constructor(payload) {
this._signatures = [];
this._payload = payload;
}
addSignature(key, options) {
const signature = new IndividualSignature();
signatureRef.set(signature, { key, options });
this._signatures.push(signature);
return signature;
}
async sign() {
if (!this._signatures.length) {
throw new JWSInvalid('at least one signature must be added');
}
const jws = {
signatures: [],
};
await Promise.all(this._signatures.map(async (sig, i) => {
const { protectedHeader, unprotectedHeader, options, key } = signatureRef.get(sig);
const flattened = new FlattenedSign(this._payload);
if (protectedHeader) {
flattened.setProtectedHeader(protectedHeader);
}
if (unprotectedHeader) {
flattened.setUnprotectedHeader(unprotectedHeader);
}
const { payload, ...rest } = await flattened.sign(key, options);
if ('payload' in jws && jws.payload !== payload) {
throw new JWSInvalid(`index ${i} signature produced a different payload`);
}
else {
jws.payload = payload;
}
jws.signatures.push(rest);
}));
if ('payload' in jws && jws.payload === undefined) {
delete jws.payload;
}
return jws;
}
}
export { GeneralSign };
export default GeneralSign;