UNPKG

@sap-ux/system-access

Version:

Reusable module allowing to access systems using the store or prompts.

119 lines 3.77 kB
import { getService, BackendSystemKey, BackendSystem } from '@sap-ux/store'; import { isAppStudio } from '@sap-ux/btp-utils'; import prompts from 'prompts'; import { questions } from './prompts.js'; /** * Checks if credentials are of basic auth type. * * @param authOpts credential options * @returns boolean */ export function isBasicAuth(authOpts) { return !!authOpts && authOpts.password !== undefined; } /** * Checks if credentials are of service auth type. * * @param authOpts credential options * @returns boolean */ export 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. */ export async function getCredentialsFromStore(target, logger) { try { if (!isAppStudio()) { const systemService = await getService({ entityName: 'system' }); let url = target.url; if (target.connectPath) { const normalizedPath = target.connectPath.startsWith('/') ? target.connectPath : `/${target.connectPath}`; url = new URL(normalizedPath, target.url).href; } let system = await systemService.read(new BackendSystemKey({ url, client: target.client })); // check if there are credentials for the default client if (!system && target.client) { system = await systemService.read(new BackendSystemKey({ 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 target.systemType system type * @param target.connectionType connection type * @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 */ export async function storeCredentials(name, target, credentials, logger) { try { const systemService = await getService({ entityName: 'system' }); const system = new 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. */ export 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 */ export async function getCredentialsWithPrompts(username) { const credentials = await prompts([ { ...questions.username, initial: username }, questions.password ]); return credentials; } //# sourceMappingURL=credentials.js.map