UNPKG

4.26 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.TokenStorage = undefined;
5
6var _typeof2 = require('babel-runtime/helpers/typeof');
7
8var _typeof3 = _interopRequireDefault(_typeof2);
9
10var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
11
12var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
13
14exports.authEndpoint = authEndpoint;
15exports.clearAuthTokens = clearAuthTokens;
16
17var _queryParse = require('./util/query-parse');
18
19var _queryParse2 = _interopRequireDefault(_queryParse);
20
21var _Observable = require('rxjs/Observable');
22
23var _map = require('rxjs/operator/map');
24
25var _fetch = require('./util/fetch.js');
26
27var _fetch2 = _interopRequireDefault(_fetch);
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31var HORIZON_JWT = 'horizon-jwt';
32
33/** @this Horizon **/
34function 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// Simple shim to make a Map look like local/session storage
59
60var 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
81function 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
98var 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 // Handshake types are implemented here
127
128
129 TokenStorage.prototype.handshake = function handshake() {
130 // If we have a token, we should send it rather than requesting a
131 // new one
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 // Whether there is an auth token for the provided authType
143
144
145 TokenStorage.prototype.hasAuthToken = function hasAuthToken() {
146 return Boolean(this.get());
147 };
148
149 return TokenStorage;
150}();
151
152function clearAuthTokens() {
153 return getStorage().removeItem(HORIZON_JWT);
154}
155//# sourceMappingURL=auth.js.map
\No newline at end of file