UNPKG

baasic-sdk-javascript

Version:

JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).

140 lines (139 loc) 5.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var common_1 = require("../common"); var contracts_1 = require("./contracts"); var inversify_1 = require("inversify"); var TokenHandler = /** @class */ (function () { function TokenHandler(eventHandler, storageHandler, application) { this.eventHandler = eventHandler; this.storageHandler = storageHandler; this.application = application; this.utility = new common_1.Utility(); this.messageTypes = { tokenExpired: 'tokenExpired', tokenUpdated: 'tokenUpdated' }; this.initEventing(); this.tokenKey = 'baasic-auth-token-' + this.application.getApiKey(); this.token = this.get(contracts_1.TokenTypes.Access); if (this.token) { this.userAccessTokenTimerHandle = this.setExpirationTimer(this.token); } } Object.defineProperty(TokenHandler.prototype, "tokenExpiredEventName", { get: function () { return "tokenExpired-" + this.application.getApiKey(); }, enumerable: true, configurable: true }); Object.defineProperty(TokenHandler.prototype, "tokenUpdatedEventName", { get: function () { return "tokenUpdated-" + this.application.getApiKey(); }, enumerable: true, configurable: true }); TokenHandler.prototype.store = function (token, options) { //Type guard for plain JavaScript var anyToken = token; if (anyToken && !this.utility.isUndefined(anyToken.access_token)) { var t = { token: anyToken.access_token, expires_in: anyToken.expires_in, sliding_window: anyToken.sliding_window, tokenUrl: anyToken.access_url_token, type: anyToken.token_type }; token = t; } this.syncToken(token); if (token === undefined || token === null) { this.storageHandler.remove(this.tokenKey); } else { this.storageHandler.set(this.tokenKey, token); } if (options && options.skipTokenEvents) { return; } this.handleTokenEvent(token); }; TokenHandler.prototype.handleTokenEvent = function (token) { if (token === undefined || token === null) { this.triggerTokenExpired(this.application); } else { this.triggerTokenUpdated(this.application); } }; TokenHandler.prototype.get = function (type) { var token = this.storageHandler.get(this.tokenKey); if (typeof token === 'string') { return JSON.parse(token); } return token; }; TokenHandler.prototype.triggerTokenExpired = function (app) { var data = { app: app }; this.eventHandler.triggerEvent(this.tokenExpiredEventName, data); this.eventHandler.pushMessage({ type: this.tokenExpiredEventName }, {}); }; TokenHandler.prototype.triggerTokenUpdated = function (app) { var data = { app: app }; this.eventHandler.triggerEvent(this.tokenUpdatedEventName, data); this.eventHandler.pushMessage({ type: this.tokenUpdatedEventName }, {}); }; TokenHandler.prototype.setExpirationTimer = function (token) { var _this = this; if (token && token.expireTime) { var expiresIn = token.expireTime - new Date().getTime(); if (expiresIn > 0) { return setTimeout(function () { _this.store(null); }, expiresIn); } else { this.store(null); } } return null; }; TokenHandler.prototype.syncToken = function (newToken) { clearTimeout(this.userAccessTokenTimerHandle); if (newToken) { if (newToken.expires_in && !newToken.expireTime) { newToken.expireTime = new Date().getTime() + (newToken.expires_in * 1000); } else if (newToken.sliding_window) { newToken.expireTime = new Date().getTime() + (newToken.sliding_window * 1000); } this.userAccessTokenTimerHandle = this.setExpirationTimer(newToken); } }; TokenHandler.prototype.initEventing = function () { var _this = this; this.eventHandler.addEvent(this.tokenExpiredEventName, function (e) { e = e || event; if (e.originalEvent) { e = e.originalEvent; } _this.syncToken(null); }); }; TokenHandler = tslib_1.__decorate([ inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(contracts_1.TYPES.IEventHandler)), tslib_1.__param(1, inversify_1.inject(contracts_1.TYPES.IStorageHandler)), tslib_1.__param(2, inversify_1.inject(contracts_1.TYPES.IBaasicApp)), tslib_1.__metadata("design:paramtypes", [Object, Object, Object]) ], TokenHandler); return TokenHandler; }()); exports.TokenHandler = TokenHandler; ;