Skip to content

Commit

Permalink
fix: reject oauthCallback when id_token is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Dec 3, 2021
1 parent fb6a141 commit 92ffee5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,14 @@ class BaseClient {
throw new OPError(params);
}

if ('id_token' in params) {
throw new RPError({
message:
'id_token detected in the response, you must use client.callback() instead of client.oauthCallback()',
params,
});
}

const RESPONSE_TYPE_REQUIRED_PARAMS = {
code: ['code'],
token: ['access_token', 'token_type'],
Expand Down Expand Up @@ -608,6 +616,14 @@ class BaseClient {
{ clientAssertionPayload, DPoP },
);

if ('id_token' in tokenset) {
throw new RPError({
message:
'id_token detected in the response, you must use client.callback() instead of client.oauthCallback()',
params,
});
}

if (tokenset.scope && checks.scope && this.fapi()) {
const expected = new Set(checks.scope.split(' '));
const actual = tokenset.scope.split(' ');
Expand Down
38 changes: 38 additions & 0 deletions test/client/client_instance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,44 @@ describe('Client', () => {
});
});

describe('cannot be used for id_token responses', function () {
it('rejects when id_token was issued by the authorization endpoint', function () {
return this.client
.oauthCallback('https://rp.example.com/cb', {
code: 'foo',
id_token: 'foo',
})
.then(fail, (error) => {
expect(error).to.be.instanceof(Error);
expect(error).to.have.property(
'message',
'id_token detected in the response, you must use client.callback() instead of client.oauthCallback()',
);
});
});

it('rejects when id_token was issued by the token endpoint', function () {
nock('https://op.example.com')
.matchHeader('Accept', 'application/json')
.matchHeader('Content-Length', isNumber)
.matchHeader('Transfer-Encoding', isUndefined)
.post('/token')
.reply(200, { id_token: 'foo' });

return this.client
.oauthCallback('https://rp.example.com/cb', {
code: 'foo',
})
.then(fail, (error) => {
expect(error).to.be.instanceof(Error);
expect(error).to.have.property(
'message',
'id_token detected in the response, you must use client.callback() instead of client.oauthCallback()',
);
});
});
});

describe('response type checks', function () {
it('rejects with an Error when code is missing', function () {
return this.client
Expand Down

0 comments on commit 92ffee5

Please sign in to comment.