/** * QCObjects Framework SDK * ________________ * * Author: Jean Machuca * * Cross Browser Javascript Framework for MVC Patterns * QCObjects is licensed under the MIT License * [LICENSE] (https://github.com/QCObjects/qcobjects-sdk/blob/main/LICENSE.txt) * * Copyright (C) 2015 Jean Machuca, * * Everyone is permitted to copy and distribute verbatim copies of this * license document, but changing it is not allowed. */ "use strict"; import { CONFIG, ComplexStorageCache, InheritClass, New, Package, _Crypt, get, set } from "qcobjects"; type TGlobalUser = { username: string, token: string, id: string, priority: number }; export class SessionUserToken extends InheritClass { static user = {}; __cache__: ComplexStorageCache; __instanceID: any; constructor(o: any) { super(o); const __instance__ = this; this.__cache__ = new ComplexStorageCache({ index: __instance__.__instanceID.toString(), load() { let __token__; if (typeof navigator !== "undefined" && typeof origin !== "undefined") { __token__ = _Crypt.encrypt(`${navigator.userAgent}|${o.username}|${(+(new Date())).toString()}`, origin); } else { __token__ = _Crypt.encrypt(`${o.username}|${(+(new Date())).toString()}`, CONFIG.get("domain", "localhost")); } SessionUserToken.user = { priority: __instance__.__instanceID.toString(), token: __token__ }; return SessionUserToken.user; }, alternate(cacheController: any) { SessionUserToken.user = cacheController?.cache.getCached(__instance__.__instanceID.toString()); // setting dataObject with the cached value } }); } static generateIndex(s: any) { return (typeof Buffer !== "undefined") ? (Buffer.from(s, "ascii").toString("base64")) : (btoa(s)); } static getGlobalUser(...args: any[]): TGlobalUser { const username = [args].join("|"); const __index__ = "userToken_" + SessionUserToken.generateIndex(username); if (typeof get(__index__) === "undefined" || get(__index__) === null) { set(__index__, New(SessionUserToken, { username })); } SessionUserToken.user = get(__index__).user; return get(__index__).user as TGlobalUser; } static getGlobalUserToken(...args: any[]): string { return SessionUserToken.getGlobalUser(args).token; } static getGlobalUserId(...args: any[]) { return SessionUserToken.getGlobalUser(args).id; } static getGlobalUserPriority(...args: any[]) { return SessionUserToken.getGlobalUser(args).priority; } static getLoginCredentialsToken(username: string, password: string): string { return _Crypt.encrypt(`${username}${password}`, SessionUserToken.getGlobalUserToken(username)) as string; } static closeGlobalSession(...args: any[]) { SessionUserToken.getGlobalUser(args); const username = [args].join("|"); const __index__ = "userToken_" + SessionUserToken.generateIndex(username); if (typeof get(__index__) !== "undefined") { get(__index__).__cache__.clear(); set(__index__, null); SessionUserToken.user = {}; } } } Package("org.qcobjects.cloud.auth.session.usertoken", [ SessionUserToken ]);