ts-jose
Version:
Wrap functions of JOSE in steady interface
55 lines • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JWS = void 0;
const jose_1 = require("jose");
const error_js_1 = require("./error.cjs");
class JWS {
static async verify(signature, jwk, options) {
const key = await this.getKeyFrom(signature, jwk, options);
const result = (await (typeof key === 'function'
? (0, jose_1.compactVerify)(signature, key, options)
: (0, jose_1.compactVerify)(signature, key, options)));
if (options?.typ !== undefined &&
result.protectedHeader.typ !== options.typ) {
throw new error_js_1.JoseError('JWS', 'typ', options.typ);
}
return options?.complete
? {
payload: Buffer.from(result.payload).toString(),
header: result.protectedHeader,
key: result.key,
}
: Buffer.from(result.payload).toString();
}
static async sign(data, key, options) {
const jwk = key.getKey({
kid: options?.kid,
use: 'sig',
alg: options?.alg,
});
const encoder = new TextEncoder();
const jws = new jose_1.CompactSign(encoder.encode(data));
jws.setProtectedHeader({
typ: options?.typ,
kid: options?.kid ?? jwk.kid,
alg: options?.alg ?? jwk.alg ?? '',
jwk: options?.jwk ? jwk.toObject() : undefined,
});
return jws.sign(jwk.key);
}
static async getKeyFrom(signature, key, options) {
if (key === undefined)
return jose_1.EmbeddedJWK;
const header = (0, jose_1.decodeProtectedHeader)(signature);
const publicKey = await key
.getKey({
use: 'sig',
kid: options?.kid ? options.kid : header.kid,
alg: header.alg,
})
.toPublic();
return publicKey.key;
}
}
exports.JWS = JWS;
//# sourceMappingURL=jws.js.map