@zowe/imperative
Version:
framework for building configurable CLIs
134 lines • 5.98 kB
JavaScript
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractCredentialManager = void 0;
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
const error_1 = require("../../../error");
/**
* The abstract credential manager defines basic methods that must be implemented by any credential manager
* provided to Imperative. The abstract class is used by various methods for proper typing information.
*
* All credential managers will be instantiated by {@link CredentialManagerFactory.initialize} and are
* expected to extend the __AbstractCredentialManager__. This is enforced by requiring your class constructor
* to follow the implementation rules specified by the {@link ICredentialManagerConstructor}.
*
* The constructor signature that the {@link ICredentialManagerConstructor} specifies will always be identical to
* the constructor signature of the __AbstractCredentialManager__ superclass.
*
* Imperative will provide your CLI with a default manager, {@link DefaultCredentialManager}. If the default provided
* is not to your liking, you can provide your own manager as a parameter on {@link Imperative.init} at startup. This is
* done by providing it as part of the {@link IImperativeConfig.overrides} object.
*
* @see {@link IImperativeOverrides.CredentialManager}
*
*/
class AbstractCredentialManager {
/**
* This class can not be directly instantiated so the constructor is protected. All extending classes must make a call
* to `super(...)` with the expected parameters.
*
* @param {string} service The service that the Credential Manager is running under. Imperative will set this to the
* cliName
* @param {string} displayName The display name of this manager. Used in messaging/logging.
*/
constructor(service, displayName) {
this.service = service;
this.displayName = displayName;
}
/**
* @returns {string} - the display name of this manager. Use in logging/messaging.
*/
get name() {
return this.displayName;
}
/**
* Delete credentials for an account managed by the credential manager.
*
* @param {string} account The account (or profile identifier) associated with credentials
*
* @returns {Promise<void>}
*/
delete(account) {
return __awaiter(this, void 0, void 0, function* () {
yield this.deleteCredentials(account);
});
}
/**
* Load credentials for an account managed by the credential manager.
*
* @param {string} account The account (or profile identifier) associated with credentials
* @param {boolean} optional Set to true if failure to find credentials should be ignored
*
* @returns {Promise<string>} The username and password associated with the account.
*/
load(account, optional) {
return __awaiter(this, void 0, void 0, function* () {
const encodedString = yield this.loadCredentials(account, optional);
if (optional && encodedString == null) {
return null;
}
return Buffer.from(encodedString, "base64").toString();
});
}
/**
* Save credentials for an account managed by the credential manager.
*
* @param {string} account The account (or profile identifier) associated with credentials
* @param {string} secureValue Value to be securely stored
*
* @returns {Promise<void>}
*
* @throws {@link ImperativeError} - when the secure field is missing.
*/
save(account, secureValue) {
return __awaiter(this, void 0, void 0, function* () {
// Check both username and password are set and are not empty strings. Ah, the magic of JavaScript
if (!(secureValue == null) && secureValue !== "") {
const encodedString = Buffer.from(`${secureValue}`).toString("base64");
yield this.saveCredentials(account, encodedString);
}
else {
throw new error_1.ImperativeError({
msg: "Missing Secure Field"
});
}
});
}
/**
* @returns {string} - Additional details for credential manager errors,
* if the current CredentialManager has provided any.
*/
secureErrorDetails() {
if (this.possibleSolutions == null || this.possibleSolutions.length === 0) {
return;
}
return ["Possible Solutions:", ...this.possibleSolutions].join("\n - ");
}
/**
* @returns {string[] | undefined} - List of possible solutions for credential manager errors.
* Override this in your CredentialManager to supply more detailed error messages.
*/
get possibleSolutions() {
return undefined;
}
}
exports.AbstractCredentialManager = AbstractCredentialManager;
//# sourceMappingURL=AbstractCredentialManager.js.map
;