Skip to content

Commit 9d40953

Browse files
committed
feat(api): generically add add oidc userInfo
1 parent 6c26138 commit 9d40953

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

api/src/identity-access-management/application/oidc-provider/oidc-provider.controller.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,8 @@ async function authenticateOidcUser(request, h) {
4242
// TODO utiliser un message en anglais au lieu du français
4343
const message = "L'utilisateur n'a pas de compte Pix";
4444
const responseCode = 'SHOULD_VALIDATE_CGU';
45-
const { authenticationKey, givenName, familyName, email } = result;
46-
const meta = { authenticationKey, givenName, familyName };
4745

48-
if (email) {
49-
Object.assign(meta, { email });
50-
}
51-
52-
throw new UnauthorizedError(message, responseCode, meta);
46+
throw new UnauthorizedError(message, responseCode, result);
5347
}
5448

5549
/**

api/src/identity-access-management/domain/usecases/authenticate-oidc-user.usecase.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import lodash from 'lodash';
2+
13
import { ForbiddenAccess } from '../../../shared/domain/errors.js';
24

5+
const { omit } = lodash;
6+
37
/**
48
* @typedef {function} authenticateOidcUser
59
* @param {Object} params
@@ -63,8 +67,17 @@ async function authenticateOidcUser({
6367

6468
if (!user) {
6569
const authenticationKey = await authenticationSessionService.save({ userInfo, sessionContent });
66-
const { firstName: givenName, lastName: familyName, email } = userInfo;
67-
return { authenticationKey, givenName, familyName, email, isAuthenticationComplete: false };
70+
71+
const userClaims = omit(userInfo, ['externalIdentityId']);
72+
73+
return {
74+
authenticationKey,
75+
userClaims,
76+
isAuthenticationComplete: false,
77+
// TODO: The properties givenName and familyName are kept for backward compatibility with the Front. They will be removed soon.
78+
givenName: userClaims.firstName,
79+
familyName: userClaims.lastName,
80+
};
6881
}
6982

7083
await _assertUserHasAccessToApplication({ requestedApplication, user, adminMemberRepository });

api/tests/identity-access-management/unit/application/oidc-provider.controller.test.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,20 @@ describe('Unit | Identity Access Management | Application | Controller | oidc-pr
8383
it('returns UnauthorizedError', async function () {
8484
// given
8585
const authenticationKey = 'aaa-bbb-ccc';
86-
const givenName = 'Mélusine';
87-
const familyName = 'TITEGOUTTE';
86+
const firstName = 'Mélusine';
87+
const lastName = 'TITEGOUTTE';
8888
const email = 'melu@example.net';
89-
usecases.authenticateOidcUser.resolves({ authenticationKey, givenName, familyName, email });
89+
const userClaims = {
90+
firstName,
91+
lastName,
92+
email,
93+
};
94+
usecases.authenticateOidcUser.resolves({
95+
authenticationKey,
96+
userClaims,
97+
givenName: firstName,
98+
familyName: lastName,
99+
});
90100

91101
// when
92102
const error = await catchErr(oidcProviderController.authenticateOidcUser)(request, hFake);
@@ -95,7 +105,12 @@ describe('Unit | Identity Access Management | Application | Controller | oidc-pr
95105
expect(error).to.be.an.instanceOf(UnauthorizedError);
96106
expect(error.message).to.equal("L'utilisateur n'a pas de compte Pix");
97107
expect(error.code).to.equal('SHOULD_VALIDATE_CGU');
98-
expect(error.meta).to.deep.equal({ authenticationKey, givenName, familyName, email });
108+
expect(error.meta).to.deep.equal({
109+
authenticationKey,
110+
userClaims,
111+
givenName: firstName,
112+
familyName: lastName,
113+
});
99114
});
100115
});
101116
});

api/tests/identity-access-management/unit/domain/usecases/authenticate-oidc-user.usecase.test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,13 @@ describe('Unit | Identity Access Management | Domain | UseCase | authenticate-oi
231231
expect(authenticationSessionService.save).to.have.been.calledWithExactly({ userInfo, sessionContent });
232232
expect(result).to.deep.equal({
233233
authenticationKey,
234+
userClaims: {
235+
firstName: 'Mélusine',
236+
lastName: 'TITEGOUTTE',
237+
email: 'melu@example.net',
238+
},
234239
givenName: 'Mélusine',
235240
familyName: 'TITEGOUTTE',
236-
email: 'melu@example.net',
237241
isAuthenticationComplete: false,
238242
});
239243
});

0 commit comments

Comments
 (0)