UNPKG

@kineticdata/react

Version:
199 lines (191 loc) 8.29 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports.singleSignOn = exports.retrieveJwt = exports.logoutDirect = exports.login = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/toConsumableArray")); var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/regeneratorRuntime")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/asyncToGenerator")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2")); var _axios = _interopRequireDefault(require("axios")); var _helpers = require("../../helpers"); var _profile = require("./profile"); var _http = require("../http"); var login = exports.login = function login(_ref) { var username = _ref.username, password = _ref.password; return _axios["default"].post("".concat(_helpers.bundle.spaceLocation(), "/app/login.do"), { j_username: username, j_password: password }, { __bypassAuthInterceptor: true })["catch"](_http.handleErrors); }; var logoutDirect = exports.logoutDirect = function logoutDirect(isSaml) { return _axios["default"].get(isSaml === true ? "".concat(_helpers.bundle.spaceLocation(), "/app/saml/logout") : "".concat(_helpers.bundle.spaceLocation(), "/app/logout")); }; var checkedOrigin = process.env.REACT_APP_API_HOST ? process.env.REACT_APP_API_HOST : typeof window !== 'undefined' ? window.location.origin : null; var clientId = process.env.REACT_APP_OAUTH_CLIENT_ID ? process.env.REACT_APP_OAUTH_CLIENT_ID : process.env.NODE_ENV === 'production' ? 'system' : 'system-dev'; var retrieveJwt = exports.retrieveJwt = function retrieveJwt() { return new Promise(function (resolve) { var iframe = document.createElement('iframe'); iframe.src = _helpers.bundle.spaceLocation() + '/app/oauth/authorize?grant_type=implicit&response_type=token&client_id=' + clientId; iframe.title = 'oauth jwt iframe'; iframe.style.cssText = 'display: none'; var listener = function listener(e) { if (e.origin === checkedOrigin && e.data.token) { window.removeEventListener('message', listener); document.body.removeChild(iframe); resolve(e.data.token); } if (e.origin === checkedOrigin && e.data.type === 'ping') { e.source.postMessage({ type: 'pong' }, e.origin); } }; window.addEventListener('message', listener); document.body.appendChild(iframe); }); }; var singleSignOn = exports.singleSignOn = function singleSignOn(spaceSlug, dimensions) { var target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '_blank'; return new Promise(function (resolve) { var options = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, dimensions), getPopupPosition(window, dimensions)); var endpoint = _helpers.bundle.spaceLocation() + '/app/saml/login/alias/' + spaceSlug; var popup = window.open(endpoint, target, stringifyOptions(options)); if (!popup) { resolve({ error: 'Enterprise Sign In popup was blocked by the browser.' }); return; } // Create an event handler that closes the popup window if we focus the // parent window var windowFocusHandler = function windowFocusHandler() { popup.close(); window.removeEventListener('focus', windowFocusHandler); }; window.addEventListener('focus', windowFocusHandler); // use a larger interval in dev mode because we are going to be checking // by making an ajax call var popupPollingInterval = 2000; var pollCounter = 30; // Check the status of the popup window. If closed or open for too long, // show error. Otherwise, check if profile is avilable to verify successful // authentication. var checkPopup = /*#__PURE__*/function () { var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee() { return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!popup.closed) { _context.next = 4; break; } resolve({ error: 'Enterprise Sign In was cancelled.' }); _context.next = 12; break; case 4: _context.next = 6; return profileAvailable(); case 6: if (!_context.sent) { _context.next = 11; break; } popup.close(); resolve({}); _context.next = 12; break; case 11: if (pollCounter > 0) { pollCounter--; setTimeout(checkPopup, popupPollingInterval); } else { popup.close(); resolve({ error: 'Enterprise Sign In timed out.' }); } case 12: case "end": return _context.stop(); } }, _callee); })); return function checkPopup() { return _ref2.apply(this, arguments); }; }(); // Start the recursive checkPopup calls. setTimeout(checkPopup, popupPollingInterval); }); }; // Checks to see if the user has been authenticated via SSO by checking if the // profile endpoint successfully returns data. var profileAvailable = /*#__PURE__*/function () { var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee3() { return (0, _regeneratorRuntime2["default"])().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: return _context3.abrupt("return", new Promise( /*#__PURE__*/function () { var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee2(resolve) { var result; return (0, _regeneratorRuntime2["default"])().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; _context2.next = 3; return (0, _profile.fetchProfile)({ "public": true }); case 3: result = _context2.sent; resolve(!!result.profile); _context2.next = 10; break; case 7: _context2.prev = 7; _context2.t0 = _context2["catch"](0); resolve(false); case 10: case "end": return _context2.stop(); } }, _callee2, null, [[0, 7]]); })); return function (_x) { return _ref4.apply(this, arguments); }; }())); case 1: case "end": return _context3.stop(); } }, _callee3); })); return function profileAvailable() { return _ref3.apply(this, arguments); }; }(); // window.open takes a string of options rather than a JS object so we use this // helper to do that conversion. var stringifyOptions = function stringifyOptions(options) { return Object.keys(options).reduce(function (reduction, option) { return [].concat((0, _toConsumableArray2["default"])(reduction), ["".concat(option, "=").concat(options[option])]); }, []).join(','); }; // Given the dimensions of the popup and the parent window returns the correct // position for the popup to be centered within the parent. var getPopupPosition = function getPopupPosition(window, _ref5) { var width = _ref5.width, height = _ref5.height; return { top: window.screenY + window.innerHeight / 2 - height / 2, left: window.screenX + window.innerWidth / 2 - width / 2 }; };