Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (91 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
107 lines (91 loc) · 2.05 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
/**
* Module dependencies
*/
var debug = require('simple-debug')('consulate-authcode-simple-secrets-redis')
, redis = require('redis')
, redisurl = require('redisurl');
/**
* Defines
*/
var TEN_MINUTES_S = 10 * 60 * 60
, PREFIX = 'authcode';
/**
* Expose AuthcodeRedis
*/
module.exports = AuthcodeRedis;
/**
* Create an AuthcodeRedis connection
*
* @param {Object} options
* @api public
*/
function AuthcodeRedis (options) {
if (!(this instanceof AuthcodeRedis)) return new AuthcodeRedis(options);
// Defaults
options = options || {};
this.conn = redisurl(options.url);
this.ttl = options.ttl || TEN_MINUTES_S;
this.prefix = options.prefix || PREFIX;
debug('using options', {ttl: this.ttl, prefix: this.prefix});
};
/**
* Save an auth code
*
* @param {String} code
* @param {Function} cb
* @return {AuthcodeRedis}
* @api public
*/
AuthcodeRedis.prototype.save = function(code, cb) {
var key = this._key(code);
this.conn.multi()
.set(key, '')
.expire(key, this.ttl)
.exec(function(err) {
debug('event=save_code key="%s" err="%s"', key, err);
cb(err);
});
return this;
};
/**
* Validate an auth code
*
* @param {String} code
* @param {Function} cb
* @return {AuthcodeRedis}
* @api public
*/
AuthcodeRedis.prototype.validate = function(code, cb) {
var key = this._key(code);
this.conn.exists(key, function(err, isValid) {
debug('event=validate_code key="%s" error="%s" valid=%d', key, err, isValid);
cb(err, !!isValid);
});
return this;
};
/**
* Invalidate an auth code
*
* @param {String} code
* @param {Function} cb
* @return {AuthcodeRedis}
* @api public
*/
AuthcodeRedis.prototype.invalidate = function(code, cb) {
var key = this._key(code);
this.conn.del(key, function(err) {
debug('event=invalidate_code key="%s" error="%s"', key, err);
cb(err);
});
return this;
};
/**
* Create a redis key from a code
*
* @param {String} code
* @return {String}
* @api private
*/
AuthcodeRedis.prototype._key = function(code) {
return [this.prefix, code].join(':');
};