Skip to content

Commit 31d0681

Browse files
committed
fix: Return status 403 for invalid uaa oauth code
1 parent 4ec625e commit 31d0681

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

api/services/uaaStrategy.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Strategy } = require('passport-oauth2');
1+
const { Strategy, TokenError } = require('passport-oauth2');
22
const { Sequelize } = require('sequelize');
33
const UAAClient = require('../utils/uaaClient');
44
const { Event, UAAIdentity, User } = require('../models');
@@ -25,6 +25,14 @@ function createUAAStrategy(options, verify) {
2525
});
2626
};
2727

28+
strategy.parseErrorResponse = function(body) {
29+
var json = JSON.parse(body);
30+
if (json.error) {
31+
return new TokenError(json.error_description, json.error, json.error_uri, 403);
32+
}
33+
return null;
34+
};
35+
2836
const params = new URLSearchParams();
2937
params.set('redirect', logoutCallbackURL);
3038
params.set('client_id', opts.clientID);

test/api/admin/requests/admin-auth.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ describe('Admin authentication request', () => {
6262
.expect(401);
6363
});
6464

65+
it('returns forbidden with invalid code', () => {
66+
const invalidCode = 'invlaid';
67+
68+
cfUAANock.mockExchangeTokenFailure();
69+
70+
return request(app)
71+
.get(`/admin/auth/uaa/callback?code=${invalidCode}&state=abc123`)
72+
.expect(403);
73+
});
74+
6575
describe('when successful', () => {
6676
const uaaId = 'admin_id_1';
6777
const code = 'code';

test/api/support/cfUAANock.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ function mockExchangeToken(code, accessToken) {
203203
expires_in: 10,
204204
});
205205
}
206+
function mockExchangeTokenFailure() {
207+
const url = new URL(uaaConfig.tokenURL);
208+
209+
return nock(url.origin)
210+
.post(url.pathname)
211+
.reply(403, {
212+
error: new Error('Invalid token'),
213+
});
214+
}
206215

207216
function mockFailedExchange(code) {
208217
const url = new URL(uaaConfig.tokenURL);
@@ -282,6 +291,7 @@ function mockServerErrorStatus(status, path, message, accessToken, method = 'get
282291
module.exports = {
283292
mockAddUserToGroup,
284293
mockUAAAuth,
294+
mockExchangeTokenFailure,
285295
mockFailedExchange,
286296
mockFetchClientToken,
287297
mockFetchGroupId,

0 commit comments

Comments
 (0)