-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathauthorization-code-grant-type.js
More file actions
276 lines (226 loc) · 8.58 KB
/
authorization-code-grant-type.js
File metadata and controls
276 lines (226 loc) · 8.58 KB
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
'use strict';
/*
* Module dependencies.
*/
const crypto = require('crypto');
const AbstractGrantType = require('./abstract-grant-type');
const InvalidArgumentError = require('../errors/invalid-argument-error');
const InvalidGrantError = require('../errors/invalid-grant-error');
const InvalidRequestError = require('../errors/invalid-request-error');
const ServerError = require('../errors/server-error');
const isFormat = require('@node-oauth/formats');
const pkce = require('../pkce/pkce');
/**
* @class
* @classDesc
*/
class AuthorizationCodeGrantType extends AbstractGrantType {
/**
* @constructor
* @param options
*/
constructor(options = {}) {
if (!options.model) {
throw new InvalidArgumentError('Missing parameter: `model`');
}
if (!options.model.getAuthorizationCode) {
throw new InvalidArgumentError('Invalid argument: model does not implement `getAuthorizationCode()`');
}
if (!options.model.revokeAuthorizationCode) {
throw new InvalidArgumentError('Invalid argument: model does not implement `revokeAuthorizationCode()`');
}
if (!options.model.saveToken) {
throw new InvalidArgumentError('Invalid argument: model does not implement `saveToken()`');
}
super(options);
// xxx: plain PKCE is only allowed if explicitly enabled
this.enablePlainPKCE = options.enablePlainPKCE === true;
}
/**
* Handle authorization code grant.
*
* @param request {Request}
* @param client {ClientData}
* @see https://tools.ietf.org/html/rfc6749#section-4.1.3
*/
async handle(request, client) {
if (!request) {
throw new InvalidArgumentError('Missing parameter: `request`');
}
if (!client) {
throw new InvalidArgumentError('Missing parameter: `client`');
}
const code = await this.getAuthorizationCode(request, client);
await this.revokeAuthorizationCode(code);
// xxx: PKCE verification is done after revoking the code,
// so that a failed verification attempt consumes the code and prevents
// online brute-force guessing.
await this.verifyPKCE(request, code);
await this.validateRedirectUri(request, code);
return this.saveToken(code.user, client, code.authorizationCode, code.scope);
}
/**
* Get the authorization code.
* @param request {Request}
* @param client {ClientData}
* @return {Promise<{user}>}
*/
async getAuthorizationCode(request, client) {
if (!request.body.code) {
throw new InvalidRequestError('Missing parameter: `code`');
}
if (!isFormat.vschar(request.body.code)) {
throw new InvalidRequestError('Invalid parameter: `code`');
}
const code = await this.model.getAuthorizationCode(request.body.code);
if (!code) {
throw new InvalidGrantError('Invalid grant: authorization code is invalid');
}
if (!code.client) {
throw new ServerError('Server error: `getAuthorizationCode()` did not return a `client` object');
}
if (!code.user) {
throw new ServerError('Server error: `getAuthorizationCode()` did not return a `user` object');
}
if (code.client.id !== client.id) {
throw new InvalidGrantError('Invalid grant: authorization code is invalid');
}
if (!(code.expiresAt instanceof Date)) {
throw new ServerError('Server error: `expiresAt` must be a Date instance');
}
if (code.expiresAt < new Date()) {
throw new InvalidGrantError('Invalid grant: authorization code has expired');
}
if (code.redirectUri && !isFormat.uri(code.redirectUri)) {
throw new InvalidGrantError('Invalid grant: `redirect_uri` is not a valid URI');
}
return code;
}
/**
* Verify PKCE code_verifier against the stored code_challenge.
*
* This is called from handle() AFTER the authorization code has been
* revoked, so that a failed verification attempt consumes the code
* and prevents online brute-force guessing.
*
* @param request {Request}
* @param code {AuthorizationCodeData}
* @see https://datatracker.ietf.org/doc/html/rfc7636#section-4.6
*/
verifyPKCE(request, code) {
if (code.codeChallenge) {
const method = this.getCodeChallengeMethod(code.codeChallengeMethod);
if (!this.enablePlainPKCE && method === 'plain') {
throw new InvalidRequestError('Invalid request: `code_challenge_method` "plain" is not allowed; use "S256"');
}
if (!request.body.code_verifier) {
throw new InvalidGrantError('Missing parameter: `code_verifier`');
}
if (!pkce.codeChallengeMatchesABNF(request.body.code_verifier)) {
throw new InvalidRequestError('Invalid parameter: `code_verifier`');
}
const hash = pkce.getHashForCodeChallenge({
verifier: request.body.code_verifier,
method
});
if (!hash) {
throw new ServerError('Server error: no valid hash algorithm available to verify `code_verifier`');
}
// xxx: Use timingSafeEqual to prevent against timing attacks when comparing
// the hash of the code_verifier to the stored code_challenge.
if (!this.hashesAreEqual(hash, code.codeChallenge)) {
throw new InvalidGrantError('Invalid grant: code verifier is invalid');
}
}
else {
if (request.body.code_verifier) {
// No code challenge but code_verifier was passed in.
throw new InvalidGrantError('Invalid grant: code verifier is invalid');
}
}
}
hashesAreEqual(trusted, untrusted) {
const trustedBuf = Buffer.isBuffer(trusted) ? trusted : Buffer.from(trusted);
const untrustedBuf = Buffer.isBuffer(untrusted) ? untrusted : Buffer.from(untrusted);
const equalLength = trustedBuf.byteLength === untrustedBuf.byteLength;
// if the buffers are the same length, compare them,
// otherwise only compare with the trusted buffer but return false anyway
return crypto.timingSafeEqual(trustedBuf, equalLength ? untrustedBuf : trustedBuf) && equalLength;
}
getCodeChallengeMethod(method) {
if (method) {
return method;
}
// Per RFC 7636 §4.6, the default code challenge method is "plain".
// However, plain PKCE is not secure, so we only allow it if explicitly enabled.
return this.enablePlainPKCE ? 'plain' : 'S256';
}
/**
* Validate the redirect URI.
*
* "The authorization server MUST ensure that the redirect_uri parameter is
* present if the redirect_uri parameter was included in the initial
* authorization request as described in Section 4.1.1, and if included
* ensure that their values are identical."
* @param request {Request}
* @param code {AuthorizationCodeData}
* @see https://tools.ietf.org/html/rfc6749#section-4.1.3
*/
validateRedirectUri(request, code) {
if (!code.redirectUri) {
return;
}
const redirectUri = request.body.redirect_uri || request.query.redirect_uri;
if (!isFormat.uri(redirectUri)) {
throw new InvalidRequestError('Invalid request: `redirect_uri` is not a valid URI');
}
if (redirectUri !== code.redirectUri) {
throw new InvalidRequestError('Invalid request: `redirect_uri` is invalid');
}
}
/**
* Revoke the authorization code.
*
* "The authorization code MUST expire shortly after it is issued to mitigate
* the risk of leaks. [...] If an authorization code is used more than once,
* the authorization server MUST deny the request."
* @param code {AuthorizationCodeData}
* @see https://tools.ietf.org/html/rfc6749#section-4.1.2
*/
async revokeAuthorizationCode(code) {
const status = await this.model.revokeAuthorizationCode(code);
if (!status) {
throw new InvalidGrantError('Invalid grant: authorization code is invalid');
}
return code;
}
/**
* Save token.
*
* @param user {object}
* @param client {ClientData}
* @param authorizationCode {string}
* @param requestedScope {string}
*
*/
async saveToken(user, client, authorizationCode, requestedScope) {
const validatedScope = await this.validateScope(user, client, requestedScope);
const accessToken = await this.generateAccessToken(client, user, validatedScope);
const refreshToken = await this.generateRefreshToken(client, user, validatedScope);
const accessTokenExpiresAt = await this.getAccessTokenExpiresAt();
const refreshTokenExpiresAt = await this.getRefreshTokenExpiresAt();
const token = {
accessToken,
authorizationCode,
accessTokenExpiresAt,
refreshToken,
refreshTokenExpiresAt,
scope: validatedScope,
};
return this.model.saveToken(token, client, user);
}
}
/**
* Export constructor.
*/
module.exports = AuthorizationCodeGrantType;