Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@bmc/react-common / dist / security / AuthManager.js
Size: Mime:
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
// this will act as a caching proxy for the auth.getUserInfo() method
// needs to have an instance of auth - so it knows how to call it to get the user info
// needs to have an event listener for when a user is authenticated or is no longer authenticated to invalidate the cache
// this even listener will need to register with the AuthorizedRoutes for their state change
import { intersection, isArray, isNil } from "lodash";
import * as jwt_decode from "jwt-decode";

var AuthManager =
/*#__PURE__*/
function () {
  function AuthManager(auth) {
    _classCallCheck(this, AuthManager);

    this.authenticated = null;
    this.user = null;
    this.auth = auth;
  }

  _createClass(AuthManager, [{
    key: "isAuthenticated",
    value: function () {
      var _isAuthenticated = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee() {
        var _authn;

        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (!(typeof this.auth !== 'undefined')) {
                  _context.next = 6;
                  break;
                }

                _context.next = 3;
                return this.auth.isAuthenticated();

              case 3:
                _context.t0 = _context.sent;
                _context.next = 7;
                break;

              case 6:
                _context.t0 = null;

              case 7:
                _authn = _context.t0;

                if (!(_authn !== this.authenticated)) {
                  _context.next = 11;
                  break;
                }

                _context.next = 11;
                return this.__update(_authn);

              case 11:
                return _context.abrupt("return", this.authenticated);

              case 12:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function isAuthenticated() {
        return _isAuthenticated.apply(this, arguments);
      }

      return isAuthenticated;
    }()
  }, {
    key: "isAuthorizedFor",
    value: function () {
      var _isAuthorizedFor = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee2(allowedGroups, path) {
        var idToken, _decoded, user_groups, email, authorized;

        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.prev = 0;

                if (!(isNil(allowedGroups) || !isArray(allowedGroups))) {
                  _context2.next = 4;
                  break;
                }

                console.error('Tried to check authorization with an undefined or a non-array value of allowed groups.');
                return _context2.abrupt("return", false);

              case 4:
                _context2.next = 6;
                return this.auth.getIdToken();

              case 6:
                idToken = _context2.sent;
                _decoded = this.__decode(idToken);
                user_groups = _decoded['groups'];
                email = _decoded['email'];

                if (isNil(user_groups)) {
                  _context2.next = 14;
                  break;
                }

                authorized = intersection(allowedGroups, user_groups).length > 0;

                if (!authorized) {
                  console.warn("User [".concat(email, "] does not have the group [").concat(allowedGroups.toString(), "] in their assigned groups for [").concat(path, "]"));
                }

                return _context2.abrupt("return", authorized);

              case 14:
                return _context2.abrupt("return", false);

              case 17:
                _context2.prev = 17;
                _context2.t0 = _context2["catch"](0);
                console.warn(_context2.t0);
                return _context2.abrupt("return", false);

              case 21:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this, [[0, 17]]);
      }));

      function isAuthorizedFor(_x, _x2) {
        return _isAuthorizedFor.apply(this, arguments);
      }

      return isAuthorizedFor;
    }()
  }, {
    key: "__decode",
    value: function __decode(idToken) {
      return jwt_decode(idToken);
    }
  }, {
    key: "__update",
    value: function () {
      var _update = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee3(authenticated) {
        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                if (authenticated !== this.authenticated) {
                  this.authenticated = authenticated;
                  this.user = null;
                }

                if (!(this.authenticated && isNil(this.user))) {
                  _context3.next = 10;
                  break;
                }

                if (!isNil(this.auth)) {
                  _context3.next = 6;
                  break;
                }

                _context3.t0 = null;
                _context3.next = 9;
                break;

              case 6:
                _context3.next = 8;
                return this.auth.getUser();

              case 8:
                _context3.t0 = _context3.sent;

              case 9:
                this.user = _context3.t0;

              case 10:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function __update(_x3) {
        return _update.apply(this, arguments);
      }

      return __update;
    }()
  }, {
    key: "__getEmail",
    value: function () {
      var _getEmail = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee4() {
        return _regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                if (!isNil(this.user)) {
                  _context4.next = 3;
                  break;
                }

                console.warn("The user is undefined, unable to retrieve email.");
                return _context4.abrupt("return", undefined);

              case 3:
                return _context4.abrupt("return", this.user.email);

              case 4:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      function __getEmail() {
        return _getEmail.apply(this, arguments);
      }

      return __getEmail;
    }()
  }, {
    key: "__getGroups",
    value: function () {
      var _getGroups = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee5() {
        return _regeneratorRuntime.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                if (!isNil(this.user)) {
                  _context5.next = 3;
                  break;
                }

                console.warn("The user is undefined, unable to retrieve email.");
                return _context5.abrupt("return", undefined);

              case 3:
                if (!isNil(this.user.groups)) {
                  _context5.next = 8;
                  break;
                }

                // console.warn('User has groups are undefined. Check the Okta application configuration.');
                console.warn('User has groups are undefined. Check the Okta application configuration.');
                return _context5.abrupt("return", undefined);

              case 8:
                if (isArray(this.user.groups)) {
                  _context5.next = 11;
                  break;
                }

                console.warn('User has no groups. Check Okta to verify the user is in application groups.');
                return _context5.abrupt("return", undefined);

              case 11:
                return _context5.abrupt("return", this.user.groups);

              case 12:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));

      function __getGroups() {
        return _getGroups.apply(this, arguments);
      }

      return __getGroups;
    }()
  }]);

  return AuthManager;
}();

export { AuthManager as default };
//# sourceMappingURL=AuthManager.js.map