-
Notifications
You must be signed in to change notification settings - Fork 53
/
model.js
244 lines (186 loc) · 4.47 KB
/
model.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var mongoose = require('mongoose');
/**
* Configuration.
*/
var clientModel = require('./mongo/model/client'),
tokenModel = require('./mongo/model/token'),
userModel = require('./mongo/model/user');
/**
* Add example client and user to the database (for debug).
*/
var loadExampleData = function() {
var client1 = new clientModel({
id: 'application', // TODO: Needed by refresh_token grant, because there is a bug at line 103 in https://github.com/oauthjs/node-oauth2-server/blob/v3.0.1/lib/grant-types/refresh-token-grant-type.js (used client.id instead of client.clientId)
clientId: 'application',
clientSecret: 'secret',
grants: [
'password',
'refresh_token'
],
redirectUris: []
});
var client2 = new clientModel({
clientId: 'confidentialApplication',
clientSecret: 'topSecret',
grants: [
'password',
'client_credentials'
],
redirectUris: []
});
var user = new userModel({
username: 'pedroetb',
password: 'password'
});
client1.save(function(err, client) {
if (err) {
return console.error(err);
}
console.log('Created client', client);
});
user.save(function(err, user) {
if (err) {
return console.error(err);
}
console.log('Created user', user);
});
client2.save(function(err, client) {
if (err) {
return console.error(err);
}
console.log('Created client', client);
});
};
/**
* Dump the database content (for debug).
*/
var dump = function() {
clientModel.find(function(err, clients) {
if (err) {
return console.error(err);
}
console.log('clients', clients);
});
tokenModel.find(function(err, tokens) {
if (err) {
return console.error(err);
}
console.log('tokens', tokens);
});
userModel.find(function(err, users) {
if (err) {
return console.error(err);
}
console.log('users', users);
});
};
/*
* Methods used by all grant types.
*/
var getAccessToken = function(token, callback) {
tokenModel.findOne({
accessToken: token
}).lean().exec((function(callback, err, token) {
if (!token) {
console.error('Token not found');
}
callback(err, token);
}).bind(null, callback));
};
var getClient = function(clientId, clientSecret, callback) {
clientModel.findOne({
clientId: clientId,
clientSecret: clientSecret
}).lean().exec((function(callback, err, client) {
if (!client) {
console.error('Client not found');
}
callback(err, client);
}).bind(null, callback));
};
var saveToken = function(token, client, user, callback) {
token.client = {
id: client.clientId
};
token.user = {
username: user.username
};
var tokenInstance = new tokenModel(token);
tokenInstance.save((function(callback, err, token) {
if (!token) {
console.error('Token not saved');
} else {
token = token.toObject();
delete token._id;
delete token.__v;
}
callback(err, token);
}).bind(null, callback));
};
/*
* Method used only by password grant type.
*/
var getUser = function(username, password, callback) {
userModel.findOne({
username: username,
password: password
}).lean().exec((function(callback, err, user) {
if (!user) {
console.error('User not found');
}
callback(err, user);
}).bind(null, callback));
};
/*
* Method used only by client_credentials grant type.
*/
var getUserFromClient = function(client, callback) {
clientModel.findOne({
clientId: client.clientId,
clientSecret: client.clientSecret,
grants: 'client_credentials'
}).lean().exec((function(callback, err, client) {
if (!client) {
console.error('Client not found');
}
callback(err, {
username: ''
});
}).bind(null, callback));
};
/*
* Methods used only by refresh_token grant type.
*/
var getRefreshToken = function(refreshToken, callback) {
tokenModel.findOne({
refreshToken: refreshToken
}).lean().exec((function(callback, err, token) {
if (!token) {
console.error('Token not found');
}
callback(err, token);
}).bind(null, callback));
};
var revokeToken = function(token, callback) {
tokenModel.deleteOne({
refreshToken: token.refreshToken
}).exec((function(callback, err, results) {
var deleteSuccess = results && results.deletedCount === 1;
if (!deleteSuccess) {
console.error('Token not deleted');
}
callback(err, deleteSuccess);
}).bind(null, callback));
};
/**
* Export model definition object.
*/
module.exports = {
getAccessToken: getAccessToken,
getClient: getClient,
saveToken: saveToken,
getUser: getUser,
getUserFromClient: getUserFromClient,
getRefreshToken: getRefreshToken,
revokeToken: revokeToken
};