-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
68 lines (57 loc) · 1.55 KB
/
utils.js
File metadata and controls
68 lines (57 loc) · 1.55 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
var config = require('./config');
var jwt = require('jsonwebtoken');
var MongoClient = require('mongodb').MongoClient;
var url = config.mongoConnectionString;
var secret= config.jwtSecret;
var cassandra = require('cassandra-driver');
var async = require('async');
var cassandraClient = new cassandra.Client({contactPoints: [config.cassandraContactPoint], keyspace: config.cassandraKeyspace
// protocolOptions: {
// port: 9043 //only for testing
// }
});
getSecret=function(req,payload,done){
if(done)
done(null,secret);
else
return secret;
};
getToken=function (req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.query && req.query.token) {
return req.query.token;
}
return null;
};
getUserFromToken=function(req, token){
var token = token || getToken(req);
try {
return jwt.verify(token,getSecret({},{}));
} catch(err){
// console.log(err);
return false;
}
};
getDbConnection=function(){
return MongoClient.connect(url);
};
getCassandraConnection=function () {
return cassandraClient;
};
generateToken=function(user){
return jwt.sign({
sub: user.email,
id: user._id,
}, secret, {
expiresIn: 3*60*60
});
};
module.exports={
getToken:getToken,
getSecret:getSecret,
getDbConnection:getDbConnection,
getCassandraConnection:getCassandraConnection,
generateToken:generateToken,
getUserFromToken:getUserFromToken
}