-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
135 lines (110 loc) · 2.91 KB
/
index.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
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
var randomToken = require('random-token-generator'),
redis = require('redis');
var util = require('util');
var RedisToken = module.exports = function(opts) {
this.conn = null;
this.opts = opts = opts || {};
this.opts.prefix = opts.prefix || '';
// Redis options
this.opts.expires = typeof opts.expires !== 'undefined' ? opts.expires : 60;
this.opts.onReady = opts.onReady || function() {};
this.opts.onConnect = opts.onConnect || function() {};
this.opts.onError = opts.onError || function() {};
// Token options
this.opts.strongKey = opts.strongKey || false;
this.opts.tokenLen = opts.tokenLen || 16;
// Data options
this.opts.stringifyData = typeof opts.stringifyData !== 'undefined' ? opts.stringifyData : true;
this.opts.singleUse = typeof opts.singleUse !== 'undefined' ? opts.singleUse : true;
};
RedisToken.prototype.set = function(data, done) {
// Create connection if not already created
this.connect();
this.generateKey(function(err, token) {
if (err) {
return done(err);
}
// Stringify data?
if (this.opts.stringifyData) {
try {
data = JSON.stringify(data);
} catch(e) {
return done(e);
}
}
// Shared done function
var _done = function(err) {
if (err) {
return done(err);
}
done(null, token);
};
// Prefix key
var key = this.opts.prefix + token;
// Expires or not?
if (this.opts.expires) {
this.conn.setex(key, this.opts.expires, data, _done);
} else {
this.conn.set(key, data, _done);
}
}.bind(this));
};
RedisToken.prototype.get = function(token, done) {
// Create connection if not already created
this.connect();
// Prefix key
var token = this.opts.prefix + token;
this.conn.get(token, function(err, data) {
if (err || !data) {
return done(err || new Error('No data found under token'));
}
// Delete if single use
if (this.opts.singleUse) {
this.conn.del(token);
}
// Parse data?
if (this.opts.stringifyData) {
try {
data = JSON.parse(data);
} catch(e) {
return done(e);
}
}
// Successfully done
done(null, data);
}.bind(this));
};
RedisToken.prototype.generateKey = function(done) {
// Create connection if not already created
this.connect();
randomToken.generateKey({
len: this.opts.tokenLen,
strong: this.opts.strongKey
}, function(err, token) {
if (err) {
return done(err);
}
var key = this.prefix + token;
this.conn.exists(key, function(err, exists) {
if (err) {
return done(err);
}
// If it exists try again
if (exists) {
return this.generateKey(done);
}
// Valid key
done(err, token, key);
}.bind(this));
}.bind(this));
};
RedisToken.prototype.connect = function() {
// Create connection if not already created
if (!this.conn) {
if (this.opts.redisUrl) {
this.conn = require('redis-url').connect(this.opts.redisUrl);
} else {
this.conn = redis.createClient(this.opts.redisPort, this.opts.redisHost, this.opts.redisOpts);
}
}
}