1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getEffectiveJwt = exports.hasAccess = exports.matchAllRoles = exports.matchAnyRole = undefined;
|
7 |
|
8 | var _contains2 = require('ramda/src/contains');
|
9 |
|
10 | var _contains3 = _interopRequireDefault(_contains2);
|
11 |
|
12 | var _any2 = require('ramda/src/any');
|
13 |
|
14 | var _any3 = _interopRequireDefault(_any2);
|
15 |
|
16 | var _jwtDecode = require('jwt-decode');
|
17 |
|
18 | var _jwtDecode2 = _interopRequireDefault(_jwtDecode);
|
19 |
|
20 | var _data = require('data.maybe');
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 |
|
25 | var matchAnyRole = exports.matchAnyRole = function matchAnyRole(roles) {
|
26 | return (0, _any3.default)(function (r) {
|
27 | return (0, _contains3.default)(r, roles);
|
28 | });
|
29 | };
|
30 |
|
31 | var 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 |
|
39 | var 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 |
|
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 |
|
59 | console.log(e);
|
60 | console.log(e.stack);
|
61 |
|
62 | }
|
63 | return false;
|
64 | };
|
65 |
|
66 | var 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 |
|
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 |
|
83 | if (window && window.localStorage && injectedJwt) {
|
84 | window.localStorage.setItem(storageKey, injectedJwt);
|
85 |
|
86 | if (development) console.log('stored jwt', jwt, ' into local storage');
|
87 |
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 | return jwt;
|
93 | };
|
94 |
|
\ | No newline at end of file |