UNPKG

@viewdo/dxp-story-cli

Version:

DXP Story Management CLI

109 lines 5.41 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 __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthenticationManager = void 0; const ConventionService_1 = require("./../services/ConventionService"); const date_fns_1 = require("date-fns"); const FileService_1 = require("./../services/FileService"); const ConsoleService_1 = require("../services/ConsoleService"); const AuthenticationService_1 = require("../services/AuthenticationService"); const ConfigurationFileService_1 = require("../services/ConfigurationFileService"); const AuthConfiguration_1 = require("../models/configuration/AuthConfiguration"); const typedi_1 = require("typedi"); const JSONFormatService_1 = require("../services/JSONFormatService"); let AuthenticationManager = class AuthenticationManager { constructor(auth_service, console_service, json_format_service, file_service, conventions) { this.auth_service = auth_service; this.console_service = console_service; this.json_format_service = json_format_service; this.file_service = file_service; this.conventions = conventions; this._readConfig(); } _readConfig() { this.config_accessor = new ConfigurationFileService_1.ConfigurationFileService(`~/.dxp/${this.conventions.auth_filename}`, AuthConfiguration_1.AuthConfiguration); this.config = this.config_accessor.read(new AuthConfiguration_1.AuthConfiguration()); this.email = this.config.email; this.token = this.config.token; } // config API ------ hasToken() { if (this.token) { let expiration = (0, date_fns_1.subHours)(new Date(), 12); // 12 hour expiration return this.config.updated > expiration; } return false; } setToken(token) { this.token = token; this.saveConfig(); } setEmail(email) { this.email = email; this.saveConfig(); } saveConfig() { this.config.email = this.email; this.config.token = this.token; this.config.updated = new Date(); this.config_accessor.write(this.config); } // auth API ----- requireToken(client_id_1, client_secret_1) { return __awaiter(this, arguments, void 0, function* (client_id, client_secret, warn = true) { if (this.hasToken()) return true; const token = yield this.auth_service.getAppTokenFromPlatform(client_id, client_secret); if (token) { this.setToken(token); this.console_service.log(`You are authenticated ${this.email ? `as ${this.email}` : ""}`.green); return true; } else if (warn) { this.console_service.log(`You must be authenticated to use this command`.yellow); this.console_service.log(`Run ${"storyctl login".yellow} ${"to authenticate first".gray}`.gray); } return false; }); } sendVerificationEmail(client_id, email) { this.setEmail(email); return this.auth_service.sendVerificationEmail(client_id, email); } getTokenFromVerification(client_id, email, code) { return __awaiter(this, void 0, void 0, function* () { let token = yield this.auth_service.getTokenFromVerification(client_id, email, code); if (!token) throw new Error("Something went wrong."); this.setToken(token); return this.token; }); } }; exports.AuthenticationManager = AuthenticationManager; exports.AuthenticationManager = AuthenticationManager = __decorate([ (0, typedi_1.Service)(), __metadata("design:paramtypes", [AuthenticationService_1.AuthenticationService, ConsoleService_1.ConsoleService, JSONFormatService_1.JSONFormatService, FileService_1.FileService, ConventionService_1.ConventionService]) ], AuthenticationManager); //# sourceMappingURL=AuthenticationManager.js.map