-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcantina-tokens.js
46 lines (40 loc) · 1.26 KB
/
cantina-tokens.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
var idgen = require('idgen');
module.exports = function (app) {
app.require('cantina-redis');
app.tokens = {};
/**
* Create a token that will automatically expire.
*
* Supported options are:
* - `expire` (key TTL, in milliseconds) (default: 15 minutes)
* - `len` (token length) (default: 16 characters)
* - `prefix`: (required) the redis key prefix (`app_prefix`:tokens:`prefix`:)
*/
app.tokens.create = function (id, options, cb) {
var expire = options.expire || 900000 /* 15 minutes */
, token = idgen(options.len || 16)
, key = app.redisKey('tokens', options.prefix, token);
app.redis.PSETEX(key, expire, id, function (err) {
if (err) return cb(err);
cb(null, token);
});
};
/**
* Check the validity of a token and return the associated id.
*
* `prefix`: the redis key prefix (`app_prefix`:tokens:`prefix`:)
*/
app.tokens.check = function (token, prefix, cb) {
var key = app.redisKey('tokens', prefix, token);
app.redis.GET(key, cb);
};
/**
* Delete a token.
*
* `prefix`: the redis key prefix (`app_prefix`:tokens:`prefix`:)
*/
app.tokens.delete = function (token, prefix, cb) {
var key = app.redisKey('tokens', prefix, token);
app.redis.DEL(key, cb);
};
};