@sap-ux/store
Version:
NPM module for storing persistent data
141 lines • 4.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyStoreManager = void 0;
const utils_1 = require("../utils");
class KeyStoreManager {
log;
keyring;
constructor(log, zoweSecretSdk) {
this.log = log;
this.keyring = zoweSecretSdk;
}
/**
* Helper function for serializing objects
*/
serialize(value) {
try {
return JSON.stringify(value);
}
catch (e) {
throw new Error(`Failed to serialize value while storing credentials`);
}
}
/**
* Helper function for deserializing objects
*/
deserialize(serialized) {
try {
return JSON.parse(serialized);
}
catch (e) {
throw new Error(`Failed to deserialize value while retrieving credentials`);
}
}
/**
* Validate input parameters for service and key
*/
validateInput(service, key) {
if (!service || !key) {
this.log.error('Invalid input: Service or Key is missing.');
return false;
}
return true;
}
/**
* Save credentials to the keyring
*/
async save(service, key, value) {
if (!this.validateInput(service, key)) {
return false;
}
try {
const serialized = this.serialize(value);
await this.keyring.setPassword(service, key, serialized);
this.log.info(`Credential saved successfully. Service: [${service}], Key: [${key}]`);
return true;
}
catch (e) {
this.log.error(`Failed to save credential. Service: [${service}], Key: [${key}]. Error: ${(0, utils_1.errorString)(e)}`);
return false;
}
}
/**
* Retrieve credentials from the keyring
*/
async retrieve(service, key) {
if (!this.validateInput(service, key)) {
return undefined;
}
try {
const serializedValue = await this.keyring.getPassword(service, key);
if (!serializedValue) {
this.log.warn(`No credential found. Service: [${service}], Key: [${key}]`);
return undefined;
}
return this.deserialize(serializedValue);
}
catch (e) {
if (e instanceof Error) {
this.log.error(`Deserialization error for Service: [${service}], Key: [${key}]. Error: ${(0, utils_1.errorString)(e)}`);
}
else {
this.log.error(`Failed to retrieve credential. Service: [${service}], Key: [${key}]. Error: ${(0, utils_1.errorString)(e)}`);
}
return undefined;
}
}
/**
* Delete credentials from the keyring
*/
async delete(service, key) {
if (!this.validateInput(service, key)) {
return false;
}
try {
const deleted = await this.keyring.deletePassword(service, key);
if (deleted) {
this.log.info(`Credential deleted. Service: [${service}], Key: [${key}]`);
}
else {
this.log.warn(`No credential to delete. Service: [${service}], Key: [${key}]`);
}
return deleted;
}
catch (e) {
this.log.error(`Failed to delete credential. Service: [${service}], Key: [${key}]. Error: ${(0, utils_1.errorString)(e)}`);
return false;
}
}
/**
* Retrieve all credentials for a given service
*/
async getAll(service) {
const results = {};
try {
const entries = await this.keyring.findCredentials(service);
if (!entries || entries.length === 0) {
this.log.warn(`No credentials found for Service: [${service}]`);
return results;
}
entries.forEach(({ account, password }) => {
if (account) {
try {
const value = this.deserialize(password);
results[account] = value;
}
catch (e) {
this.log.error(`Failed to parse credential for Account: [${account}]. Error: ${(0, utils_1.errorString)(e)}`);
}
}
});
this.log.info(`All credentials retrieved. Service: [${service}], Count: ${Object.keys(results).length}`);
return results;
}
catch (e) {
this.log.error(`Failed to retrieve credentials for Service: [${service}]. Error: ${(0, utils_1.errorString)(e)}`);
return results;
}
}
}
exports.KeyStoreManager = KeyStoreManager;
//# sourceMappingURL=key-store.js.map