-
Notifications
You must be signed in to change notification settings - Fork 3
/
default_db_tokens.js
68 lines (52 loc) · 1.4 KB
/
default_db_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*jshint node:true */
'use strict';
var db = []
, _ = require('lodash')
, serializer = require('serializer').createSecureSerializer('crypt_key', 'sign_key');
function DefaultDBTokens(token, user_id, client_id) {
this.token = token;
this.user_id = user_id;
this.client_id = client_id;
}
DefaultDBTokens.prototype = {
save: function(cb) {
var item = _.pick(this, function(value) {
return !_.isFunction(value);
});
db.push(item);
process.nextTick(function() {
if (cb) { cb(null, item); }
});
}
};
DefaultDBTokens.findOneByToken = function(token, cb) {
var res = _.find(db, {token: token});
process.nextTick(function() {
if (res) { return cb(null, res); }
return cb(null, false);
});
};
DefaultDBTokens.count = function(cb) {
process.nextTick(function() {
cb(null, db.length);
});
};
DefaultDBTokens.createByParams = function(user_id, client_id, cb) {
this.count(function(err, count) {
var token = serializer.stringify([user_id, client_id, +new Date(), count]);
var item = new DefaultDBTokens(token, user_id, client_id);
process.nextTick(function() {
item.save(cb);
});
});
};
DefaultDBTokens.remove = function(token, cb) {
db = _.reject(db, {token: token});
process.nextTick(function() {
cb(null, true);
});
};
DefaultDBTokens.list = function() {
return db;
};
module.exports = DefaultDBTokens;