1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var _regenerator = require("babel-runtime/regenerator");
|
25 |
|
26 | var _regenerator2 = _interopRequireDefault(_regenerator);
|
27 |
|
28 | var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
|
29 |
|
30 | var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
31 |
|
32 | var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
|
33 |
|
34 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
35 |
|
36 | var _createClass2 = require("babel-runtime/helpers/createClass");
|
37 |
|
38 | var _createClass3 = _interopRequireDefault(_createClass2);
|
39 |
|
40 | var _pouchdb = require("pouchdb");
|
41 |
|
42 | var _pouchdb2 = _interopRequireDefault(_pouchdb);
|
43 |
|
44 | var _http = require("http");
|
45 |
|
46 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
47 |
|
48 | var Promise = require("bluebird");
|
49 |
|
50 | var urlLib = require("url");
|
51 | var _ = require("lodash");
|
52 |
|
53 | var crypto = require("crypto");
|
54 | var oadaIdClient = require("@oada/oada-id-client");
|
55 |
|
56 | var Token = function () {
|
57 | function Token() {
|
58 | var param = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
59 | (0, _classCallCheck3.default)(this, Token);
|
60 |
|
61 | var self = this;
|
62 | self._token = param.token || null;
|
63 | self._domain = param.domain || "localhost";
|
64 | self._options = param.options;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | var hash = crypto.createHash("sha256");
|
70 | hash.update(self._domain);
|
71 | self._name = hash.digest("hex");
|
72 |
|
73 | self._isSet = self._token ? true : false;
|
74 | self._tokenDB = new _pouchdb2.default(this._name);
|
75 | self._id = "OadaTokenID";
|
76 | self._rev = null;
|
77 | self.token = self._token ? self._token : "";
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | (0, _createClass3.default)(Token, [{
|
86 | key: "checkTokenDB",
|
87 | value: function () {
|
88 | var _ref = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee() {
|
89 | var result, doc;
|
90 | return _regenerator2.default.wrap(function _callee$(_context) {
|
91 | while (1) {
|
92 | switch (_context.prev = _context.next) {
|
93 | case 0:
|
94 | result = null;
|
95 | _context.prev = 1;
|
96 | _context.next = 4;
|
97 | return this._tokenDB.get(this._id);
|
98 |
|
99 | case 4:
|
100 | doc = _context.sent;
|
101 |
|
102 |
|
103 | result = doc.token;
|
104 | this._rev = doc._rev;
|
105 | _context.next = 12;
|
106 | break;
|
107 |
|
108 | case 9:
|
109 | _context.prev = 9;
|
110 | _context.t0 = _context["catch"](1);
|
111 | return _context.abrupt("return", result);
|
112 |
|
113 | case 12:
|
114 | return _context.abrupt("return", result);
|
115 |
|
116 | case 13:
|
117 | case "end":
|
118 | return _context.stop();
|
119 | }
|
120 | }
|
121 | }, _callee, this, [[1, 9]]);
|
122 | }));
|
123 |
|
124 | function checkTokenDB() {
|
125 | return _ref.apply(this, arguments);
|
126 | }
|
127 |
|
128 | return checkTokenDB;
|
129 | }()
|
130 |
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 | }, {
|
137 | key: "setup",
|
138 | value: function () {
|
139 | var _ref2 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee2(_expired) {
|
140 | var TOKEN, urlObj, result, gat;
|
141 | return _regenerator2.default.wrap(function _callee2$(_context2) {
|
142 | while (1) {
|
143 | switch (_context2.prev = _context2.next) {
|
144 | case 0:
|
145 |
|
146 | TOKEN = null;
|
147 |
|
148 | if (!this.isSet()) {
|
149 | _context2.next = 5;
|
150 | break;
|
151 | }
|
152 |
|
153 | TOKEN = this.token;
|
154 | _context2.next = 23;
|
155 | break;
|
156 |
|
157 | case 5:
|
158 | _context2.next = 7;
|
159 | return this.checkTokenDB();
|
160 |
|
161 | case 7:
|
162 | TOKEN = _context2.sent;
|
163 |
|
164 | if (!(!TOKEN || _expired)) {
|
165 | _context2.next = 23;
|
166 | break;
|
167 | }
|
168 |
|
169 |
|
170 | urlObj = urlLib.parse(this._domain);
|
171 | result = void 0;
|
172 |
|
173 |
|
174 | if (!(typeof window === "undefined")) {
|
175 | _context2.next = 17;
|
176 | break;
|
177 | }
|
178 |
|
179 | _context2.next = 14;
|
180 | return oadaIdClient.node(urlObj.host, this._options);
|
181 |
|
182 | case 14:
|
183 | result = _context2.sent;
|
184 | _context2.next = 21;
|
185 | break;
|
186 |
|
187 | case 17:
|
188 |
|
189 | gat = Promise.promisify(oadaIdClient.getAccessToken);
|
190 | _context2.next = 20;
|
191 | return gat(urlObj.host, this._options);
|
192 |
|
193 | case 20:
|
194 | result = _context2.sent;
|
195 |
|
196 | case 21:
|
197 | TOKEN = result.access_token;
|
198 |
|
199 | this.put(TOKEN);
|
200 |
|
201 | case 23:
|
202 | return _context2.abrupt("return", TOKEN);
|
203 |
|
204 | case 24:
|
205 | case "end":
|
206 | return _context2.stop();
|
207 | }
|
208 | }
|
209 | }, _callee2, this);
|
210 | }));
|
211 |
|
212 | function setup(_x2) {
|
213 | return _ref2.apply(this, arguments);
|
214 | }
|
215 |
|
216 | return setup;
|
217 | }()
|
218 |
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 | }, {
|
225 | key: "get",
|
226 | value: function () {
|
227 | var _ref3 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee3() {
|
228 | return _regenerator2.default.wrap(function _callee3$(_context3) {
|
229 | while (1) {
|
230 | switch (_context3.prev = _context3.next) {
|
231 | case 0:
|
232 | return _context3.abrupt("return", this.setup());
|
233 |
|
234 | case 1:
|
235 | case "end":
|
236 | return _context3.stop();
|
237 | }
|
238 | }
|
239 | }, _callee3, this);
|
240 | }));
|
241 |
|
242 | function get() {
|
243 | return _ref3.apply(this, arguments);
|
244 | }
|
245 |
|
246 | return get;
|
247 | }()
|
248 |
|
249 | /**
|
250 | * searches for the token in the this._tokenDB
|
251 | * if present, the it sends the current _rev
|
252 | * if not present (404), it creates a new document in the created this._tokenDB
|
253 | * @param {string} _token
|
254 | */
|
255 |
|
256 | }, {
|
257 | key: "put",
|
258 | value: function () {
|
259 | var _ref4 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee4(_token) {
|
260 | var TOKEN, response, _response;
|
261 |
|
262 | return _regenerator2.default.wrap(function _callee4$(_context4) {
|
263 | while (1) {
|
264 | switch (_context4.prev = _context4.next) {
|
265 | case 0:
|
266 |
|
267 | TOKEN = this.checkTokenDB();
|
268 | _context4.prev = 1;
|
269 |
|
270 | if (!TOKEN) {
|
271 | _context4.next = 9;
|
272 | break;
|
273 | }
|
274 |
|
275 | _context4.next = 5;
|
276 | return this._tokenDB.put({
|
277 | _id: this._id,
|
278 | _rev: this._rev,
|
279 | token: _token
|
280 | });
|
281 |
|
282 | case 5:
|
283 | response = _context4.sent;
|
284 |
|
285 | this.token = _token;
|
286 | _context4.next = 13;
|
287 | break;
|
288 |
|
289 | case 9:
|
290 | _context4.next = 11;
|
291 | return this._tokenDB.put({
|
292 | _id: this._id,
|
293 | token: _token
|
294 | });
|
295 |
|
296 | case 11:
|
297 | _response = _context4.sent;
|
298 |
|
299 | this.token = _token;
|
300 |
|
301 | case 13:
|
302 | _context4.next = 17;
|
303 | break;
|
304 |
|
305 | case 15:
|
306 | _context4.prev = 15;
|
307 | _context4.t0 = _context4["catch"](1);
|
308 |
|
309 | case 17:
|
310 | case "end":
|
311 | return _context4.stop();
|
312 | }
|
313 | }
|
314 | }, _callee4, this, [[1, 15]]);
|
315 | }));
|
316 |
|
317 | function put(_x3) {
|
318 | return _ref4.apply(this, arguments);
|
319 | }
|
320 |
|
321 | return put;
|
322 | }()
|
323 |
|
324 | }, {
|
325 | key: "renew",
|
326 | value: function () {
|
327 | var _ref5 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee5() {
|
328 | return _regenerator2.default.wrap(function _callee5$(_context5) {
|
329 | while (1) {
|
330 | switch (_context5.prev = _context5.next) {
|
331 | case 0:
|
332 | this._isSet = false;
|
333 | return _context5.abrupt("return", this.setup(true));
|
334 |
|
335 | case 2:
|
336 | case "end":
|
337 | return _context5.stop();
|
338 | }
|
339 | }
|
340 | }, _callee5, this);
|
341 | }));
|
342 |
|
343 | function renew() {
|
344 | return _ref5.apply(this, arguments);
|
345 | }
|
346 |
|
347 | return renew;
|
348 | }()
|
349 | }, {
|
350 | key: "cleanUp",
|
351 | value: function () {
|
352 | var _ref6 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee6() {
|
353 | return _regenerator2.default.wrap(function _callee6$(_context6) {
|
354 | while (1) {
|
355 | switch (_context6.prev = _context6.next) {
|
356 | case 0:
|
357 | _context6.prev = 0;
|
358 | _context6.next = 3;
|
359 | return this._tokenDB.destroy();
|
360 |
|
361 | case 3:
|
362 |
|
363 | this._isSet = false;
|
364 | _context6.next = 9;
|
365 | break;
|
366 |
|
367 | case 6:
|
368 | _context6.prev = 6;
|
369 | _context6.t0 = _context6["catch"](0);
|
370 |
|
371 | console.log("deleting token from cache", _context6.t0);
|
372 |
|
373 | case 9:
|
374 | case "end":
|
375 | return _context6.stop();
|
376 | }
|
377 | }
|
378 | }, _callee6, this, [[0, 6]]);
|
379 | }));
|
380 |
|
381 | function cleanUp() {
|
382 | return _ref6.apply(this, arguments);
|
383 | }
|
384 |
|
385 | return cleanUp;
|
386 | }()
|
387 |
|
388 | }, {
|
389 | key: "isSet",
|
390 | value: function isSet() {
|
391 | return this._isSet;
|
392 | }
|
393 | }]);
|
394 | return Token;
|
395 | }();
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | module.exports = Token; |
\ | No newline at end of file |