This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.js
110 lines (102 loc) · 2.82 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*global require, exports, applicationContext */
'use strict';
const _ = require('underscore');
const joi = require('joi');
const crypto = require('org/arangodb/crypto');
const Foxx = require('org/arangodb/foxx');
const errors = require('./errors');
const cfg = applicationContext.configuration;
const Session = Foxx.Model.extend({
schema: {
uid: joi.string().allow(null).default(null),
sessionData: joi.object().default(Object, 'Empty object'),
userData: joi.object().default(Object, 'Empty object'),
created: joi.number().integer().default(Date.now, 'Current date'),
lastAccess: joi.number().integer().default(Date.now, 'Current date'),
lastUpdate: joi.number().integer().default(Date.now, 'Current date')
}
});
function createSession(sessionData) {
let session = new Session({
sessionData: sessionData || {}
});
return session;
}
Session.fromClient = function (sid) {
const now = Date.now();
let session = new Session();
try {
const data = crypto.jwtDecode(cfg.jwtAlgorithm === 'none' ? null : cfg.jwtSecret, sid);
session.set({
uid: data.uid,
sessionData: data.sessionData,
userData: data.userData,
lastAccess: data.luat,
lastUpdate: data.lmat,
created: data.iat
});
} catch (e) {
throw new errors.SessionNotFound();
}
session.set('lastAccess', now);
session.enforceTimeout();
return session;
};
_.extend(Session.prototype, {
forClient: function () {
const data = {
uid: this.get('uid'),
sessionData: this.get('sessionData'),
userData: this.get('userData'),
luat: this.get('lastAccess'),
lmat: this.get('lastUpdate'),
iat: this.get('created')
};
const secret = cfg.jwtAlgorithm === 'none' ? null : cfg.jwtSecret;
const sid = crypto.jwtEncode(secret, data, cfg.jwtAlgorithm);
return sid;
},
enforceTimeout: function () {
if (this.hasExpired()) {
throw new errors.SessionExpired();
}
},
hasExpired: function () {
return this.getTTL() === 0;
},
getTTL: function () {
if (!cfg.timeToLive) {
return Infinity;
}
return Math.max(0, this.getExpiry() - Date.now());
},
getExpiry: function () {
if (!cfg.timeToLive) {
return Infinity;
}
let prop = cfg.ttlType;
if (!prop || !this.get(prop)) {
prop = 'created';
}
return this.get(prop) + cfg.timeToLive;
},
setUser: function (user) {
if (user) {
this.set('uid', user.get('_id'));
this.set('userData', user.get('userData'));
} else {
delete this.attributes.uid;
this.set('userData', {});
}
return this;
},
save: function () {
const now = Date.now();
this.set('lastAccess', now);
this.set('lastUpdate', now);
return this;
}
});
exports.create = createSession;
exports.get = Session.fromClient;
exports.errors = errors;