@sap-ux/system-access
Version:
Reusable module allowing to access systems using the store or prompts.
121 lines • 3.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isBasicAuth = isBasicAuth;
exports.isServiceAuth = isServiceAuth;
exports.getCredentialsFromStore = getCredentialsFromStore;
exports.storeCredentials = storeCredentials;
exports.getCredentialsFromEnvVariables = getCredentialsFromEnvVariables;
exports.getCredentialsWithPrompts = getCredentialsWithPrompts;
const store_1 = require("@sap-ux/store");
const btp_utils_1 = require("@sap-ux/btp-utils");
const prompts_1 = __importDefault(require("prompts"));
const prompts_2 = require("./prompts");
/**
* Checks if credentials are of basic auth type.
*
* @param authOpts credential options
* @returns boolean
*/
function isBasicAuth(authOpts) {
return !!authOpts && authOpts.password !== undefined;
}
/**
* Checks if credentials are of service auth type.
*
* @param authOpts credential options
* @returns boolean
*/
function isServiceAuth(authOpts) {
return !!authOpts && authOpts.serviceKeys !== undefined;
}
/**
* Check the secure storage if it has credentials for the given target.
*
* @param target ABAP target
* @param logger - reference to the logger instance
* @returns credentials from the store or undefined.
*/
async function getCredentialsFromStore(target, logger) {
try {
if (!(0, btp_utils_1.isAppStudio)()) {
const systemService = await (0, store_1.getService)({ entityName: 'system' });
let system = await systemService.read(new store_1.BackendSystemKey({ url: target.url, client: target.client }));
// check if there are credentials for the default client
if (!system && target.client) {
system = await systemService.read(new store_1.BackendSystemKey({ url: target.url }));
}
return system;
}
}
catch (error) {
logger.warn('Reading credentials from store failed');
logger.debug(error.message);
}
return undefined;
}
/**
* Store the credentials in the secure storage.
*
* @param name system name
* @param target target
* @param target.url system url
* @param target.client optional system client
* @param credentials basic auth credentials
* @param credentials.username username
* @param credentials.password password
* @param logger reference to the logger instance
* @returns true if the credentials are successfully stored
*/
async function storeCredentials(name, target, credentials, logger) {
try {
const systemService = await (0, store_1.getService)({ entityName: 'system' });
const system = new store_1.BackendSystem({
name,
...target,
...credentials
});
await systemService.write(system);
return true;
}
catch (error) {
logger.error('Could not store credentials.');
logger.debug(error);
return false;
}
}
/**
* Checks the environment variables for Fiori tools settings.
*
* @returns basic auth credentials from the environment or undefined.
*/
function getCredentialsFromEnvVariables() {
if (process.env.FIORI_TOOLS_USER && process.env.FIORI_TOOLS_PASSWORD) {
return {
username: process.env.FIORI_TOOLS_USER,
password: process.env.FIORI_TOOLS_PASSWORD
};
}
else {
return undefined;
}
}
/**
* Prompt for username and password.
*
* @param username - optional username that is to be offered as default
* @returns credentials object with username/password
*/
async function getCredentialsWithPrompts(username) {
const credentials = await (0, prompts_1.default)([
{
...prompts_2.questions.username,
initial: username
},
prompts_2.questions.password
]);
return credentials;
}
//# sourceMappingURL=credentials.js.map