UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

163 lines (162 loc) 7.93 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); // Implementation of AppCenter login command const os = require("os"); const opener = require("opener"); const qs = require("qs"); const commandline_1 = require("../util/commandline"); const profile_1 = require("../util/profile"); const interaction_1 = require("../util/interaction"); const apis_1 = require("../util/apis"); const logout_1 = require("./lib/logout"); const debug = require("debug")("appcenter-cli:commands:login"); let LoginCommand = class LoginCommand extends commandline_1.Command { constructor(args) { super(args); } runNoClient() { return __awaiter(this, void 0, void 0, function* () { let result = this.validateArguments(); let userSuppliedToken = false; try { if (commandline_1.succeeded(result)) { yield this.removeLoggedInUser(); let token; if (this.token) { userSuppliedToken = true; token = yield this.doTokenLogin(); } else if (this.isInteractiveEnvironment) { userSuppliedToken = false; token = yield this.doInteractiveLogin(); } else { userSuppliedToken = false; token = yield this.doUserNameAndPasswordLogin(); } const userResponse = yield interaction_1.out.progress("Getting user info ...", this.getUserInfo(token.token)); yield profile_1.saveUser(userResponse.result, { id: token.id, token: token.token }, this.environmentName, userSuppliedToken); interaction_1.out.text(`Logged in as ${userResponse.result.name}`); // Force early exit to avoid long standing delays if token deletion is slow process.exit(0); result = commandline_1.success(); } } catch (err) { result = commandline_1.failure(commandline_1.ErrorCodes.Exception, err.message); } return result; }); } get isInteractiveEnvironment() { return !!(profile_1.environments(this.environmentName).loginEndpoint); } validateArguments() { if (this.isInteractiveEnvironment && (this.userName || this.password) && !this.token) { return commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "this environment requires interactive login, do not use the --user or --password switches"); } if (!this.isInteractiveEnvironment && (this.userName || this.password) && this.token) { return commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "you must specify either a token or a user/password, not both"); } if (profile_1.getTokenFromEnvironmentVar()) { return commandline_1.failure(commandline_1.ErrorCodes.IllegalCommand, `can't login when token is set in environment variable ${profile_1.appCenterAccessTokenEnvVar}`); } return commandline_1.success(); } doUserNameAndPasswordLogin() { return __awaiter(this, void 0, void 0, function* () { const questions = this.userNameQuestion.concat(this.passwordQuestion); if (questions.length > 0) { const answers = yield interaction_1.prompt.question(questions); Object.assign(this, answers); } const endpoint = profile_1.environments(this.environmentName).endpoint; const client = this.clientFactory.fromUserNameAndPassword(this.userName, this.password, endpoint); const createTokenResponse = yield interaction_1.out.progress("Logging in...", apis_1.clientRequest((cb) => client.apiTokens.newMethod({ description: "AppCenter CLI" }, cb))); if (createTokenResponse.response.statusCode >= 400) { throw new Error("login was not successful"); } return { id: createTokenResponse.result.id, token: createTokenResponse.result.apiToken }; }); } doInteractiveLogin() { return __awaiter(this, void 0, void 0, function* () { const loginUrl = profile_1.environments(this.environmentName).loginEndpoint + "?" + qs.stringify({ hostname: os.hostname() }); interaction_1.out.text(`Opening your browser... ${os.EOL}? Visit ${loginUrl} and enter the code:`); opener(loginUrl); const token = yield interaction_1.prompt("Access code from browser: "); return { id: null, token: token }; }); } doTokenLogin() { return __awaiter(this, void 0, void 0, function* () { return { id: "SuppliedByUser", token: this.token }; }); } get userNameQuestion() { if (!this.token && !this.userName) { return [{ name: "userName", message: "Username or email: " }]; } return []; } get passwordQuestion() { if (!this.token && !this.password) { return [{ type: "password", name: "password", message: "Password: " }]; } return []; } getUserInfo(token) { const endpoint = profile_1.environments(this.environmentName).endpoint; const client = this.clientFactory.fromToken(token, endpoint); return apis_1.clientRequest((cb) => client.users.get(cb)); } removeLoggedInUser() { return __awaiter(this, void 0, void 0, function* () { const currentUser = profile_1.getUser(); if (currentUser !== null) { debug(`Currently logged in as ${currentUser.userName}, removing token`); debug(`Creating client factory`); const client = this.clientFactory.fromProfile(currentUser); debug(`Removing existing token`); yield logout_1.logout(client, currentUser); } }); } }; __decorate([ commandline_1.help("Username to log in as"), commandline_1.shortName("u"), commandline_1.longName("user"), commandline_1.hasArg ], LoginCommand.prototype, "userName", void 0); __decorate([ commandline_1.help("Password to log in with"), commandline_1.shortName("p"), commandline_1.longName("password"), commandline_1.hasArg ], LoginCommand.prototype, "password", void 0); LoginCommand = __decorate([ commandline_1.help("Log in") ], LoginCommand); exports.default = LoginCommand;