This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoauthbase-mongo.js
69 lines (59 loc) · 2.06 KB
/
oauthbase-mongo.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
/* vim: set sw=2 ts=2 nocin si: */
var mongoose = require("mongoose"), utils = require("./utils");
mongoose.connect('mongodb://localhost/net9-auth');
mongoose.model('OAuthCode', new mongoose.Schema({
code: { type: String, index: true },
expiredate: Date,
username: String,
redirect_uri: String,
clientid: String,
scope: String
}));
var Code = mongoose.model('OAuthCode');
exports.getCode = function (code, callback) {
Code.findOne({ code: code }, function (err, code) {
if (code === null) callback(false, 'no-such-code');
else callback(true, code.toObject());
});
};
exports.upsertCode = function (codeinfo, callback) {
Code.findOne({ code: codeinfo.code }, function (err, code) {
var newCode = code === null ? new Code(codeinfo) : utils.merge(code, codeinfo);
newCode.save(function (err) {
if (err) callback(false, err);
else callback(true, newCode.toObject());
});
});
};
exports.deleteCode = function (code, callback) {
Code.remove({ code: code }, function (err) {
callback(err ? false : true, err);
});
};
mongoose.model('AccessToken', new mongoose.Schema({
accesstoken: { type: String, index: true },
expiredate: Date,
username: String,
clientid: String,
scope: String
}));
var AccessToken = mongoose.model('AccessToken');
exports.getAccessToken = function (token, callback) {
AccessToken.findOne({ accesstoken: token }, function (err, token) {
if (token === null) callback(false, 'no-such-token');
else callback(true, token.toObject());
});
};
exports.upsertAccessToken = function (tokeninfo, callback) {
// Delete excess object keys, or mongoose will vomit
delete tokeninfo.code;
delete tokeninfo.redirect_uri;
// Now do the upserting.
AccessToken.findOne({ accesstoken: tokeninfo.accesstoken }, function (err, token) {
var newToken = token === null ? new AccessToken(tokeninfo) : utils.merge(token, tokeninfo);
newToken.save(function (err) {
if (err) callback(false, err);
else callback(true, newToken.toObject());
});
});
};