-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.js
More file actions
74 lines (60 loc) · 1.86 KB
/
session.js
File metadata and controls
74 lines (60 loc) · 1.86 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
var sqlite3 = require('sqlite3').verbose();
var file = 'db.sqlite3';
var db = new sqlite3.Database(file);
module.exports = {
session: {'WEB': {}, 'CELL': {}},
timeouts: {'WEB': {}, 'CELL': {}},
jars: {},
data: {},
tokens: {},
hash: function (username, password, usertype){
return new Buffer(username+'/'+password + '/' + usertype).toString('base64')
},
die_session: function(session_id, usertype){
this.timeouts[usertype][session_id] = setTimeout(function(session_id) {
delete this.session[usertype][session_id];
}.bind(this), 20000, session_id);
},
login: function (session_id, username, password, usertype, callback) {
db.serialize(function() {
password = this.hash(username, password, usertype);
db.all("SELECT * FROM user WHERE username = '" + username + "' AND password = '" + password + "'", function(err, rows) {
if (rows.length > 0) {
this.session[usertype][session_id] = rows[0];
this.die_session(session_id, usertype);
callback(true);
}else{
callback(false);
}
}.bind(this));
}.bind(this));
},
add_jar: function(session_id, cookieJar){
this.jars[session_id] = cookieJar;
},
get_jar: function(session_id){
return this.jars[session_id];
},
add_data: function(session_id, data){
this.data[session_id] = data;
},
get_data: function(session_id){
return this.data[session_id];
},
add_token: function(session_id, token){
this.tokens[token] = session_id;
},
get_token: function(token){
return this.tokens[token];
},
clear: function (session_id, usertype){
clearTimeout(this.timeouts[usertype][session_id]);
this.die_session(session_id, usertype);
},
get_session: function (session_id, usertype) {
return this.session[usertype][session_id];
},
set_value: function(session_id, key, value, usertype){
this.session[usertype][session_id][key] = value;
}
};