@aminya/dotenv-vault
Version:
A secrets manager for .env files – from the same people that pioneered dotenv.
91 lines (90 loc) • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogoutService = void 0;
const tslib_1 = require("tslib");
const crypto = tslib_1.__importStar(require("crypto"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const axios_1 = tslib_1.__importDefault(require("axios"));
const vars_1 = require("../vars");
const core_1 = require("@oclif/core");
const log_service_1 = require("../services/log-service");
const abort_service_1 = require("../services/abort-service");
class LogoutService {
constructor(attrs = {}) {
this.cmd = attrs.cmd;
this.yes = attrs.yes;
this.log = new log_service_1.LogService({ cmd: attrs.cmd });
this.abort = new abort_service_1.AbortService({ cmd: attrs.cmd });
const rand = crypto.randomBytes(32).toString('hex');
this.requestUid = `req_${rand}`;
this.checkCount = 0;
}
async run() {
await this.logout();
}
async logout(tip = true) {
if (!this.yes) {
this.log.local(`Logout URL: ${this.logoutUrl}`);
const answer = await core_1.CliUx.ux.prompt(`${chalk_1.default.dim(this.log.pretextLocal)}Press ${chalk_1.default.green('y')} (or any key) to logout and revoke credential (.env.me) or ${chalk_1.default.yellow('q')} to exit`);
if (answer === 'q' || answer === 'Q') {
this.abort.quit();
}
}
this.log.local(`Opening browser to ${this.logoutUrl}`);
core_1.CliUx.ux.open(this.logoutUrl).catch(_ => { });
core_1.CliUx.ux.action.start(`${chalk_1.default.dim(this.log.pretextLocal)}Waiting for logout and credential (.env.me) to be revoked`);
await this.check(tip);
}
async check(tip = true) {
if (this.controller) {
this.controller.abort();
}
this.controller = new AbortController();
const options = {
method: 'POST',
headers: { 'content-type': 'application/json' },
data: {
vaultUid: vars_1.vars.vaultValue,
requestUid: this.requestUid,
},
url: this.checkUrl,
signal: this.controller.signal,
};
let resp;
try {
this.checkCount += 1;
resp = await (0, axios_1.default)(options);
}
catch (error) {
resp = error.response;
}
finally {
if (resp.status < 300) {
// Step 3
core_1.CliUx.ux.action.stop();
const meUid = resp.data.data.meUid;
this.log.local(`Revoked .env.me (DOTENV_ME=${meUid.slice(0, 9)}...)`);
if (tip) {
this.log.plain('');
this.log.plain(`Run ${chalk_1.default.bold(`${vars_1.vars.cli} login`)} to generate a new credential (.env.me)`);
}
}
else if (this.checkCount < 50) {
// 404 - keep trying
await core_1.CliUx.ux.wait(2000); // check every 2 seconds
await this.check(tip); // check again
}
else {
core_1.CliUx.ux.action.stop('giving up');
this.log.local('Things were taking too long... gave up. Please try again.');
}
}
}
get logoutUrl() {
return `${vars_1.vars.apiUrl}/logout?DOTENV_VAULT=${vars_1.vars.vaultValue}&requestUid=${this.requestUid}`;
}
get checkUrl() {
return `${vars_1.vars.apiUrl}/check?DOTENV_VAULT=${vars_1.vars.vaultValue}&requestUid=${this.requestUid}`;
}
}
exports.LogoutService = LogoutService;