UNPKG

@mamba-le/auth

Version:
498 lines (438 loc) 13.2 kB
/** * @description 认证模块 * @version 0.3.7 * @author lengyingxin <lengyingxin8966@gmail.com> (https://github.com/LengYXin) */ import _compact from 'lodash/compact'; import { __awaiter, __generator, __decorate } from 'tslib'; import { BindAll } from 'lodash-decorators'; import { toJS, computed } from 'mobx'; import { lastValueFrom, zip, Subject } from 'rxjs'; import { AuthController } from './auth'; import env__default from './env'; import { InfoController } from './info'; import { AuthOptions } from './options'; /** * Portal 认证信息 BP&SP * @export * @class PortalAuthController */ var PortalAuthController = /** @class */ function () { function PortalAuthController(options) { if (options === void 0) { options = { PortalType: 'BP' }; } var _a; this.options = options; /** * 持久化初始化完成 Subject * @type {Promise<any>} * @memberof ControllerUser */ this.HydrateSubject = new Subject(); // 浏览器 if (AuthOptions.browser) { // 微前端 环境中 if (AuthOptions.micro && ((_a = window === null || window === void 0 ? void 0 : window.rawWindow) === null || _a === void 0 ? void 0 : _a.PortalAuth)) { PortalAuthController_1.PortalAuth = window.rawWindow.PortalAuth; } else if (window === null || window === void 0 ? void 0 : window.PortalAuth) { PortalAuthController_1.PortalAuth = window.PortalAuth; } // 非微前端环境 if (!AuthOptions.micro) { window.PortalAuth = this; } // 单例 if (PortalAuthController_1.PortalAuth) { return PortalAuthController_1.PortalAuth; } } PortalAuthController_1.createAuth(this.options); PortalAuthController_1.PortalInfo = new InfoController({ StorageKey: 'PortalInfo' }); PortalAuthController_1.PortalDrainage = new InfoController({ StorageKey: 'PortalDrainage' }); this.createHydrate(); PortalAuthController_1.PortalAuth = this; AuthOptions.log('PortalAuth', this); } PortalAuthController_1 = PortalAuthController; Object.defineProperty(PortalAuthController, "env", { get: function get() { return env__default; }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "env", { get: function get() { return env__default; }, enumerable: false, configurable: true }); PortalAuthController.createAuth = function (options) { switch (options.PortalType) { case 'BP': PortalAuthController_1.BPAuth = new AuthController({ CookieKey: ['BPPORTAL_TOKEN'], StorageKey: 'BPPortal', TokenType: options.TokenType || 'JWT' }); return; case 'SP': PortalAuthController_1.SPAuth = new AuthController({ CookieKey: ['SALES_TOKEN'], StorageKey: 'SALESPortal', TokenType: options.TokenType || 'Other' }); return; default: PortalAuthController_1.GeneralAuth = new AuthController({ CookieKey: ['BPPORTAL_TOKEN', 'SALES_TOKEN'], StorageKey: 'Portal', TokenType: options.TokenType || 'Other' }); } }; Object.defineProperty(PortalAuthController.prototype, "HydrateAsync", { /** * 持久化初始化完成 Promise * @type {Promise<any>} * @memberof ControllerUser */ get: function get() { return lastValueFrom(this.HydrateSubject, { defaultValue: undefined }); }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "HydrateisStopped", { /** * 异步 HydrateSubject 已经完成 * @readonly * @memberof PortalAuthController */ get: function get() { return this.HydrateSubject.isStopped; }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "BPAuth", { /** * BP Auth * @type {AuthController} * @memberof PortalAuthController */ get: function get() { return PortalAuthController_1.BPAuth; }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "SPAuth", { /** * SP Auth * @type {AuthController} * @memberof PortalAuthController */ get: function get() { return PortalAuthController_1.SPAuth; }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "GeneralAuth", { /** * 通用的 Auth * @type {AuthController} * @memberof PortalAuthController */ get: function get() { return PortalAuthController_1.GeneralAuth; }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "PortalInfo", { /** * 数据 Info * @readonly * @memberof PortalAuthController */ get: function get() { return PortalAuthController_1.PortalInfo; }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "PortalDrainage", { /** * 数据 引流 * @readonly * @protected * @memberof PortalAuthController */ get: function get() { return PortalAuthController_1.PortalDrainage; }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "Info", { /** * 基础数据 * @readonly * @memberof PortalAuthController */ get: function get() { return toJS(this.PortalInfo.value); }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "Drainage", { /** * 引流数据 * @readonly * @memberof PortalAuthController */ get: function get() { return toJS(this.PortalDrainage.value); }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "AccessToken", { /** * AccessToken 返回校验 时间是否过期 过期 的 AccessToken 不返回 * @readonly * @memberof PortalAuthController */ get: function get() { switch (this.options.PortalType) { case 'BP': return this.BPAuth.AccessToken; case 'SP': return this.SPAuth.AccessToken; default: return this.GeneralAuth.AccessToken; } }, enumerable: false, configurable: true }); Object.defineProperty(PortalAuthController.prototype, "JwtDecoded", { /** * AccessToken 解码信息 * @readonly * @memberof PortalAuthController */ get: function get() { switch (this.options.PortalType) { case 'BP': return this.BPAuth.JwtDecoded; case 'SP': return this.SPAuth.JwtDecoded; default: return this.GeneralAuth.JwtDecoded; } }, enumerable: false, configurable: true }); /** * 保存 AccessToken 校验有效性 无效过滤 * @param {string} [AccessToken=undefined] * @memberof PortalAuthController */ PortalAuthController.prototype.onSaveAccessToken = function (AccessToken, setCookie) { var _a, _b, _c; if (AccessToken === void 0) { AccessToken = undefined; } if (setCookie === void 0) { setCookie = false; } (_a = this.BPAuth) === null || _a === void 0 ? void 0 : _a.onSaveAccessToken(AccessToken, setCookie); (_b = this.SPAuth) === null || _b === void 0 ? void 0 : _b.onSaveAccessToken(AccessToken, setCookie); (_c = this.GeneralAuth) === null || _c === void 0 ? void 0 : _c.onSaveAccessToken(AccessToken, setCookie); }; /** * 存储info 信息 * @param {*} value * @memberof PortalAuthController */ PortalAuthController.prototype.onSaveInfo = function (value) { this.PortalInfo.onSave(value); AuthOptions.trace('Save Info', value); }; /** * 存储 Drainage 信息 * @param {*} value * @memberof PortalAuthController */ PortalAuthController.prototype.onSaveDrainage = function (value) { this.PortalDrainage.onSave(value); AuthOptions.trace('Save Drainage', value); }; /** * 清理所有的登录信息 * @return {*} * @memberof AuthController */ PortalAuthController.prototype.onClear = function () { var _a, _b, _c, _d, _e, _f, _g; return __awaiter(this, void 0, void 0, function () { var error_1; return __generator(this, function (_h) { switch (_h.label) { case 0: _h.trys.push([0, 4,, 5]); AuthOptions.writeCheck(); AuthOptions.trace('clear start'); (_a = this.BPAuth) === null || _a === void 0 ? void 0 : _a.onClear(); (_b = this.SPAuth) === null || _b === void 0 ? void 0 : _b.onClear(); (_c = this.GeneralAuth) === null || _c === void 0 ? void 0 : _c.onClear(); (_d = this.PortalInfo) === null || _d === void 0 ? void 0 : _d.onClear(); (_e = this.PortalDrainage) === null || _e === void 0 ? void 0 : _e.onClear(); if (!AuthOptions.browser) return [3 /*break*/ , 3]; (_f = window.localStorage) === null || _f === void 0 ? void 0 : _f.clear(); (_g = window.sessionStorage) === null || _g === void 0 ? void 0 : _g.clear(); return [4 /*yield*/ , this.HydrateAsync]; case 1: _h.sent(); return [4 /*yield*/ , AuthOptions.LocalForage.clear()]; case 2: _h.sent(); _h.label = 3; case 3: AuthOptions.log('clear success', 'localStorage,sessionStorage,IndexedDB,cookie 全部清理'); return [3 /*break*/ , 5]; case 4: error_1 = _h.sent(); AuthOptions.trace('clear error', error_1); return [3 /*break*/ , 5]; case 5: return [2 /*return*/ ]; } }); }); }; /** * 创建持久化存储 * @memberof BaseModel */ PortalAuthController.prototype.createHydrate = function () { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var observables, error_2; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 2,, 3]); observables = _compact([(_a = this.BPAuth) === null || _a === void 0 ? void 0 : _a.HydrateAsync, (_b = this.SPAuth) === null || _b === void 0 ? void 0 : _b.HydrateAsync, (_c = this.GeneralAuth) === null || _c === void 0 ? void 0 : _c.HydrateAsync, (_d = this.PortalInfo) === null || _d === void 0 ? void 0 : _d.HydrateAsync]); return [4 /*yield*/ , lastValueFrom(zip(observables), { defaultValue: undefined })]; case 1: _e.sent(); this.HydrateSubject.next(true); this.HydrateSubject.complete(); return [3 /*break*/ , 3]; case 2: error_2 = _e.sent(); AuthOptions.trace('error', error_2); this.HydrateSubject.next(false); this.HydrateSubject.complete(); return [3 /*break*/ , 3]; case 3: return [2 /*return*/ ]; } }); }); }; var PortalAuthController_1; /** * 存储的单列 * @private * @static * @type {PortalAuthController} * @memberof PortalAuthController */ PortalAuthController.PortalAuth = undefined; /** * 混合认证 * @protected * @static * @type {AuthController} * @memberof PortalAuthController */ PortalAuthController.GeneralAuth = undefined; /** * BP 认证 * @protected * @static * @type {AuthController} * @memberof PortalAuthController */ PortalAuthController.BPAuth = undefined; /** * SP 认证 * @protected * @static * @type {AuthController} * @memberof PortalAuthController */ PortalAuthController.SPAuth = undefined; /** * 基础信息 * @protected * @static * @type {InfoController} * @memberof PortalAuthController */ PortalAuthController.PortalInfo = undefined; /** * 引流信息 * @protected * @static * @type {InfoController} * @memberof PortalAuthController */ PortalAuthController.PortalDrainage = undefined; __decorate([computed], PortalAuthController.prototype, "Info", null); __decorate([computed], PortalAuthController.prototype, "Drainage", null); __decorate([computed], PortalAuthController.prototype, "AccessToken", null); __decorate([computed], PortalAuthController.prototype, "JwtDecoded", null); PortalAuthController = PortalAuthController_1 = __decorate([BindAll()], PortalAuthController); return PortalAuthController; }(); export { PortalAuthController }; //# sourceMappingURL=portal.js.map