UNPKG

teko-oauth2

Version:

Teko Identity OAuth 2 Javascript Library for Web App Client

97 lines (73 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; require("regenerator-runtime/runtime"); var _constants = require("./constants"); var _IframeWindow = _interopRequireDefault(require("./IframeWindow")); var _Timer = _interopRequireDefault(require("./Timer")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var CheckSessionService = /*#__PURE__*/function () { function CheckSessionService(userManager) { _classCallCheck(this, CheckSessionService); this._userManager = userManager; this._userManager._events.addUserLoaded(this._start.bind(this)); this._timer = new _Timer["default"]('Check Session', this._checkSession.bind(this), _constants.DEFAULT_CHECK_SESSION_FREQUENCY); this._checkSessionFrame = new _IframeWindow["default"]({ src: this._userManager._oidc._checkSessionUri, callback: this._sessionStatusCallback.bind(this), cleanupFrame: false }); this._start(); } _createClass(CheckSessionService, [{ key: "_checkSession", value: function _checkSession() { var _this = this; var message = this._userManager._clientId + ' ' + this._currentSessionState; this._checkSessionFrame.listen().then(function (sessionStatus) { if (sessionStatus === 'error') { throw new Error('Error throw from OP when get session status'); } else if (sessionStatus === 'changed') { _this._userManager._events._raiseUserSessionChanged(); } else if (sessionStatus === 'unchanged') { _this._start(); } else { throw new Error('Invalid OP session status'); } })["catch"](function (err) { console.warn(err); _this._stop(); }); this._checkSessionFrame.postMessage(message, this._userManager._oauthDomain); } }, { key: "_sessionStatusCallback", value: function _sessionStatusCallback(e) { if (e.origin !== this._userManager._oauthDomain || e.source !== this._checkSessionFrame.frame.contentWindow) { return Promise.resolve(); } return Promise.resolve(e.data); } }, { key: "_start", value: function _start() { var user = this._userManager._userData; if (user && user.sessionState) { this._currentSessionState = user.sessionState; this._timer.init(); } } }, { key: "_stop", value: function _stop() { this._timer.cancel(); } }]); return CheckSessionService; }(); exports["default"] = CheckSessionService;