UNPKG

@interaktiv/mibuilder-core

Version:

Core libraries to interact with MiBuilder projects.

120 lines (91 loc) 3.24 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.AccountManager = exports.DEFAULT_PREFIX = void 0; var _keytar = _interopRequireDefault(require("keytar")); var _dxl = require("@interaktiv/dxl"); var _mibuilderError = require("../mibuilder-error"); var _ux = require("../ux"); const DEFAULT_PREFIX = 'mibuilder'; exports.DEFAULT_PREFIX = DEFAULT_PREFIX; class AccountManager extends _dxl.AsyncOptionalCreatable { constructor(options = {}) { super(options); this.prefix = options.prefix || DEFAULT_PREFIX; this.serviceName = options.serviceName; this.user = options.user; this.password = options.password; this.note = options.note; this.passwordType = options.passwordType; } async init() { this.cliUx = await _ux.UX.create('account-manager'); } isDefaultPrefix() { return this.prefix === DEFAULT_PREFIX; } async find() { const { account, password } = await _keytar.default.findCredentials(this.getServerName()); return { user: account, password }; } getServerName() { if (this.serviceName) return this.serviceName; return `${this.prefix}.${this.user.toLowerCase().replace(/\s/g, '.')}`; } async getPassword(askIfMissing = true) { if (this.password == null) { const passwordFromKeyChain = await _keytar.default.findPassword(this.getServerName()); if (passwordFromKeyChain) this.password = passwordFromKeyChain; } if (askIfMissing === true && (this.password == null || this.password.length === 0)) { await this.askForLogin(); } return this.password; } getUser() { return this.user; } async addToKeyChain() { await _keytar.default.setPassword(this.getServerName(), this.user, this.password); return true; } async removeFromKeyChain() { await _keytar.default.deletePassword(this.getServerName(), this.user); this.password = null; } async askForLogin() { if (this.user == null || this.user.length === 0) { let promptText = 'Benutzername'; if (this.note) { promptText = `${promptText} (${this.note})`; } const newUser = await this.cliUx.prompt(`\n${promptText}`); if (newUser) this.user = newUser; // Returning here since only the username was asked for. This method will // be called again when a password is needed. return null; } if (this.password == null || this.password.length === 0) { let promptText = this.passwordType || 'Passwort'; if (this.note) { promptText = `${promptText} (${this.note} für ${this.user})`; } else { promptText = `${promptText} (für ${this.user})`; } const newPassword = await this.cliUx.prompt(promptText, { type: 'hide' }); if (newPassword) this.password = newPassword; } // Now we store this information in the keychain if (await this.addToKeyChain()) return true; throw new _mibuilderError.MiBuilderError('Password konnte nicht in der Keychain gespeichert werden.', 'KeychainSaveError'); } } exports.AccountManager = AccountManager;