UNPKG

@salesforce/core

Version:

Core libraries to interact with SFDX projects, orgs, and APIs.

182 lines 7.01 kB
"use strict"; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthRemover = void 0; const kit_1 = require("@salesforce/kit"); const aliases_1 = require("../config/aliases"); const config_1 = require("../config/config"); const configAggregator_1 = require("../config/configAggregator"); const logger_1 = require("../logger"); const messages_1 = require("../messages"); const globalInfoConfig_1 = require("../config/globalInfoConfig"); const orgConfigProperties_1 = require("./orgConfigProperties"); messages_1.Messages.importMessagesDirectory(__dirname); const coreMessages = messages_1.Messages.load('@salesforce/core', 'core', ['namedOrgNotFound']); const messages = messages_1.Messages.load('@salesforce/core', 'auth', ['defaultUsernameNotSet']); /** * Handles the removing of authorizations, which includes deleting the auth file * in the global .sfdx folder, deleting any configs that are associated with the username/alias, * and deleting any aliases associated with the username * * ``` * const remover = await AuthRemover.create(); * await remover.removeAuth('example@mycompany.com'); * ``` * * ``` * const remover = await AuthRemover.create(); * await remover.removeAllAuths(); * ``` * * ``` * const remover = await AuthRemover.create(); * const auth = await remover.findAuth( * example@mycompany.com * ); * await remover.removeAuth(auth.username); * ``` */ class AuthRemover extends kit_1.AsyncOptionalCreatable { /** * Removes the authentication and any configs or aliases associated with it * * @param usernameOrAlias the username or alias that you want to remove */ async removeAuth(usernameOrAlias) { const username = await this.resolveUsername(usernameOrAlias); this.logger.debug(`Removing authorization for user ${username}`); await this.unsetConfigValues(username); await this.unsetAliases(username); this.globalInfo.unsetOrg(username); await this.globalInfo.write(); } /** * Removes all authentication files and any configs or aliases associated with them */ async removeAllAuths() { const auths = this.findAllAuths(); const usernames = Object.keys(auths); for (const username of usernames) { await this.removeAuth(username); } } /** * Finds authorization files for username/alias in the global .sfdx folder * **Throws** *{@link SfdxError}{ name: 'DefaultUsernameNotSetError' }* if no defaultusername * **Throws** *{@link SfdxError}{ name: 'NamedOrgNotFoundError' }* if specified user is not found * * @param usernameOrAlias username or alias of the auth you want to find, defaults to the configured defaultusername * @returns {Promise<SfOrg>} */ async findAuth(usernameOrAlias) { const username = usernameOrAlias ? await this.resolveUsername(usernameOrAlias) : await this.getDefaultUsername(); const auth = this.globalInfo.getOrg(username); if (!auth) { throw coreMessages.createError('namedOrgNotFound'); } return auth; } /** * Finds all authorization files in the global .sfdx folder * * @returns {SfOrgs} */ findAllAuths() { return this.globalInfo.getOrgs(); } async init() { this.logger = await logger_1.Logger.child(this.constructor.name); this.globalConfig = await this.getConfig(true); this.localConfig = await this.getConfig(false); this.globalInfo = await globalInfoConfig_1.GlobalInfo.getInstance(); this.aliases = await aliases_1.Aliases.create(aliases_1.Aliases.getDefaultOptions()); } /** * Returns the username for a given alias if the alias exists. * * @param usernameOrAlias username or alias * @returns {Promise<string>} */ async resolveUsername(usernameOrAlias) { const aliasedValue = this.aliases.get(usernameOrAlias); return (aliasedValue || usernameOrAlias); } /** * Instantiates config class * * @param isGlobal * @returns {Promise<Nullable<Config>>} */ async getConfig(isGlobal) { let config; try { config = await config_1.Config.create({ isGlobal }); } catch { config = null; } return config; } /** * @returns {Promise<string>} */ async getDefaultUsername() { const configAggregator = await configAggregator_1.ConfigAggregator.create(); const defaultUsername = configAggregator.getInfo(orgConfigProperties_1.OrgConfigProperties.TARGET_ORG).value || configAggregator.getInfo(config_1.SfdxPropertyKeys.DEFAULT_USERNAME).value; if (!defaultUsername) { throw messages.createError('defaultUsernameNotSet'); } return defaultUsername; } /** * Returns aliases for provided username * * @param username username that's been aliased * @returns {Promise<string[]>} */ getAliases(username) { return this.aliases.getKeysByValue(username) || []; } /** * Unsets any configured values (both global and local) for provided username * * @param username username that you want to remove from config files */ async unsetConfigValues(username) { const aliases = this.getAliases(username); this.logger.debug(`Clearing config keys for username ${username} and aliases: ${aliases}`); for (const config of [this.globalConfig, this.localConfig]) { if (config) { const keysWithUsername = config.getKeysByValue(username) || []; const keysWithAlias = aliases .map((alias) => config.getKeysByValue(alias)) .filter((k) => !!k) .reduce((x, y) => x.concat(y), []); const allKeys = keysWithUsername.concat(keysWithAlias); this.logger.debug(`Found these config keys to remove: ${allKeys}`); allKeys.forEach((key) => config.unset(key)); await config.write(); } } } /** * Unsets any aliases for provided username * * @param username username that you want to remove from aliases */ async unsetAliases(username) { this.logger.debug(`Clearing aliases for username: ${username}`); const existingAliases = this.aliases.getKeysByValue(username); this.logger.debug(`Found these aliases to remove: ${existingAliases}`); existingAliases.forEach((alias) => this.aliases.unset(alias)); await this.aliases.write(); } } exports.AuthRemover = AuthRemover; //# sourceMappingURL=authRemover.js.map