Skip to content

Commit b799985

Browse files
committed
test(pkce): added test for bypassed saving unsupported code challenge method
1 parent 2411f92 commit b799985

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

test/unit/grant-types/authorization-code-grant-type_test.js

+28
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const AuthorizationCodeGrantType = require('../../../lib/grant-types/authorization-code-grant-type');
88
const InvalidGrantError = require('../../../lib/errors/invalid-grant-error');
9+
const ServerError = require('../../../lib/errors/server-error');
910
const Promise = require('bluebird');
1011
const Request = require('../../../lib/request');
1112
const sinon = require('sinon');
@@ -119,6 +120,33 @@ describe('AuthorizationCodeGrantType', function() {
119120
});
120121
});
121122

123+
it('should throw an error in getAuthorizationCode if an invalid code challenge method has been saved', function () {
124+
const codeVerifier = stringUtil.base64URLEncode(crypto.randomBytes(32));
125+
const authorizationCode = {
126+
authorizationCode: 12345,
127+
client: { id: 'foobar', isPublic: true },
128+
expiresAt: new Date(new Date().getTime() * 2),
129+
user: {},
130+
codeChallengeMethod: 'foobar', // assume this bypassed validation
131+
codeChallenge: stringUtil.base64URLEncode(crypto.createHash('sha256').update(codeVerifier).digest())
132+
};
133+
const client = { id: 'foobar', isPublic: true };
134+
const model = {
135+
getAuthorizationCode: function() { return authorizationCode; },
136+
revokeAuthorizationCode: function() {},
137+
saveToken: function() {}
138+
};
139+
const grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
140+
const request = new Request({ body: { code: 12345, code_verifier: codeVerifier }, headers: {}, method: {}, query: {} });
141+
142+
return grantType.getAuthorizationCode(request, client)
143+
.then(should.fail)
144+
.catch(function(e) {
145+
e.should.be.an.instanceOf(ServerError);
146+
e.message.should.equal('Server error: `getAuthorizationCode()` did not return a valid `codeChallengeMethod` property');
147+
});
148+
});
149+
122150
it('should throw an error if the `code_verifier` is invalid with plain code challenge method', function() {
123151
const codeVerifier = stringUtil.base64URLEncode(crypto.randomBytes(32));
124152
const authorizationCode = {

0 commit comments

Comments
 (0)