dbm-graph-api 1.1.37 → 1.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbm-graph-api",
3
- "version": "1.1.37",
3
+ "version": "1.1.38",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -13,7 +13,7 @@
13
13
  "dependencies": {
14
14
  "@aws-sdk/client-s3": "^3.741.0",
15
15
  "@aws-sdk/s3-request-presigner": "^3.741.0",
16
- "dbm": "^1.2.6",
16
+ "dbm": "^1.2.7",
17
17
  "mime": "^4.0.6",
18
18
  "sharp": "^0.33.5",
19
19
  "ws": "^8.18.0"
@@ -299,6 +299,12 @@ export default class WebSocketConnection extends Dbm.core.BaseObject {
299
299
  this._sendData({"type": "currentUser/response", "id": userId, "requestId": data["requestId"]});
300
300
  }
301
301
  break;
302
+ case "user/signOut":
303
+ {
304
+ this.item.setValue("user", null);
305
+ this._sendData({"type": "currentUser/response", "id": 0, "requestId": data["requestId"]});
306
+ }
307
+ break;
302
308
  case "heartbeat":
303
309
  {
304
310
  this._sendData({"type": "heartbeat/response"});
@@ -341,6 +347,8 @@ export default class WebSocketConnection extends Dbm.core.BaseObject {
341
347
  this.item.api.controller.connectionClosed(this);
342
348
  this.item.setValue("api", null);
343
349
  this.item.setValue("controller", null);
350
+
351
+ this.item.setValue("user", null);
344
352
  }
345
353
 
346
354
  addListeners() {
@@ -368,38 +368,74 @@ export const setupEndpoints = function(aServer) {
368
368
  return { success: false, error: "incorrect", message: "Incorrect details"};
369
369
  });
370
370
 
371
- aServer.get('/api/user/me', async function handler (aRequest, aReply) {
372
- let cookies = aRequest.headers.cookie ? aRequest.headers.cookie.split(";") : [];
373
- let currentArray = cookies;
374
- let currentArrayLength = currentArray.length;
375
- for(let i = 0; i < currentArrayLength; i++) {
376
- let [key, value] = currentArray[i].split("=");
377
- if(key === "dbm_session" || key === " dbm_session") {
378
- let userId = 1*value.split(":")[1];
379
- let user = Dbm.getInstance().repository.getItem("graphDatabase").controller.getUser(userId);
380
-
381
- let isValidSession = await user.verifySession(value);
382
- if(isValidSession) {
383
- return {success: true, data: {id: userId}};
371
+ let getPublicSessionIdFomCookie = function(aCookies) {
372
+ if(aCookies) {
373
+ let cookies = aCookies.split(";");
374
+ let currentArray = cookies;
375
+ let currentArrayLength = currentArray.length;
376
+ for(let i = 0; i < currentArrayLength; i++) {
377
+ let [key, value] = currentArray[i].split("=");
378
+ if(key === "dbm_session" || key === " dbm_session") {
379
+ return value;
384
380
  }
385
381
  }
386
382
  }
383
+
384
+ return null;
385
+ }
386
+
387
+ let getUserFromPublicSessionId = async function(aPublicSessionId) {
388
+ if(aPublicSessionId) {
389
+ let userId = 1*aPublicSessionId.split(":")[1];
390
+ let user = Dbm.getRepositoryItem("graphDatabase").controller.getUser(userId);
391
+
392
+ let isValidSession = await user.verifySession(aPublicSessionId);
393
+ if(isValidSession) {
394
+ return user;
395
+ }
396
+ }
397
+
398
+ return null;
399
+ }
400
+
401
+ let getUserFromCookie = async function(aCookies) {
402
+ let publicSessionId = getPublicSessionIdFomCookie(aCookies);
403
+ return await getUserFromPublicSessionId(publicSessionId);
404
+ }
405
+
406
+ aServer.get('/api/user/me', async function handler (aRequest, aReply) {
407
+ let user = await getUserFromCookie(aRequest.headers.cookie);
408
+ if(user) {
409
+ return {success: true, data: {id: user.id}};
410
+ }
387
411
 
388
412
  return {success: false, data: null};
389
413
  });
390
414
 
391
415
  aServer.post('/api/user/logout', async function handler (aRequest, aReply) {
392
- console.log(aRequest.body);
393
416
 
394
- //METODO: clear session from database
395
- //METODO: clear cookie
417
+ let publicSessionId = getPublicSessionIdFomCookie(aRequest.headers.cookie);
418
+ let user = await getUserFromPublicSessionId(publicSessionId);
419
+
420
+ if(user) {
421
+ let sessionId = 1*publicSessionId.split(":")[0];
422
+ await user.deleteSession(sessionId);
423
+ }
424
+
425
+ aReply.header("Set-Cookie", "dbm_session=; Path=/; Max-Age=0; HttpOnly;");
426
+ return {success: true, data: null};
396
427
  });
397
428
 
398
429
  aServer.post('/api/user/renewSession', async function handler (aRequest, aReply) {
399
- console.log(aRequest.body);
430
+ let user = await getUserFromCookie(aRequest.headers.cookie);
431
+ if(user) {
432
+ //METODO: update session
433
+ //METODO: update cookie
434
+
435
+ //METODO: return success
436
+ }
400
437
 
401
- //METODO: clear session from database
402
- //METODO: clear cookie
438
+ return {success: false, data: null};
403
439
  });
404
440
 
405
441
  aServer.get('/api/url', async function handler (aRequest, aReply) {