forked from anvilresearch/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateAuthorizationParams.js
153 lines (127 loc) · 3.62 KB
/
validateAuthorizationParams.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
/**
* Module dependencies
*/
var crypto = require('crypto')
, AuthorizationError = require('../errors/AuthorizationError')
;
/**
* Supported response types
*/
var responseTypes = [
'code', // authorization code flow
'code token', // hybrid flow
'code id_token', // hybrid flow
'id_token', // implicit flow
'token id_token', // implicit flow
'id_token token', // implicit flow
'code id_token token' // hybrid flow
];
/**
* Supported response modes
*/
var responseModes = [
'query',
'fragment'
];
/**
* Validate Authorization Parameters
*
* Ensures that `response_type`, `redirect_uri`, and `client_id` are included in
* request parameters, and that `response_type` is supported.
*/
function validateAuthorizationParams (req, res, next) {
var params = req.connectParams;
// missing redirect uri
if (!params.redirect_uri) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Missing redirect uri',
statusCode: 400
}));
}
// invalid redirect uri
//if (!params.redirect_uri) { // HOW SHOULD WE VALIDATE THIS?
// return next(new AuthorizationError({
// error: 'invalid_request',
// error_description: 'Invalid redirect uri',
// statusCode: 400
// }));
//}
// missing response type
if (!params.response_type) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Missing response type',
redirect_uri: params.redirect_uri,
statusCode: 302
}));
}
// unsupported response type
if (responseTypes.indexOf(params.response_type) === -1) {
return next(new AuthorizationError({
error: 'unsupported_response_type',
error_description: 'Unsupported response type',
redirect_uri: params.redirect_uri,
statusCode: 302
}));
}
// unsupported response mode
if (params.response_mode
&& responseModes.indexOf(params.response_mode) === -1) {
return next(new AuthorizationError({
error: 'unsupported_response_mode',
error_description: 'Unsupported response mode',
redirect_uri: params.redirect_uri,
statusCode: 302
}));
}
// missing client id
if (!params.client_id) {
return next(new AuthorizationError({
error: 'unauthorized_client',
error_description: 'Missing client id',
statusCode: 403
}));
}
// missing scope
if (!params.scope) {
return next(new AuthorizationError({
error: 'invalid_scope',
error_description: 'Missing scope',
redirect_uri: params.redirect_uri,
statusCode: 302
}));
}
// missing openid scope
if (params.scope.indexOf('openid') === -1) {
return next(new AuthorizationError({
error: 'invalid_scope',
error_description: 'Missing openid scope',
redirect_uri: params.redirect_uri,
statusCode: 302
}));
}
// missing nonce
if (requiresNonce(params.response_type) && !params.nonce) {
return next(new AuthorizationError({
error: 'invalid_request',
error_description: 'Missing nonce',
redirect_uri: params.redirect_uri,
statusCode: 302
}));
}
next();
}
/**
* Check if a nonce is required
*/
function requiresNonce (responseType) {
return (['id_token', 'id_token token'].indexOf(responseType) !== -1)
? true
: false
;
}
/**
* Exports
*/
module.exports = validateAuthorizationParams;