Skip to content

Commit 3242fc2

Browse files
committed
save code challenge with authorization code
1 parent 6bafe0e commit 3242fc2

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ declare namespace OAuth2Server {
306306
*
307307
*/
308308
saveAuthorizationCode(
309-
code: Pick<AuthorizationCode, 'authorizationCode' | 'expiresAt' | 'redirectUri' | 'scope'>,
309+
code: Pick<AuthorizationCode, 'authorizationCode' | 'expiresAt' | 'redirectUri' | 'scope' | 'codeChallenge' | 'codeChallengeMethod'>,
310310
client: Client,
311311
user: User,
312312
callback?: Callback<AuthorizationCode>): Promise<AuthorizationCode | Falsey>;
@@ -410,6 +410,8 @@ declare namespace OAuth2Server {
410410
scope?: string | string[] | undefined;
411411
client: Client;
412412
user: User;
413+
codeChallenge?: string;
414+
codeChallengeMethod?: string;
413415
[key: string]: any;
414416
}
415417

lib/handlers/authorize-handler.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ AuthorizeHandler.prototype.handle = function(request, response) {
114114
})
115115
.then(function(authorizationCode) {
116116
ResponseType = this.getResponseType(request);
117+
const codeChallenge = this.getCodeChallenge(request);
118+
const codeChallengeMethod = this.getCodeChallengeMethod(request);
117119

118-
return this.saveAuthorizationCode(authorizationCode, expiresAt, scope, client, uri, user);
120+
return this.saveAuthorizationCode(authorizationCode, expiresAt, scope, client, uri, user, codeChallenge, codeChallengeMethod);
119121
})
120122
.then(function(code) {
121123
const responseType = new ResponseType(code.authorizationCode);
@@ -293,12 +295,14 @@ AuthorizeHandler.prototype.getRedirectUri = function(request, client) {
293295
* Save authorization code.
294296
*/
295297

296-
AuthorizeHandler.prototype.saveAuthorizationCode = function(authorizationCode, expiresAt, scope, client, redirectUri, user) {
298+
AuthorizeHandler.prototype.saveAuthorizationCode = function(authorizationCode, expiresAt, scope, client, redirectUri, user, codeChallenge, codeChallengeMethod) {
297299
const code = {
298300
authorizationCode: authorizationCode,
299301
expiresAt: expiresAt,
300302
redirectUri: redirectUri,
301-
scope: scope
303+
scope: scope,
304+
codeChallenge: codeChallenge,
305+
codeChallengeMethod: codeChallengeMethod
302306
};
303307
return promisify(this.model.saveAuthorizationCode, 3).call(this.model, code, client, user);
304308
};
@@ -369,6 +373,14 @@ AuthorizeHandler.prototype.updateResponse = function(response, redirectUri, stat
369373
response.redirect(url.format(redirectUri));
370374
};
371375

376+
AuthorizeHandler.prototype.getCodeChallenge = function(request) {
377+
return request.body.code_challenge || request.query.code_challenge;
378+
};
379+
380+
AuthorizeHandler.prototype.getCodeChallengeMethod = function(request) {
381+
return request.body.code_challenge_method || request.query.code_challenge_method;
382+
};
383+
372384
/**
373385
* Export constructor.
374386
*/

test/unit/handlers/authorize-handler_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ describe('AuthorizeHandler', function() {
8787
};
8888
const handler = new AuthorizeHandler({ authorizationCodeLifetime: 120, model: model });
8989

90-
return handler.saveAuthorizationCode('foo', 'bar', 'qux', 'biz', 'baz', 'boz')
90+
return handler.saveAuthorizationCode('foo', 'bar', 'qux', 'biz', 'baz', 'boz', 'codeChallenge', 'codeChallengeMethod')
9191
.then(function() {
9292
model.saveAuthorizationCode.callCount.should.equal(1);
9393
model.saveAuthorizationCode.firstCall.args.should.have.length(3);
94-
model.saveAuthorizationCode.firstCall.args[0].should.eql({ authorizationCode: 'foo', expiresAt: 'bar', redirectUri: 'baz', scope: 'qux' });
94+
model.saveAuthorizationCode.firstCall.args[0].should.eql({ authorizationCode: 'foo', expiresAt: 'bar', redirectUri: 'baz', scope: 'qux', codeChallenge: 'codeChallenge', codeChallengeMethod: 'codeChallengeMethod' });
9595
model.saveAuthorizationCode.firstCall.args[1].should.equal('biz');
9696
model.saveAuthorizationCode.firstCall.args[2].should.equal('boz');
9797
model.saveAuthorizationCode.firstCall.thisValue.should.equal(model);

0 commit comments

Comments
 (0)