-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauthModel.js
More file actions
116 lines (89 loc) · 1.88 KB
/
Copy pathoauthModel.js
File metadata and controls
116 lines (89 loc) · 1.88 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
108
109
110
111
112
113
114
115
116
var mongoose = require('mongoose');
/**
* Configuration.
*/
var clientModel = require('./api/models/client'),
tokenModel = require('./api/models/token'),
userModel = require('./api/models/user');
/**
* Dump the database content (for debug).
*/
var dump = function () {
clientModel.find(function (err, clients) {
if (err) {
c
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) {
return tokenModel.findOne({
accessToken: token
});
};
var getClient = function (clientId, clientSecret) {
console.log(clientId + clientSecret);
result = clientModel.findOne({
clientId: clientId,
clientSecret: clientSecret
});
console.log(result);
return result;
};
var saveToken = function (token, client, user) {
token.client = {
id: client.clientId
};
token.user = {
id: user.username || user.clientId
};
var tokenInstance = new tokenModel(token);
console.log("save");
tokenInstance.save();
return token;
};
/*
* Method used only by password grant type.
*/
var getUser = function (username, password) {
return userModel.findOne({
username: username,
password: password
});
};
/*
* Method used only by client_credentials grant type.
*/
var getUserFromClient = function (client) {
return clientModel.findOne({
clientId: client.clientId,
clientSecret: client.clientSecret,
grants: 'client_credentials'
});
};
/**
* Export model definition object.
*/
module.exports = {
getAccessToken: getAccessToken,
getClient: getClient,
saveToken: saveToken,
getUser: getUser,
getUserFromClient: getUserFromClient
};