-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtoken-handler.js
306 lines (243 loc) · 9.13 KB
/
token-handler.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
305
306
'use strict';
/**
* Module dependencies.
*/
const BearerTokenType = require('../token-types/bearer-token-type');
const InvalidArgumentError = require('../errors/invalid-argument-error');
const InvalidClientError = require('../errors/invalid-client-error');
const InvalidRequestError = require('../errors/invalid-request-error');
const OAuthError = require('../errors/oauth-error');
const Promise = require('bluebird');
const promisify = require('promisify-any').use(Promise);
const Request = require('../request');
const Response = require('../response');
const ServerError = require('../errors/server-error');
const TokenModel = require('../models/token-model');
const UnauthorizedClientError = require('../errors/unauthorized-client-error');
const UnsupportedGrantTypeError = require('../errors/unsupported-grant-type-error');
const auth = require('basic-auth');
const pkce = require('../pkce/pkce');
const isFormat = require('@node-oauth/formats');
/**
* Grant types.
*/
const grantTypes = {
authorization_code: require('../grant-types/authorization-code-grant-type'),
client_credentials: require('../grant-types/client-credentials-grant-type'),
password: require('../grant-types/password-grant-type'),
refresh_token: require('../grant-types/refresh-token-grant-type')
};
/**
* Constructor.
*/
function TokenHandler(options) {
options = options || {};
if (!options.accessTokenLifetime) {
throw new InvalidArgumentError('Missing parameter: `accessTokenLifetime`');
}
if (!options.model) {
throw new InvalidArgumentError('Missing parameter: `model`');
}
if (!options.refreshTokenLifetime) {
throw new InvalidArgumentError('Missing parameter: `refreshTokenLifetime`');
}
if (!options.model.getClient) {
throw new InvalidArgumentError('Invalid argument: model does not implement `getClient()`');
}
this.accessTokenLifetime = options.accessTokenLifetime;
this.grantTypes = Object.assign({}, grantTypes, options.extendedGrantTypes);
this.model = options.model;
this.refreshTokenLifetime = options.refreshTokenLifetime;
this.allowExtendedTokenAttributes = options.allowExtendedTokenAttributes;
this.requireClientAuthentication = options.requireClientAuthentication || {};
this.alwaysIssueNewRefreshToken = options.alwaysIssueNewRefreshToken !== false;
}
/**
* Token Handler.
*/
TokenHandler.prototype.handle = function(request, response) {
if (!(request instanceof Request)) {
throw new InvalidArgumentError('Invalid argument: `request` must be an instance of Request');
}
if (!(response instanceof Response)) {
throw new InvalidArgumentError('Invalid argument: `response` must be an instance of Response');
}
if (request.method !== 'POST') {
return Promise.reject(new InvalidRequestError('Invalid request: method must be POST'));
}
if (!request.is('application/x-www-form-urlencoded')) {
return Promise.reject(new InvalidRequestError('Invalid request: content must be application/x-www-form-urlencoded'));
}
return Promise.bind(this)
.then(function() {
return this.getClient(request, response);
})
.then(function(client) {
return this.handleGrantType(request, client);
})
.tap(function(data) {
const model = new TokenModel(data, {allowExtendedTokenAttributes: this.allowExtendedTokenAttributes});
const tokenType = this.getTokenType(model);
this.updateSuccessResponse(response, tokenType);
}).catch(function(e) {
if (!(e instanceof OAuthError)) {
e = new ServerError(e);
}
this.updateErrorResponse(response, e);
throw e;
});
};
/**
* Get the client from the model.
*/
TokenHandler.prototype.getClient = function(request, response) {
const credentials = this.getClientCredentials(request);
const grantType = request.body.grant_type;
const codeVerifier = request.body.code_verifier;
const isPkce = pkce.isPKCERequest({ grantType, codeVerifier });
if (!credentials.clientId) {
throw new InvalidRequestError('Missing parameter: `client_id`');
}
if (this.isClientAuthenticationRequired(grantType) && !credentials.clientSecret && !isPkce) {
throw new InvalidRequestError('Missing parameter: `client_secret`');
}
if (!isFormat.vschar(credentials.clientId)) {
throw new InvalidRequestError('Invalid parameter: `client_id`');
}
if (credentials.clientSecret && !isFormat.vschar(credentials.clientSecret)) {
throw new InvalidRequestError('Invalid parameter: `client_secret`');
}
return promisify(this.model.getClient, 2).call(this.model, credentials.clientId, credentials.clientSecret)
.then(function(client) {
if (!client) {
throw new InvalidClientError('Invalid client: client is invalid');
}
if (!client.grants) {
throw new ServerError('Server error: missing client `grants`');
}
if (!(client.grants instanceof Array)) {
throw new ServerError('Server error: `grants` must be an array');
}
return client;
})
.catch(function(e) {
// Include the "WWW-Authenticate" response header field if the client
// attempted to authenticate via the "Authorization" request header.
//
// @see https://tools.ietf.org/html/rfc6749#section-5.2.
if ((e instanceof InvalidClientError) && request.get('authorization')) {
response.set('WWW-Authenticate', 'Basic realm="Service"');
throw new InvalidClientError(e, { code: 401 });
}
throw e;
});
};
/**
* Get client credentials.
*
* The client credentials may be sent using the HTTP Basic authentication scheme or, alternatively,
* the `client_id` and `client_secret` can be embedded in the body.
*
* @see https://tools.ietf.org/html/rfc6749#section-2.3.1
*/
TokenHandler.prototype.getClientCredentials = function(request) {
const credentials = auth(request);
const grantType = request.body.grant_type;
const codeVerifier = request.body.code_verifier;
if (credentials) {
return { clientId: credentials.name, clientSecret: credentials.pass };
}
if (request.body.client_id && request.body.client_secret) {
return { clientId: request.body.client_id, clientSecret: request.body.client_secret };
}
if (pkce.isPKCERequest({ grantType, codeVerifier })) {
if(request.body.client_id) {
return { clientId: request.body.client_id };
}
}
if (!this.isClientAuthenticationRequired(grantType)) {
if(request.body.client_id) {
return { clientId: request.body.client_id };
}
}
throw new InvalidClientError('Invalid client: cannot retrieve client credentials');
};
/**
* Handle grant type.
*/
TokenHandler.prototype.handleGrantType = function(request, client) {
const grantType = request.body.grant_type;
if (!grantType) {
throw new InvalidRequestError('Missing parameter: `grant_type`');
}
if (!isFormat.nchar(grantType) && !isFormat.uri(grantType)) {
throw new InvalidRequestError('Invalid parameter: `grant_type`');
}
if (!Object.prototype.hasOwnProperty.call(this.grantTypes, grantType)) {
throw new UnsupportedGrantTypeError('Unsupported grant type: `grant_type` is invalid');
}
if (!Array.isArray(client.grants) || !client.grants.includes(grantType)) {
throw new UnauthorizedClientError('Unauthorized client: `grant_type` is invalid');
}
const accessTokenLifetime = this.getAccessTokenLifetime(client);
const refreshTokenLifetime = this.getRefreshTokenLifetime(client);
const Type = this.grantTypes[grantType];
const options = {
accessTokenLifetime: accessTokenLifetime,
model: this.model,
refreshTokenLifetime: refreshTokenLifetime,
alwaysIssueNewRefreshToken: this.alwaysIssueNewRefreshToken
};
return new Type(options)
.handle(request, client);
};
/**
* Get access token lifetime.
*/
TokenHandler.prototype.getAccessTokenLifetime = function(client) {
return client.accessTokenLifetime || this.accessTokenLifetime;
};
/**
* Get refresh token lifetime.
*/
TokenHandler.prototype.getRefreshTokenLifetime = function(client) {
return client.refreshTokenLifetime || this.refreshTokenLifetime;
};
/**
* Get token type.
*/
TokenHandler.prototype.getTokenType = function(model) {
return new BearerTokenType(model.accessToken, model.accessTokenLifetime, model.refreshToken, model.scope, model.customAttributes);
};
/**
* Update response when a token is generated.
*/
TokenHandler.prototype.updateSuccessResponse = function(response, tokenType) {
response.body = tokenType.valueOf();
response.set('Cache-Control', 'no-store');
response.set('Pragma', 'no-cache');
};
/**
* Update response when an error is thrown.
*/
TokenHandler.prototype.updateErrorResponse = function(response, error) {
response.body = {
error: error.name,
error_description: error.message
};
response.status = error.code;
};
/**
* Given a grant type, check if client authentication is required
*/
TokenHandler.prototype.isClientAuthenticationRequired = function(grantType) {
if (Object.keys(this.requireClientAuthentication).length > 0) {
return (typeof this.requireClientAuthentication[grantType] !== 'undefined') ? this.requireClientAuthentication[grantType] : true;
} else {
return true;
}
};
/**
* Export constructor.
*/
module.exports = TokenHandler;