forked from anvilresearch/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateTokenParams.js
77 lines (61 loc) · 1.6 KB
/
validateTokenParams.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
/**
* Module dependencies
*/
var AuthorizationError = require('../errors/AuthorizationError');
/**
* Supported grant types
*/
var grantTypes = [
'authorization_code', 'refresh_token', 'client_credentials'
];
/**
* Validate token parameters
*/
function validateTokenParams (req, res, next) {
var params = req.body;
// missing grant type
if (!params.grant_type) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Missing grant type',
statusCode: 400
}));
}
// unsupported grant type
if (grantTypes.indexOf(params.grant_type) === -1) {
return next(new AuthorizationError({
error: 'unsupported_grant_type',
error_description: 'Unsupported grant type',
statusCode: 400
}));
}
// missing authorization code
if (params.grant_type === 'authorization_code' && !params.code) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Missing authorization code',
statusCode: 400
}));
}
// missing redirect uri
if (params.grant_type === 'authorization_code' && !params.redirect_uri) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Missing redirect uri',
statusCode: 400
}));
}
// missing refresh token
if (params.grant_type === 'refresh_token' && !params.refresh_token) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Missing refresh token',
statusCode: 400
}));
}
next();
}
/**
* Exports
*/
module.exports = validateTokenParams;