1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.TokenStorage = undefined;
|
5 |
|
6 | var _typeof2 = require('babel-runtime/helpers/typeof');
|
7 |
|
8 | var _typeof3 = _interopRequireDefault(_typeof2);
|
9 |
|
10 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
11 |
|
12 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
13 |
|
14 | exports.authEndpoint = authEndpoint;
|
15 | exports.clearAuthTokens = clearAuthTokens;
|
16 |
|
17 | var _queryParse = require('./util/query-parse');
|
18 |
|
19 | var _queryParse2 = _interopRequireDefault(_queryParse);
|
20 |
|
21 | var _Observable = require('rxjs/Observable');
|
22 |
|
23 | var _map = require('rxjs/operator/map');
|
24 |
|
25 | var _fetch = require('./util/fetch.js');
|
26 |
|
27 | var _fetch2 = _interopRequireDefault(_fetch);
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | var HORIZON_JWT = 'horizon-jwt';
|
32 |
|
33 |
|
34 | function authEndpoint(name) {
|
35 | var _this = this;
|
36 |
|
37 | var endpointForName = function endpointForName(methods) {
|
38 | if (methods.hasOwnProperty(name)) {
|
39 | return methods[name];
|
40 | } else {
|
41 | throw new Error('Unconfigured auth type: ' + name);
|
42 | }
|
43 | };
|
44 | if (!this._authMethods) {
|
45 | var _context;
|
46 |
|
47 | console.log('No auth methods, have to fetch');
|
48 | return (_context = (0, _fetch2.default)(this._horizonPath + '/auth_methods').do(function (authMethods) {
|
49 | _this._authMethods = authMethods;
|
50 | }), _map.map).call(_context, endpointForName);
|
51 | } else {
|
52 | var _context2;
|
53 |
|
54 | return (_context2 = _Observable.Observable.of(this._authMethods), _map.map).call(_context2, endpointForName);
|
55 | }
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 | var FakeStorage = function () {
|
61 | function FakeStorage() {
|
62 | (0, _classCallCheck3.default)(this, FakeStorage);
|
63 | this._storage = new Map();
|
64 | }
|
65 |
|
66 | FakeStorage.prototype.setItem = function setItem(a, b) {
|
67 | return this._storage.set(a, b);
|
68 | };
|
69 |
|
70 | FakeStorage.prototype.getItem = function getItem(a) {
|
71 | return this._storage.get(a);
|
72 | };
|
73 |
|
74 | FakeStorage.prototype.removeItem = function removeItem(a) {
|
75 | return this._storage.delete(a);
|
76 | };
|
77 |
|
78 | return FakeStorage;
|
79 | }();
|
80 |
|
81 | function getStorage() {
|
82 | try {
|
83 | if ((typeof window === 'undefined' ? 'undefined' : (0, _typeof3.default)(window)) !== 'object' || window.localStorage === undefined) {
|
84 | return new FakeStorage();
|
85 | }
|
86 | window.localStorage.setItem('$$fake', 1);
|
87 | window.localStorage.removeItem('$$fake');
|
88 | return window.localStorage;
|
89 | } catch (error) {
|
90 | if (window.sessionStorage === undefined) {
|
91 | return new FakeStorage();
|
92 | } else {
|
93 | return window.sessionStorage;
|
94 | }
|
95 | }
|
96 | }
|
97 |
|
98 | var TokenStorage = exports.TokenStorage = function () {
|
99 | function TokenStorage() {
|
100 | var authType = arguments.length <= 0 || arguments[0] === undefined ? 'unauthenticated' : arguments[0];
|
101 | (0, _classCallCheck3.default)(this, TokenStorage);
|
102 |
|
103 | this._storage = getStorage();
|
104 | this._authType = authType;
|
105 | }
|
106 |
|
107 | TokenStorage.prototype.set = function set(jwt) {
|
108 | return this._storage.setItem(HORIZON_JWT, jwt);
|
109 | };
|
110 |
|
111 | TokenStorage.prototype.get = function get() {
|
112 | return this._storage.getItem(HORIZON_JWT);
|
113 | };
|
114 |
|
115 | TokenStorage.prototype.remove = function remove() {
|
116 | return this._storage.removeItem(HORIZON_JWT);
|
117 | };
|
118 |
|
119 | TokenStorage.prototype.setAuthFromQueryParams = function setAuthFromQueryParams() {
|
120 | var parsed = (0, _queryParse2.default)(window.location.search);
|
121 | if (parsed.horizon_auth != null) {
|
122 | this.set(parsed.horizon_auth);
|
123 | }
|
124 | };
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | TokenStorage.prototype.handshake = function handshake() {
|
130 |
|
131 |
|
132 | var token = this.get();
|
133 | if (token != null) {
|
134 | return { method: 'token', token: token };
|
135 | } else if (this._authType === 'token') {
|
136 | throw new Error('Attempting to authenticate with a token, but no token is present');
|
137 | } else {
|
138 | return { method: this._authType };
|
139 | }
|
140 | };
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | TokenStorage.prototype.hasAuthToken = function hasAuthToken() {
|
146 | return Boolean(this.get());
|
147 | };
|
148 |
|
149 | return TokenStorage;
|
150 | }();
|
151 |
|
152 | function clearAuthTokens() {
|
153 | return getStorage().removeItem(HORIZON_JWT);
|
154 | }
|
155 |
|
\ | No newline at end of file |