forked from anvilresearch/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifyClient.js
56 lines (45 loc) · 1.27 KB
/
verifyClient.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
/**
* Module dependencies
*/
var Client = require('../models/Client')
, AuthorizationError = require('../errors/AuthorizationError')
;
/**
* Verify Client
*
* This route-specific middleware retrieves a registered client and adds it
* to the request object for use downstream. It verifies that the client is
* registered and that the redirect_uri parameter matches the configuration
* of the registered client.
*/
function verifyClient (req, res, next) {
var params = req.connectParams;
Client.get(params.client_id, {
private: true
}, function (err, client) {
if (err) { return next(err); }
// The client must be registered.
if (!client) {
return next(new AuthorizationError({
error: 'unauthorized_client',
error_description: 'Unknown client',
statusCode: 401
}));
}
// Make client available to downstream middleware.
req.client = client;
// Redirect URI must be configured for this client.
if (client.redirect_uris.indexOf(params.redirect_uri) === -1) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Mismatching redirect uri',
statusCode: 400
}));
}
next();
});
}
/**
* Exports
*/
module.exports = verifyClient;