UNPKG

11.3 kBJavaScriptView Raw
1"use strict";
2
3/* Copyright 2018 Open Ag Data Alliance
4 *
5 * Licensed under the Apache License, Version 2.0 (the 'License');
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an 'AS IS' BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @author Servio Palacios, Samuel Noel
20 * Token API for Handling Tokens in the Cache Library - Super Class.
21 * @module src/token
22 */
23
24var _regenerator = require("babel-runtime/regenerator");
25
26var _regenerator2 = _interopRequireDefault(_regenerator);
27
28var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
29
30var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
31
32var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
33
34var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
35
36var _createClass2 = require("babel-runtime/helpers/createClass");
37
38var _createClass3 = _interopRequireDefault(_createClass2);
39
40var _pouchdb = require("pouchdb");
41
42var _pouchdb2 = _interopRequireDefault(_pouchdb);
43
44var _http = require("http");
45
46function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
47
48var Promise = require("bluebird");
49
50var urlLib = require("url");
51var _ = require("lodash");
52//const debug = require("debug")("oada-cache:token");
53var crypto = require("crypto");
54var oadaIdClient = require("@oada/oada-id-client");
55
56var 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 // creating database nae based on the domain
67 // ensured one to one correspondence with the domain
68 // i.e., token belongs to that domain
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 } //constructor
79
80 /**
81 * searches for a local db and a doc
82 */
83
84
85 (0, _createClass3.default)(Token, [{
86 key: "checkTokenDB",
87 value: function () {
88 var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_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 // debug("received document ->", doc);
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 }() //checkTokenDB
130
131 /**
132 * if token was provided then it sets the .token in the constructor -> returns that value
133 * sets the pouch db if it does not exist
134 */
135
136 }, {
137 key: "setup",
138 value: function () {
139 var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_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 // Get a token
146 TOKEN = null; //returned to the chache library
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 //local cache does not have a token
170 urlObj = urlLib.parse(this._domain);
171 result = void 0;
172 // Open the browser and the login popup
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 // the library itself detects a browser environment and delivers .browser
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 // debug("setup token -> access token:", result.access_token);
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 }() //setup
218
219 /**
220 * fetches the token from the this._tokenDB or
221 * setups the new database and retrieves the new token to be used
222 */
223
224 }, {
225 key: "get",
226 value: function () {
227 var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_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)( /*#__PURE__*/_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 // get token from local cache
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 }() //put
323
324 }, {
325 key: "renew",
326 value: function () {
327 var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_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)( /*#__PURE__*/_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 //await this._tokenDB.close();
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 }() //cleanUp
387
388 }, {
389 key: "isSet",
390 value: function isSet() {
391 return this._isSet;
392 }
393 }]);
394 return Token;
395}(); //class
396
397/* exporting the module */
398
399
400module.exports = Token;
\No newline at end of file