This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessions.js
More file actions
142 lines (118 loc) · 4.94 KB
/
sessions.js
File metadata and controls
142 lines (118 loc) · 4.94 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
This file is part of the Smart Developer Hub Project:
http://www.smartdeveloperhub.org/
Center for Open Middleware
http://www.centeropenmiddleware.com/
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
Copyright (C) 2015 Center for Open Middleware.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
*/
'use strict';
try {
/* Default enviroment vars */
if (!process.env.SESSION_INFO_URL) {
log.warn("Enviroment SESSION_INFO_URL not found");
process.env.SESSION_INFO_URL = "ldap://demo.smartdeveloperhub.org:9010";
}
if (!process.env.SESSION_INFO_BINDDN) {
log.warn("Enviroment SESSION_INFO_BINDDN not found");
process.env.SESSION_INFO_BINDDN = 'cn=admin,dc=ldap,dc=smartdeveloperhub,dc=org';
}
if (!process.env.SESSION_INFO_BINDCREDENTIALS) {
log.warn("Enviroment SESSION_INFO_BINDCREDENTIALS not found");
process.env.SESSION_INFO_BINDCREDENTIALS = '<password>';
}
if (!process.env.SESSION_INFO_SEARCHBASE) {
log.warn("Enviroment SESSION_INFO_SEARCHBASE not found");
process.env.SESSION_INFO_SEARCHBASE = 'cn=users,dc=ldap,dc=smartdeveloperhub,dc=org';
}
if (!process.env.SESSION_INFO_SEARCHFILTER) {
log.warn("Enviroment SESSION_INFO_SEARCHFILTER not found");
process.env.SESSION_INFO_SEARCHFILTER = '(uid={{username}})';
}
if (!process.env.SESSION_DURATION) {
log.warn("Enviroment SESSION_DURATION not found");
process.env.SESSION_DURATION = 3600000;
}
if (!process.env.SESSION_GARBAGE_COLLECTOR) {
log.warn("Enviroment SESSION_GARBAGE_COLLECTOR not found");
process.env.SESSION_GARBAGE_COLLECTOR = 600000;
}
// PRIVATE METHODS
var hasExpired = function hasExpired(session) {
return session['info']['lastAccess'] + process.env.SESSION_DURATION < Date.now();
};
// PUBLIC METHODS
var getSession = function getSession(token, checkExpiration) {
var session = GLOBAL.SESSIONS[token];
if(session == null) {
return null;
}
//Check expiration. If it has expired delete the session
if (checkExpiration && hasExpired(session)) {
deleteSession(token);
return null;
}
return session;
};
var deleteSession = function deleteSession(token) {
delete GLOBAL.SESSIONS[token];
};
var touch = function touch(token) {
GLOBAL.SESSIONS[token]['info']['lastAccess'] = Date.now();
};
var getSessionInfo = function getSessionInfo(token, checkExpiration) {
var session = getSession(token, checkExpiration);
return session == null ? null : session['info'];
};
var getSessionData = function getSessionData(token, checkExpiration) {
var session = getSession(token, checkExpiration);
return session == null ? null : session['data'];
};
//Get the authentication token of the given request
var getToken = function(req) {
if (req.headers && req.headers.authorization) {
var parts = req.headers.authorization.split(' ');
if (parts.length == 2) {
var scheme = parts[0]
, credentials = parts[1];
if (/^Bearer$/i.test(scheme)) {
return credentials;
}
}
}
return null;
};
var initialize = function initialize() {
//Create a lobal variable to store sessions
GLOBAL.SESSIONS = {};
// Session garbage collection...Remove expired sessions
setInterval(function() {
for(var token in GLOBAL.SESSIONS) {
getSession(token, true); //et session can check if it has expired
}
}, process.env.SESSION_GARBAGE_COLLECTOR);
};
exports.getSession = getSession;
exports.deleteSession = deleteSession;
exports.touch = touch;
exports.getSessionInfo = getSessionInfo;
exports.getSessionData = getSessionData;
exports.getToken = getToken;
exports.initialize = initialize;
log.debug("session utils OK");
} catch (err){
log.error(" ! Error loading authentication util:");
log.error(err);
}