@authup/server-api
Version:
This is a standalone application.
81 lines • 2.47 kB
JavaScript
;
/*
* Copyright (c) 2023.
* 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.findRobotCredentialsInVault = exports.removeRobotCredentialsFromVault = exports.saveRobotCredentialsToVault = exports.createRobotVaultEngine = void 0;
const vault_1 = require("@hapic/vault");
async function createRobotVaultEngine() {
if (!(0, vault_1.hasClient)()) {
return;
}
const client = (0, vault_1.useClient)();
await client.mount.create('robots', {
type: 'kv',
options: {
version: 1,
},
});
}
exports.createRobotVaultEngine = createRobotVaultEngine;
async function saveRobotCredentialsToVault(entity) {
if (!(0, vault_1.hasClient)()) {
return;
}
const client = (0, vault_1.useClient)();
try {
await client.keyValueV1.create('robots', entity.name, {
id: entity.id,
secret: entity.secret,
});
}
catch (e) {
if ((0, vault_1.isClientErrorWithStatusCode)(e, 404)) {
await createRobotVaultEngine();
await saveRobotCredentialsToVault(entity);
return;
}
throw e;
}
}
exports.saveRobotCredentialsToVault = saveRobotCredentialsToVault;
async function removeRobotCredentialsFromVault(entity) {
if (!(0, vault_1.hasClient)()) {
return;
}
const client = (0, vault_1.useClient)();
try {
await client.keyValueV1.delete('robots', entity.name);
}
catch (e) {
if ((0, vault_1.isClientErrorWithStatusCode)(e, 404)) {
return;
}
throw e;
}
}
exports.removeRobotCredentialsFromVault = removeRobotCredentialsFromVault;
async function findRobotCredentialsInVault(entity) {
if (!(0, vault_1.hasClient)()) {
return undefined;
}
const client = (0, vault_1.useClient)();
try {
const response = await client.keyValueV1.getOne('robots', entity.name);
if (response && response.data) {
return response.data;
}
return undefined;
}
catch (e) {
if ((0, vault_1.isClientErrorWithStatusCode)(e, 404)) {
return undefined;
}
throw e;
}
}
exports.findRobotCredentialsInVault = findRobotCredentialsInVault;
//# sourceMappingURL=vault.js.map