@authup/server-api
Version:
This is a standalone application.
49 lines • 1.73 kB
JavaScript
;
/*
* Copyright (c) 2022-2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.useKey = void 0;
const core_1 = require("@authup/core");
const server_core_1 = require("@authup/server-core");
const typeorm_extension_1 = require("typeorm-extension");
const entity_1 = require("../entity");
async function useKey(where) {
const dataSource = await (0, typeorm_extension_1.useDataSource)();
const repository = dataSource.getRepository(entity_1.KeyEntity);
let entity = await repository.findOne({
select: {
id: true,
type: true,
signature_algorithm: true,
encryption_key: true,
decryption_key: true,
},
where,
cache: 60.000,
});
if (!entity) {
const keyPair = await (0, server_core_1.createKeyPair)({
type: 'rsa',
});
entity = repository.create({
type: core_1.KeyType.RSA,
decryption_key: (0, core_1.unwrapPrivateKeyPem)(keyPair.privateKey),
encryption_key: (0, core_1.unwrapPublicKeyPem)(keyPair.publicKey),
realm_id: where.realm_id,
signature_algorithm: 'RS256',
});
await repository.save(entity);
}
else {
entity.decryption_key = (0, core_1.unwrapPrivateKeyPem)(entity.decryption_key);
entity.encryption_key = (0, core_1.unwrapPublicKeyPem)(entity.encryption_key);
await repository.save(entity);
}
return entity;
}
exports.useKey = useKey;
//# sourceMappingURL=singleton.js.map