@sphereon/ssi-sdk-ext.jwt-service
Version:
137 lines • 7.02 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JwtService = void 0;
const debug_1 = __importDefault(require("debug"));
const jose_1 = require("jose");
const u8a = __importStar(require("uint8arrays"));
const __1 = require("..");
const JWE_1 = require("../functions/JWE");
/**
* @public
*/
class JwtService {
constructor() {
this.schema = __1.schema.IJwtService;
this.methods = {
jwtPrepareJws: this.jwtPrepareJws.bind(this),
jwtCreateJwsJsonGeneralSignature: this.jwtCreateJwsJsonGeneralSignature.bind(this),
jwtCreateJwsJsonFlattenedSignature: this.jwtCreateJwsJsonFlattenedSignature.bind(this),
jwtCreateJwsCompactSignature: this.jwtCreateJwsCompactSignature.bind(this),
jwtVerifyJwsSignature: this.jwtVerifyJwsSignature.bind(this),
jwtEncryptJweCompactJwt: this.jwtEncryptJweCompactJwt.bind(this),
jwtDecryptJweCompactJwt: this.jwtDecryptJweCompactJwt.bind(this),
};
}
jwtPrepareJws(args, context) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, __1.prepareJwsObject)(args, context);
});
}
jwtCreateJwsJsonGeneralSignature(args, context) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, __1.createJwsJsonGeneral)(args, context);
});
}
jwtCreateJwsJsonFlattenedSignature(args, context) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, __1.createJwsJsonFlattened)(args, context);
});
}
jwtCreateJwsCompactSignature(args, context) {
return __awaiter(this, void 0, void 0, function* () {
// We wrap it in a json object for remote REST calls
return { jwt: yield (0, __1.createJwsCompact)(args, context) };
});
}
jwtVerifyJwsSignature(args, context) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, __1.verifyJws)(args, context);
});
}
jwtEncryptJweCompactJwt(args, context) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g;
const { payload, protectedHeader = { alg: args.alg, enc: args.enc }, recipientKey, issuer, expirationTime, audience } = args;
try {
(0, debug_1.default)(`JWE Encrypt: ${JSON.stringify(args, null, 2)}`);
const alg = (_b = (_a = (0, __1.jweAlg)(args.alg)) !== null && _a !== void 0 ? _a : (0, __1.jweAlg)(protectedHeader.alg)) !== null && _b !== void 0 ? _b : 'ECDH-ES';
const enc = (_d = (_c = (0, __1.jweEnc)(args.enc)) !== null && _c !== void 0 ? _c : (0, __1.jweEnc)(protectedHeader.enc)) !== null && _d !== void 0 ? _d : 'A256GCM';
const encJwks = recipientKey.jwks.length === 1
? [recipientKey.jwks[0]]
: recipientKey.jwks.filter((jwk) => (jwk.kid && (jwk.kid === jwk.jwk.kid || jwk.kid === jwk.jwkThumbprint)) || jwk.jwk.use === 'enc');
if (encJwks.length === 0) {
return Promise.reject(Error(`No public JWK found that can be used to encrypt against`));
}
const jwkInfo = encJwks[0];
if (encJwks.length > 0) {
__1.JwtLogger.warning(`More than one JWK with 'enc' usage found. Selected the first one as no 'kid' was provided`, encJwks);
}
if (((_e = jwkInfo.jwk.kty) === null || _e === void 0 ? void 0 : _e.startsWith('EC')) !== true || !alg.startsWith('ECDH')) {
return Promise.reject(Error(`Currently only ECDH-ES is supported for encryption. JWK alg ${jwkInfo.jwk.kty}, header alg ${alg}`)); // TODO: Probably we support way more already
}
const apuVal = (_f = protectedHeader.apu) !== null && _f !== void 0 ? _f : args.apu;
const apu = apuVal ? u8a.fromString(apuVal, 'base64url') : undefined;
const apvVal = (_g = protectedHeader.apv) !== null && _g !== void 0 ? _g : args.apv;
const apv = apvVal ? u8a.fromString(apvVal, 'base64url') : undefined;
const pubKey = yield (0, jose_1.importJWK)(jwkInfo.jwk);
const encrypter = new JWE_1.CompactJwtEncrypter({
enc,
alg,
keyManagementParams: { apu, apv },
key: pubKey,
issuer,
expirationTime,
audience,
});
const jwe = yield encrypter.encryptCompactJWT(payload, {});
return { jwt: jwe };
}
catch (error) {
console.error(`Error encrypting JWE: ${error.message}`, error);
throw error;
}
});
}
jwtDecryptJweCompactJwt(args, context) {
return __awaiter(this, void 0, void 0, function* () {
return { jwt: 'FIXME' };
});
}
}
exports.JwtService = JwtService;
//# sourceMappingURL=JwtService.js.map