Skip to content

Commit e2fcac4

Browse files
committed
fix: cover thrown errors in PKCE tests
1 parent 2d0659f commit e2fcac4

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

lib/grant-types/authorization-code-grant-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class AuthorizationCodeGrantType extends AbstractGrantType {
146146
}
147147

148148
if (!pkce.codeChallengeMatchesABNF(request.body.code_verifier)) {
149-
throw new InvalidRequestError('Invalid parameter: `code_verifier` does not match the ABNF (RFC 7636 §4.1)');
149+
throw new InvalidRequestError('Invalid parameter: `code_verifier`');
150150
}
151151

152152
const hash = pkce.getHashForCodeChallenge({

test/compliance/pkce_test.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const { base64URLEncode } = require('../../lib/utils/string-util');
4545
const { createHash } = require('../../lib/utils/crypto-util');
4646
const { InvalidRequestError } = require('../../index');
4747
const ServerError = require('../../lib/errors/server-error');
48+
const InvalidGrantError = require('../../lib/errors/invalid-grant-error')
4849
require('chai').should();
4950

5051
/**
@@ -194,7 +195,8 @@ describe('PKCE Compliance (RFC 7636)', function () {
194195
await oAuth2Server.token(request, response);
195196
tokenIssued = true;
196197
} catch (e) {
197-
// expected once fixed
198+
e.should.be.instanceOf(InvalidRequestError);
199+
e.message.should.equal('Invalid parameter: `code_verifier`');
198200
}
199201

200202
// This assertion documents the a token IS issued
@@ -222,7 +224,8 @@ describe('PKCE Compliance (RFC 7636)', function () {
222224
await oAuth2Server.token(request, response);
223225
tokenIssued = true;
224226
} catch (e) {
225-
// expected once fixed
227+
e.should.be.instanceOf(InvalidRequestError);
228+
e.message.should.equal('Invalid parameter: `code_verifier`');
226229
}
227230

228231
if (tokenIssued) {
@@ -247,7 +250,8 @@ describe('PKCE Compliance (RFC 7636)', function () {
247250
await oAuth2Server.token(request, response);
248251
tokenIssued = true;
249252
} catch (e) {
250-
// expected once fixed
253+
e.should.be.instanceOf(InvalidRequestError);
254+
e.message.should.equal('Invalid parameter: `code_verifier`');
251255
}
252256

253257
if (tokenIssued) {
@@ -272,7 +276,8 @@ describe('PKCE Compliance (RFC 7636)', function () {
272276
await oAuth2Server.token(request, response);
273277
tokenIssued = true;
274278
} catch (e) {
275-
// expected once fixed
279+
e.should.be.instanceOf(InvalidRequestError);
280+
e.message.should.equal('Invalid parameter: `code_verifier`');
276281
}
277282

278283
if (tokenIssued) {
@@ -315,10 +320,15 @@ describe('PKCE Compliance (RFC 7636)', function () {
315320
const badRequest = tokenRequest(code.authorizationCode, 'a');
316321
const badResponse = new Response();
317322

323+
// before
324+
const codeExists = db.authorizationCodes.has(code.authorizationCode);
325+
codeExists.should.equal(true, 'Precondition failed: seeded authorization code should exist in DB');
326+
318327
try {
319328
await oAuth2Server.token(badRequest, badResponse);
320329
} catch (e) {
321-
// Expected: invalid_grant because hash doesn't match
330+
e.should.be.instanceOf(InvalidRequestError);
331+
e.message.should.equal('Invalid parameter: `code_verifier`');
322332
}
323333

324334
// After a failed PKCE attempt the authorization code should have
@@ -382,10 +392,12 @@ describe('PKCE Compliance (RFC 7636)', function () {
382392
try {
383393
await oAuth2Server.token(wrongRequest, wrongResponse);
384394
} catch (e) {
385-
// expected failure
395+
// Wrong verifier rejected
396+
e.should.be.instanceOf(InvalidGrantError);
397+
e.message.should.equal('Invalid grant: code verifier is invalid');
386398
}
387399

388-
// Attempt 2: correct verifier should fail if code was revoked
400+
// Attempt 2: correct verifier but should fail because code was revoked
389401
const correctRequest = tokenRequest(code.authorizationCode, validVerifier);
390402
const correctResponse = new Response();
391403

@@ -397,7 +409,9 @@ describe('PKCE Compliance (RFC 7636)', function () {
397409
tokenIssued = true;
398410
}
399411
} catch (e) {
400-
// This is the correct behaviour after fix: code was revoked
412+
// This is the correct behaviour after fix: code was revoked and is invalid now
413+
e.should.be.instanceOf(InvalidGrantError);
414+
e.message.should.equal('Invalid grant: authorization code is invalid');
401415
}
402416

403417
if (tokenIssued) {
@@ -473,6 +487,8 @@ describe('PKCE Compliance (RFC 7636)', function () {
473487
tokenIssued = true;
474488
} catch (e) {
475489
// would be expected if plain were rejected
490+
e.should.be.instanceOf(InvalidRequestError);
491+
e.message.should.equal('Invalid request: `code_challenge_method` "plain" is not allowed; use "S256"');
476492
}
477493

478494
// Note: accepting "plain" is RFC 7636-compliant (§4.3 says the

0 commit comments

Comments
 (0)