UNPKG

@zendesk/zcli-core

Version:

ZCLI core libraries and services

89 lines (88 loc) 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("@oclif/core/lib/errors"); const chalk = require("chalk"); const core_1 = require("@oclif/core"); const config_1 = require("./config"); const axios_1 = require("axios"); const authUtils_1 = require("./authUtils"); const requestUtils_1 = require("./requestUtils"); const secretType_1 = require("./secretType"); class Auth { constructor(options) { this.secureStore = options === null || options === void 0 ? void 0 : options.secureStore; this.config = new config_1.default(); } // 1. If env vars are set, prepare token using them // 2. If no env vars, check if current profile is set async getAuthorizationToken() { const { ZENDESK_EMAIL, ZENDESK_PASSWORD, ZENDESK_API_TOKEN, ZENDESK_OAUTH_TOKEN } = process.env; if (ZENDESK_OAUTH_TOKEN) { return `Bearer ${ZENDESK_OAUTH_TOKEN}`; } else if (ZENDESK_EMAIL && ZENDESK_API_TOKEN) { return this.createBasicAuthToken(`${ZENDESK_EMAIL}`, ZENDESK_API_TOKEN); } else if (ZENDESK_EMAIL && ZENDESK_PASSWORD) { return this.createBasicAuthToken(ZENDESK_EMAIL, ZENDESK_PASSWORD, secretType_1.SecretType.PASSWORD); } else { const profile = await this.getLoggedInProfile(); if (profile && this.secureStore) { const authToken = await this.secureStore.getSecret((0, authUtils_1.getAccount)(profile.subdomain, profile.domain)); return authToken; } return undefined; } } createBasicAuthToken(user, secret, secretType = secretType_1.SecretType.TOKEN) { const basicBase64 = (str) => `Basic ${Buffer.from(str).toString('base64')}`; if (secretType === secretType_1.SecretType.TOKEN) { return basicBase64(`${user}/token:${secret}`); } throw new errors_1.CLIError(chalk.red(`Basic authentication of type '${secretType}' is not supported.`)); } getLoggedInProfile() { return this.config.getConfig('activeProfile'); } setLoggedInProfile(subdomain, domain) { return this.config.setConfig('activeProfile', { subdomain, domain }); } async loginInteractively(options) { const subdomain = (0, authUtils_1.parseSubdomain)((options === null || options === void 0 ? void 0 : options.subdomain) || await core_1.CliUx.ux.prompt('Subdomain')); const domain = options === null || options === void 0 ? void 0 : options.domain; const account = (0, authUtils_1.getAccount)(subdomain, domain); const baseUrl = (0, requestUtils_1.getBaseUrl)(subdomain, domain); const email = await core_1.CliUx.ux.prompt('Email'); const token = await core_1.CliUx.ux.prompt('API Token', { type: 'hide' }); const authToken = this.createBasicAuthToken(email, token); const testAuth = await axios_1.default.get(`${baseUrl}/api/v2/account/settings.json`, { headers: { Authorization: authToken }, validateStatus: function (status) { return status < 500; }, adapter: 'fetch' }); if (testAuth.status === 200 && this.secureStore) { await this.secureStore.setSecret(account, authToken); await this.setLoggedInProfile(subdomain, domain); return true; } return false; } async logout() { if (!this.secureStore) { throw new errors_1.CLIError(chalk.red('Secure credentials store not found.')); } const profile = await this.getLoggedInProfile(); if (!(profile === null || profile === void 0 ? void 0 : profile.subdomain)) throw new errors_1.CLIError(chalk.red('Failed to log out: no active profile found.')); await this.config.removeConfig('activeProfile'); const deleted = await this.secureStore.deleteSecret((0, authUtils_1.getAccount)(profile.subdomain, profile.domain)); if (!deleted) throw new errors_1.CLIError(chalk.red('Failed to log out: Account, Service not found.')); return true; } async getSavedProfiles() { return this.secureStore && this.secureStore.getAllCredentials(); } } exports.default = Auth;