-
Notifications
You must be signed in to change notification settings - Fork 66
/
model.js
161 lines (117 loc) · 3.13 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
/**
* Configuration.
*/
var config = {
clients: [{
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: []
}],
confidentialClients: [{
clientId: 'confidentialApplication',
clientSecret: 'topSecret',
grants: [
'password',
'client_credentials'
],
redirectUris: []
}],
tokens: [],
users: [{
username: 'pedroetb',
password: 'password'
}]
};
/**
* Dump the memory storage content (for debug).
*/
var dump = function() {
console.log('clients', config.clients);
console.log('confidentialClients', config.confidentialClients);
console.log('tokens', config.tokens);
console.log('users', config.users);
};
/*
* Methods used by all grant types.
*/
var getAccessToken = function(token) {
var tokens = config.tokens.filter(function(savedToken) {
return savedToken.accessToken === token;
});
return tokens[0];
};
var getClient = function(clientId, clientSecret) {
var clients = config.clients.filter(function(client) {
return client.clientId === clientId && client.clientSecret === clientSecret;
});
var confidentialClients = config.confidentialClients.filter(function(client) {
return client.clientId === clientId && client.clientSecret === clientSecret;
});
return clients[0] || confidentialClients[0];
};
var saveToken = function(token, client, user) {
token.client = {
id: client.clientId
};
token.user = {
username: user.username
};
config.tokens.push(token);
return token;
};
/*
* Method used only by password grant type.
*/
var getUser = function(username, password) {
var users = config.users.filter(function(user) {
return user.username === username && user.password === password;
});
return users[0];
};
/*
* Method used only by client_credentials grant type.
*/
var getUserFromClient = function(client) {
var clients = config.confidentialClients.filter(function(savedClient) {
return savedClient.clientId === client.clientId && savedClient.clientSecret === client.clientSecret;
});
return clients.length;
};
/*
* Methods used only by refresh_token grant type.
*/
var getRefreshToken = function(refreshToken) {
var tokens = config.tokens.filter(function(savedToken) {
return savedToken.refreshToken === refreshToken;
});
if (!tokens.length) {
return;
}
return tokens[0];
};
var revokeToken = function(token) {
config.tokens = config.tokens.filter(function(savedToken) {
return savedToken.refreshToken !== token.refreshToken;
});
var revokedTokensFound = config.tokens.filter(function(savedToken) {
return savedToken.refreshToken === token.refreshToken;
});
return !revokedTokensFound.length;
};
/**
* Export model definition object.
*/
module.exports = {
getAccessToken: getAccessToken,
getClient: getClient,
saveToken: saveToken,
getUser: getUser,
getUserFromClient: getUserFromClient,
getRefreshToken: getRefreshToken,
revokeToken: revokeToken
};