-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoauth2.js
304 lines (262 loc) · 10.2 KB
/
oauth2.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Module dependencies.
*/
var oauth2orize = require('oauth2orize')
, oauth2orize_ext = require('oauth2orize-openid') // require extentions.
, passport = require('passport')
, login = require('connect-ensure-login')
, db = require('./db')
, utils = require('./utils');
// create OAuth 2.0 server
var server = oauth2orize.createServer();
// Register serialialization and deserialization functions.
//
// When a client redirects a user to user authorization endpoint, an
// authorization transaction is initiated. To complete the transaction, the
// user must authenticate and approve the authorization request. Because this
// may involve multiple HTTP request/response exchanges, the transaction is
// stored in the session.
//
// An application must supply serialization functions, which determine how the
// client object is serialized into the session. Typically this will be a
// simple matter of serializing the client's ID, and deserializing by finding
// the client by ID from the database.
server.serializeClient(function(client, done) {
return done(null, client.id);
});
server.deserializeClient(function(id, done) {
db.clients.find(id, function(err, client) {
if (err) { return done(err); }
return done(null, client);
});
});
// Register supported OpenID Connect 1.0 grant types.
// Implicit Flow
// id_token grant type.
server.grant(oauth2orize_ext.grant.idToken(function(client, user, done){
var id_token;
// Do your lookup/token generation.
// ... id_token =
done(null, id_token);
}));
// 'id_token token' grant type.
server.grant(oauth2orize_ext.grant.idTokenToken(
function(client, user, done){
var token;
// Do your lookup/token generation.
// ... token =
done(null, token);
},
function(client, user, req, done){
var id_token;
// Do your lookup/token generation.
// ... id_token =
done(null, id_token);
}
));
// Hybrid Flow
// 'code id_token' grant type.
server.grant(oauth2orize_ext.grant.codeIdToken(
function(client, redirect_uri, user, done){
var code;
// Do your lookup/token generation.
// ... code =
done(null, code);
},
function(client, user, done){
var id_token;
// Do your lookup/token generation.
// ... id_token =
done(null, id_token);
}
));
// 'code token' grant type.
server.grant(oauth2orize_ext.grant.codeToken(
function(client, user, done){
var token;
// Do your lookup/token generation.
// ... id_token =
done(null, token);
},
function(client, redirect_uri, user, done){
var code;
// Do your lookup/token generation.
// ... code =
done(null, code);
}
));
// 'code id_token token' grant type.
server.grant(oauth2orize_ext.grant.codeIdTokenToken(
function(client, user, done){
var token;
// Do your lookup/token generation.
// ... id_token =
done(null, token);
},
function(client, redirect_uri, user, done){
var code;
// Do your lookup/token generation.
// ... code =
done(null, code);
},
function(client, user, done){
var id_token;
// Do your lookup/token generation.
// ... id_token =
done(null, id_token);
}
));
// Register supported Oauth 2.0 grant types.
//
// OAuth 2.0 specifies a framework that allows users to grant client
// applications limited access to their protected resources. It does this
// through a process of the user granting access, and the client exchanging
// the grant for an access token.
// Grant authorization codes. The callback takes the `client` requesting
// authorization, the `redirectURI` (which is used as a verifier in the
// subsequent exchange), the authenticated `user` granting access, and
// their response, which contains approved scope, duration, etc. as parsed by
// the application. The application issues a code, which is bound to these
// values, and will be exchanged for an access token.
server.grant(oauth2orize.grant.code(function(client, redirectURI, user, ares, done) {
var code = utils.uid(16)
db.authorizationCodes.save(code, client.id, redirectURI, user.id, function(err) {
if (err) { return done(err); }
done(null, code);
});
}));
// Grant implicit authorization. The callback takes the `client` requesting
// authorization, the authenticated `user` granting access, and
// their response, which contains approved scope, duration, etc. as parsed by
// the application. The application issues a token, which is bound to these
// values.
server.grant(oauth2orize.grant.token(function(client, user, ares, done) {
var token = utils.uid(256);
db.accessTokens.save(token, user.id, client.clientId, function(err) {
if (err) { return done(err); }
done(null, token);
});
}));
// Exchange authorization codes for access tokens. The callback accepts the
// `client`, which is exchanging `code` and any `redirectURI` from the
// authorization request for verification. If these values are validated, the
// application issues an access token on behalf of the user who authorized the
// code.
server.exchange(oauth2orize.exchange.code(function(client, code, redirectURI, done) {
db.authorizationCodes.find(code, function(err, authCode) {
if (err) { return done(err); }
if (client.id !== authCode.clientID) { return done(null, false); }
if (redirectURI !== authCode.redirectURI) { return done(null, false); }
var token = utils.uid(256)
db.accessTokens.save(token, authCode.userID, authCode.clientID, function(err) {
if (err) { return done(err); }
done(null, token);
});
});
}));
// Exchange user id and password for access tokens. The callback accepts the
// `client`, which is exchanging the user's name and password from the
// authorization request for verification. If these values are validated, the
// application issues an access token on behalf of the user who authorized the code.
server.exchange(oauth2orize.exchange.password(function(client, username, password, scope, done) {
//Validate the client
db.clients.findByClientId(client.clientId, function(err, localClient) {
if (err) { return done(err); }
if(localClient === null) {
return done(null, false);
}
if(localClient.clientSecret !== client.clientSecret) {
return done(null, false);
}
//Validate the user
db.users.findByUsername(username, function(err, user) {
if (err) { return done(err); }
if(user === null) {
return done(null, false);
}
if(password !== user.password) {
return done(null, false);
}
//Everything validated, return the token
var token = utils.uid(256);
db.accessTokens.save(token, user.id, client.clientId, function(err) {
if (err) { return done(err); }
done(null, token);
});
});
});
}));
// Exchange the client id and password/secret for an access token. The callback accepts the
// `client`, which is exchanging the client's id and password/secret from the
// authorization request for verification. If these values are validated, the
// application issues an access token on behalf of the client who authorized the code.
server.exchange(oauth2orize.exchange.clientCredentials(function(client, scope, done) {
//Validate the client
db.clients.findByClientId(client.clientId, function(err, localClient) {
if (err) { return done(err); }
if(localClient === null) {
return done(null, false);
}
if(localClient.clientSecret !== client.clientSecret) {
return done(null, false);
}
var token = utils.uid(256);
//Pass in a null for user id since there is no user with this grant type
db.accessTokens.save(token, null, client.clientId, function(err) {
if (err) { return done(err); }
done(null, token);
});
});
}));
// user authorization endpoint
//
// `authorization` middleware accepts a `validate` callback which is
// responsible for validating the client making the authorization request. In
// doing so, is recommended that the `redirectURI` be checked against a
// registered value, although security requirements may vary accross
// implementations. Once validated, the `done` callback must be invoked with
// a `client` instance, as well as the `redirectURI` to which the user will be
// redirected after an authorization decision is obtained.
//
// This middleware simply initializes a new authorization transaction. It is
// the application's responsibility to authenticate the user and render a dialog
// to obtain their approval (displaying details about the client requesting
// authorization). We accomplish that here by routing through `ensureLoggedIn()`
// first, and rendering the `dialog` view.
exports.authorization = [
login.ensureLoggedIn(),
server.authorization(function(clientID, redirectURI, done) {
db.clients.findByClientId(clientID, function(err, client) {
if (err) { return done(err); }
// WARNING: For security purposes, it is highly advisable to check that
// redirectURI provided by the client matches one registered with
// the server. For simplicity, this example does not. You have
// been warned.
return done(null, client, redirectURI);
});
}),
function(req, res){
res.render('dialog', { transactionID: req.oauth2.transactionID, user: req.user, client: req.oauth2.client });
}
]
// user decision endpoint
//
// `decision` middleware processes a user's decision to allow or deny access
// requested by a client application. Based on the grant type requested by the
// client, the above grant middleware configured above will be invoked to send
// a response.
exports.decision = [
login.ensureLoggedIn(),
server.decision()
]
// token endpoint
//
// `token` middleware handles client requests to exchange authorization grants
// for access tokens. Based on the grant type being exchanged, the above
// exchange middleware will be invoked to handle the request. Clients must
// authenticate when making requests to this endpoint.
exports.token = [
passport.authenticate(['basic', 'oauth2-client-password'], { session: false }),
server.token(),
server.errorHandler()
]