UNPKG

3.14 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getEffectiveJwt = exports.hasAccess = exports.matchAllRoles = exports.matchAnyRole = undefined;
7
8var _contains2 = require('ramda/src/contains');
9
10var _contains3 = _interopRequireDefault(_contains2);
11
12var _any2 = require('ramda/src/any');
13
14var _any3 = _interopRequireDefault(_any2);
15
16var _jwtDecode = require('jwt-decode');
17
18var _jwtDecode2 = _interopRequireDefault(_jwtDecode);
19
20var _data = require('data.maybe');
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24// TODO: test this
25var matchAnyRole = exports.matchAnyRole = function matchAnyRole(roles) {
26 return (0, _any3.default)(function (r) {
27 return (0, _contains3.default)(r, roles);
28 });
29};
30/* import idx from 'idx' */
31var matchAllRoles = exports.matchAllRoles = function matchAllRoles(roles) {
32 return function (appRoles) {
33 return roles.reduce(function (a, b) {
34 return a && appRoles.includes(b);
35 }, appRoles.includes(roles[0]));
36 };
37};
38
39var hasAccess = exports.hasAccess = function hasAccess(_ref) {
40 var roles = _ref.roles,
41 app = _ref.app,
42 jwt = _ref.jwt,
43 _ref$matchAny = _ref.matchAny,
44 matchAny = _ref$matchAny === undefined ? false : _ref$matchAny,
45 isGodMode = _ref.isGodMode;
46
47 // TODO: add validations here
48 if (isGodMode) return true;
49 try {
50 return (0, _data.fromNullable)(jwt).map(_jwtDecode2.default).chain(function (t) {
51 return (0, _data.fromNullable)(t.roles);
52 }).chain(function (roles) {
53 return (0, _data.fromNullable)(roles[app]);
54 }).map(function (appRoles) {
55 return matchAny && matchAnyRole(roles)(appRoles) || matchAllRoles(roles)(appRoles);
56 }).getOrElse(false);
57 } catch (e) {
58 /* eslint-disable no-console */
59 console.log(e);
60 console.log(e.stack);
61 /* eslint-enable no-console */
62 }
63 return false;
64};
65
66var getEffectiveJwt = exports.getEffectiveJwt = function getEffectiveJwt(storageKey) {
67 var extractJwtFromAddress = function extractJwtFromAddress(href) {
68 return href.substr(href.indexOf('jwt') + 4);
69 };
70
71 var cachedJWt = window && window.localStorage !== null && window.localStorage.getItem(storageKey) ? window.localStorage.getItem(storageKey) : undefined;
72 var injectedJwt = window && window.location && window.location.href && window.location.href.indexOf('jwt') >= 0 ? extractJwtFromAddress(window.location.href) : undefined;
73
74 var jwt = injectedJwt || cachedJWt;
75 /* eslint-disable */
76 console.log('injected jwt-->', injectedJwt);
77 console.log('cached jwt-->', cachedJWt);
78 console.log('final jwt-->', jwt);
79
80 var development = process.env.NODE_ENV === undefined || process.env.NODE_ENV !== 'production';
81
82 // WARNING non-pure
83 if (window && window.localStorage && injectedJwt) {
84 window.localStorage.setItem(storageKey, injectedJwt);
85 //for security reasons m
86 if (development) console.log('stored jwt', jwt, ' into local storage');
87 //window.history.replaceState({}, 'UI', basename ? basename + '/' : '/')
88 }
89
90 /* eslint-enable */
91
92 return jwt;
93};
94//# sourceMappingURL=utils.js.map
\No newline at end of file