SessionsServ.js
2.54 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
var SessionModl = require('../models/SessionModl');
var NomsServ = require('../services/NomsServ');
var SshAuthServ = require('../services/SshAuthServ');
var sessions = {};
sessions.cur = false;
sessions.addData = function (session, cb) {
NomsServ.get(session.login, function (nom) {
if (typeof nom == 'string') {
session.nom = nom;
} else {
session.nom = 'Inconnu';
}
session.canAddMembre = session.login == 'gbontoux';
session.canDelMembre = session.login == 'gbontoux';
cb(session);
});
};
sessions.find = function (id, cb) {
_this = this;
SessionModl.findById(id).lean().exec(function (err, session) {
if (typeof session == 'object') {
_this.addData(session, function (session) {
cb(err, session);
});
} else {
cb(err, null);
}
});
};
sessions.valid = function (session) {
return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date();
};
sessions.delete = function (id, cb) {
SessionModl.remove({
_id: id
}, cb);
};
sessions.verify = function (id, cb) {
_this = this;
_this.find(id, function (err, session) {
if (err) {
cb('error');
} else {
if (session) {
if (sessions.valid(session)) {
cb(null, session);
} else {
cb('expired');
_this.delete(id);
}
} else {
cb('unknown');
}
}
});
};
sessions.use = function (id, cb) {
_this = this;
_this.verify(id, function (err, session) {
if (err) {
cb(err);
} else {
_this.cur = session;
cb(null);
}
});
};
sessions.create = function (login, cb) {
SessionModl.create({
login: login
}, cb);
};
sessions.login = function (data, cb) {
SshAuthServ.verify(data.login, data.pass, cb);
};
sessions.open = function (data, cb) {
_this = this;
_this.login(data, function (err, res) {
if (err) {
cb('error');
} else {
if (res) {
_this.create(data.login, function (err, session) {
if (err) {
cb('error');
} else {
_this.use(session._id, cb);
}
});
} else {
cb('invalid');
}
}
});
};
module.exports = sessions;