cose-kit
Version:
**DEPRECATED:** Use [@auth0/cose](https://www.npmjs.com/package/@auth0/cose).
73 lines (72 loc) • 3.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sign1 = void 0;
const headers_js_1 = require("../headers.js");
const sign_js_1 = __importDefault(require("#runtime/sign.js"));
const SignatureBase_js_1 = require("./SignatureBase.js");
const cbor_js_1 = require("../cbor.js");
const decode_js_1 = require("./decode.js");
class Sign1 extends SignatureBase_js_1.SignatureBase {
constructor(protectedHeaders, unprotectedHeaders, payload, signature) {
super(protectedHeaders, unprotectedHeaders, signature);
this.payload = payload;
}
getContentForEncoding() {
return [
this.encodedProtectedHeaders,
this.unprotectedHeaders,
this.payload,
this.signature,
];
}
static Signature1(protectedHeaders, applicationHeaders, payload) {
return cbor_js_1.encoder.encode([
'Signature1',
protectedHeaders,
applicationHeaders,
payload,
]);
}
async verify(key, options) {
var _a, _b;
const toBeSigned = Sign1.Signature1(this.encodedProtectedHeaders || new Uint8Array(), (_a = options === null || options === void 0 ? void 0 : options.externalAAD) !== null && _a !== void 0 ? _a : new Uint8Array(), (_b = options === null || options === void 0 ? void 0 : options.detachedPayload) !== null && _b !== void 0 ? _b : this.payload);
await this.internalVerify(toBeSigned, key, options);
}
async verifyX509(roots) {
const { publicKey } = await this.verifyX509Chain(roots);
return this.verify(publicKey);
}
static async sign(protectedHeaders, unprotectedHeaders, payload, key) {
const wProtectedHeaders = headers_js_1.ProtectedHeaders.wrap(protectedHeaders);
if (!wProtectedHeaders.has(headers_js_1.Headers.Algorithm)) {
throw new Error('The alg header must be set.');
}
const alg = headers_js_1.AlgorithmNames.get(wProtectedHeaders.get(headers_js_1.Headers.Algorithm));
const encodedProtectedHeaders = cbor_js_1.encoder.encode(wProtectedHeaders.esMap);
const unprotectedHeadersMap = headers_js_1.UnprotectedHeaders.wrap(unprotectedHeaders).esMap;
const toBeSigned = Sign1.Signature1(encodedProtectedHeaders, new Uint8Array(), payload);
if (!alg) {
throw new Error('The alg header must be set.');
}
const signature = await (0, sign_js_1.default)(alg, key, toBeSigned);
return new Sign1(encodedProtectedHeaders, unprotectedHeadersMap, payload, signature);
}
static decode(cose) {
return (0, decode_js_1.decode)(cose, Sign1);
}
}
exports.Sign1 = Sign1;
Sign1.tag = 18;
(0, cbor_js_1.addExtension)({
Class: Sign1,
tag: Sign1.tag,
encode(instance, encodeFn) {
return encodeFn(instance.getContentForEncoding());
},
decode: (data) => {
return new Sign1(data[0], data[1], data[2], data[3]);
}
});