Skip to content

Commit

Permalink
[FEATURE] Conditionner la validation des AT utilisateurs en comparant…
Browse files Browse the repository at this point in the history
… l’aud des AT utilisateurs à la forwardedOrigin (PIX-15944)

 #11208
  • Loading branch information
pix-service-auto-merge authored Jan 29, 2025
2 parents d4bd36d + ad449de commit 3f9179c
Show file tree
Hide file tree
Showing 189 changed files with 1,176 additions and 1,418 deletions.
32 changes: 21 additions & 11 deletions api/lib/infrastructure/authentication.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boom from '@hapi/boom';
import lodash from 'lodash';

import { getForwardedOrigin } from '../../src/identity-access-management/infrastructure/utils/network.js';
import { config } from '../../src/shared/config.js';
import { tokenService } from '../../src/shared/domain/services/token-service.js';

Expand Down Expand Up @@ -60,7 +61,6 @@ function validateUser(decoded) {

function validateClientApplication(decoded) {
const application = find(config.apimRegisterApplicationsCredentials, { clientId: decoded.client_id });

if (!application) {
return { isValid: false, errorCode: 401 };
}
Expand All @@ -73,29 +73,39 @@ function validateClientApplication(decoded) {
}

async function _checkIsAuthenticated(request, h, { key, validate }) {
if (!request.headers.authorization) {
const authorizationHeader = request.headers.authorization;
if (!authorizationHeader) {
return boom.unauthorized(null, 'jwt');
}

const authorizationHeader = request.headers.authorization;
const accessToken = tokenService.extractTokenFromAuthChain(authorizationHeader);

if (!accessToken) {
return boom.unauthorized();
}

const decodedAccessToken = tokenService.getDecodedToken(accessToken, key);
if (decodedAccessToken) {
const { isValid, credentials, errorCode } = validate(decodedAccessToken, request, h);
if (isValid) {
return h.authenticated({ credentials });
}
if (!decodedAccessToken) {
return boom.unauthorized();
}

if (errorCode === 403) {
return boom.forbidden();
// Only tokens including user_id are User Access Tokens.
// This is why applications Access Tokens are not subject to audience validation for now.
if (decodedAccessToken.user_id && config.featureToggles.isUserTokenAudConfinementEnabled) {
const audience = getForwardedOrigin(request.headers);
if (decodedAccessToken.aud !== audience) {
return boom.unauthorized();
}
}

const { isValid, credentials, errorCode } = validate(decodedAccessToken, request, h);
if (isValid) {
return h.authenticated({ credentials });
}

if (errorCode === 403) {
return boom.forbidden();
}

return boom.unauthorized();
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/shared/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ const configuration = (function () {
config.featureToggles.isSelfAccountDeletionEnabled = false;
config.featureToggles.isQuestEnabled = false;
config.featureToggles.isAsyncQuestRewardingCalculationEnabled = false;
config.featureToggles.isUserTokenAudConfinementEnabled = false;
config.featureToggles.isUserTokenAudConfinementEnabled = true;
config.featureToggles.isTextToSpeechButtonEnabled = false;
config.featureToggles.isLegalDocumentsVersioningEnabled = false;
config.featureToggles.showNewResultPage = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createServer,
databaseBuilder,
expect,
generateValidRequestAuthorizationHeader,
generateAuthenticatedUserRequestHeaders,
} from '../../../test-helper.js';

describe('Acceptance | API | Campaign Participations', function () {
Expand All @@ -29,7 +29,7 @@ describe('Acceptance | API | Campaign Participations', function () {
const options = {
method: 'GET',
url: `/api/campaign-participations/${campaignParticipation.id}/trainings`,
headers: { authorization: generateValidRequestAuthorizationHeader(user.id) },
headers: generateAuthenticatedUserRequestHeaders({ userId: user.id }),
};

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createServer,
databaseBuilder,
expect,
generateValidRequestAuthorizationHeader,
generateAuthenticatedUserRequestHeaders,
} from '../../../test-helper.js';

describe('Acceptance | API | Campaign Controller', function () {
Expand All @@ -25,7 +25,7 @@ describe('Acceptance | API | Campaign Controller', function () {
const response = await server.inject({
method: 'PATCH',
url: `/api/campaigns/${campaign.id}`,
headers: { authorization: generateValidRequestAuthorizationHeader(userId) },
headers: generateAuthenticatedUserRequestHeaders({ userId }),
payload: {
data: {
type: 'campaigns',
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Acceptance | API | Campaign Controller', function () {
const response = await server.inject({
method: 'PATCH',
url: `/api/campaigns/${campaign.id}`,
headers: { authorization: generateValidRequestAuthorizationHeader(userId) },
headers: generateAuthenticatedUserRequestHeaders({ userId }),
payload: {
data: {
type: 'campaigns',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createServer,
databaseBuilder,
expect,
generateValidRequestAuthorizationHeader,
generateAuthenticatedUserRequestHeaders,
} from '../../../test-helper.js';

describe('Acceptance | API | Campaign Management Controller', function () {
Expand All @@ -23,7 +23,7 @@ describe('Acceptance | API | Campaign Management Controller', function () {
const response = await server.inject({
method: 'GET',
url: `/api/admin/campaigns/${campaign.id}`,
headers: { authorization: generateValidRequestAuthorizationHeader(user.id) },
headers: generateAuthenticatedUserRequestHeaders({ userId: user.id }),
});

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createServer,
databaseBuilder,
expect,
generateValidRequestAuthorizationHeader,
generateAuthenticatedUserRequestHeaders,
knex,
} from '../../../test-helper.js';

Expand Down Expand Up @@ -45,9 +45,7 @@ describe('Acceptance | API | Certification center invitations', function () {
const response = await server.inject({
method: 'PATCH',
url: `/api/certification-center-invitations/${certificationCenterInvitation.id}`,
headers: {
authorization: generateValidRequestAuthorizationHeader(adminUser.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: adminUser.id }),
});

// then
Expand Down Expand Up @@ -98,9 +96,7 @@ describe('Acceptance | API | Certification center invitations', function () {
const response = await server.inject({
method: 'PATCH',
url: `/api/certification-center-invitations/${certificationCenterInvitation.id}`,
headers: {
authorization: generateValidRequestAuthorizationHeader(user.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: user.id }),
});

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
createServer,
databaseBuilder,
expect,
generateValidRequestAuthorizationHeader,
generateAuthenticatedUserRequestHeaders,
insertUserWithRoleSuperAdmin,
} from '../../../test-helper.js';

Expand Down Expand Up @@ -40,9 +40,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
const request = {
method: 'DELETE',
url: `/api/admin/certification-center-memberships/${certificationCenterMembership.id}`,
headers: {
authorization: generateValidRequestAuthorizationHeader(pixAgentWithCertifRole.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: pixAgentWithCertifRole.id }),
};

await databaseBuilder.commit();
Expand All @@ -64,9 +62,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
const request = {
method: 'DELETE',
url: `/api/admin/certification-center-memberships/${certificationCenterMembership.id}`,
headers: {
authorization: generateValidRequestAuthorizationHeader(userWithoutRole.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: userWithoutRole.id }),
};

await databaseBuilder.commit();
Expand All @@ -87,9 +83,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
const request = {
method: 'DELETE',
url: `/api/admin/certification-center-memberships/${nonexistentCertificationCenterMembershipId}`,
headers: {
authorization: generateValidRequestAuthorizationHeader(pixAgentWithAdminRole.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: pixAgentWithAdminRole.id }),
};

await databaseBuilder.commit();
Expand Down Expand Up @@ -141,9 +135,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
},
},
},
headers: {
authorization: generateValidRequestAuthorizationHeader(certifCenterAdminUser.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: certifCenterAdminUser.id }),
};
await databaseBuilder.commit();

Expand Down Expand Up @@ -190,9 +182,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
},
},
},
headers: {
authorization: generateValidRequestAuthorizationHeader(certifCenterMemberUser.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: certifCenterMemberUser.id }),
};
await databaseBuilder.commit();

Expand Down Expand Up @@ -231,9 +221,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
},
},
},
headers: {
authorization: generateValidRequestAuthorizationHeader(certifCenterAdminUser.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: certifCenterAdminUser.id }),
};
await databaseBuilder.commit();

Expand Down Expand Up @@ -267,9 +255,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
},
},
},
headers: {
authorization: generateValidRequestAuthorizationHeader(certifCenterAdminUser.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: certifCenterAdminUser.id }),
};
await databaseBuilder.commit();

Expand Down Expand Up @@ -309,9 +295,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
const request = {
method: 'DELETE',
url: `/api/certification-center-memberships/${certificationCenterMembership.id}`,
headers: {
authorization: generateValidRequestAuthorizationHeader(pixCertifAdminUser.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: pixCertifAdminUser.id }),
};

await databaseBuilder.commit();
Expand All @@ -333,9 +317,7 @@ describe('Acceptance | API | Certification Center Membership', function () {
const request = {
method: 'DELETE',
url: `/api/certification-center-memberships/${certificationCenterMembership.id}`,
headers: {
authorization: generateValidRequestAuthorizationHeader(userWithoutRole.id),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: userWithoutRole.id }),
};

await databaseBuilder.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
createServer,
databaseBuilder,
expect,
generateValidRequestAuthorizationHeader,
generateAuthenticatedUserRequestHeaders,
insertUserWithRoleSuperAdmin,
knex,
} from '../../../test-helper.js';
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('Acceptance | API | Certification Center', function () {
const request = {
method: 'GET',
url: '/api/certification-centers/' + certificationCenter.id + '/divisions',
headers: { authorization: generateValidRequestAuthorizationHeader(user.id) },
headers: generateAuthenticatedUserRequestHeaders({ userId: user.id }),
};

// when
Expand All @@ -63,9 +63,7 @@ describe('Acceptance | API | Certification Center', function () {

// when
const response = await server.inject({
headers: {
authorization: generateValidRequestAuthorizationHeader(),
},
headers: generateAuthenticatedUserRequestHeaders(),
method: 'GET',
url: `/api/admin/certification-centers/${certificationCenter.id}/certification-center-memberships`,
});
Expand All @@ -91,9 +89,7 @@ describe('Acceptance | API | Certification Center', function () {

// when
const response = await server.inject({
headers: {
authorization: generateValidRequestAuthorizationHeader(),
},
headers: generateAuthenticatedUserRequestHeaders(),
method: 'GET',
url: `/api/admin/certification-centers/${certificationCenter.id}/certification-center-memberships`,
});
Expand Down Expand Up @@ -172,9 +168,7 @@ describe('Acceptance | API | Certification Center', function () {
databaseBuilder.factory.buildCoreSubscription({ certificationCandidateId: candidate.id });
await databaseBuilder.commit();
const request = {
headers: {
authorization: generateValidRequestAuthorizationHeader(userId),
},
headers: generateAuthenticatedUserRequestHeaders({ userId }),
method: 'GET',
url: `/api/certification-centers/${certificationCenterId}/session-summaries?page[number]=1&page[size]=10`,
};
Expand Down Expand Up @@ -214,9 +208,7 @@ describe('Acceptance | API | Certification Center', function () {
databaseBuilder.factory.buildUser({ email });

request = {
headers: {
authorization: generateValidRequestAuthorizationHeader(),
},
headers: generateAuthenticatedUserRequestHeaders(),
method: 'POST',
url: `/api/admin/certification-centers/${certificationCenterId}/certification-center-memberships`,
payload: { email },
Expand All @@ -236,7 +228,7 @@ describe('Acceptance | API | Certification Center', function () {
context('when user is not SuperAdmin', function () {
it('should return 403 HTTP status code ', async function () {
// given
request.headers.authorization = generateValidRequestAuthorizationHeader(1111);
request.headers = generateAuthenticatedUserRequestHeaders({ userId: 1111 });

// when
const response = await server.inject(request);
Expand Down Expand Up @@ -339,7 +331,7 @@ describe('Acceptance | API | Certification Center', function () {
method: 'POST',
url: `/api/certif/certification-centers/${certificationCenterId}/update-referer`,
payload,
headers: { authorization: generateValidRequestAuthorizationHeader(certificationCenterMemberId) },
headers: generateAuthenticatedUserRequestHeaders({ userId: certificationCenterMemberId }),
};

// when
Expand Down Expand Up @@ -382,7 +374,7 @@ describe('Acceptance | API | Certification Center', function () {
method: 'POST',
url: `/api/certification-centers/${certificationCenterId}/session`,
payload,
headers: { authorization: generateValidRequestAuthorizationHeader(userId) },
headers: generateAuthenticatedUserRequestHeaders({ userId }),
};

// when
Expand Down Expand Up @@ -433,7 +425,7 @@ describe('Acceptance | API | Certification Center', function () {
method: 'POST',
url: `/api/certification-centers/${certificationCenterId}/session`,
payload,
headers: { authorization: generateValidRequestAuthorizationHeader(userId) },
headers: generateAuthenticatedUserRequestHeaders({ userId }),
};

// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createServer, expect, generateValidRequestAuthorizationHeader } from '../../../test-helper.js';
import { createServer, expect, generateAuthenticatedUserRequestHeaders } from '../../../test-helper.js';

describe('Acceptance | API | countries-controller', function () {
let server;
Expand All @@ -13,9 +13,7 @@ describe('Acceptance | API | countries-controller', function () {
const options = {
method: 'GET',
url: '/api/countries',
headers: {
authorization: generateValidRequestAuthorizationHeader({ userId: 12345 }),
},
headers: generateAuthenticatedUserRequestHeaders({ userId: 12345 }),
};

// when
Expand Down
Loading

0 comments on commit 3f9179c

Please sign in to comment.