Skip to content

Commit

Permalink
[BUGFIX][MON-PIX] Prendre en compte le choix de la langue lors de la …
Browse files Browse the repository at this point in the history
…création d'un compte via un SSO (PIX-12860)

 #9282
  • Loading branch information
pix-service-auto-merge authored Jun 19, 2024
2 parents 3318562 + 14b3b7b commit e77f98e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BadRequestError, UnauthorizedError } from '../../../../lib/application/http-errors.js';
import * as oidcSerializer from '../../../../lib/infrastructure/serializers/jsonapi/oidc-serializer.js';
import { requestResponseUtils } from '../../../shared/infrastructure/utils/request-response-utils.js';
import { usecases } from '../../domain/usecases/index.js';
import * as oidcProviderSerializer from '../../infrastructure/serializers/jsonapi/oidc-identity-providers.serializer.js';

Expand Down Expand Up @@ -52,14 +53,16 @@ async function authenticateOidcUser(request, h) {
* @param h
* @return {Promise<{access_token: string, logout_url_uuid: string}>}
*/
async function createUser(request, h) {
async function createUser(request, h, dependencies = { requestResponseUtils }) {
const { identityProvider, authenticationKey } = request.deserializedPayload;
const localeFromCookie = request.state?.locale;
const language = dependencies.requestResponseUtils.extractLocaleFromRequest(request);

const { accessToken: access_token, logoutUrlUUID: logout_url_uuid } = await usecases.createOidcUser({
authenticationKey,
identityProvider,
localeFromCookie,
language,
});

return h.response({ access_token, logout_url_uuid }).code(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ import { AuthenticationKeyExpired } from '../errors.js';
import { UserToCreate } from '../models/UserToCreate.js';

/**
* @typedef {function} createOidcUser
* @param {Object} params
* @param {string} params.identityProvider
* @param {string} params.authenticationKey
* @param {string} params.localeFromCookie
* @param {AuthenticationSessionService} params.authenticationSessionService
* @param {OidcAuthenticationServiceRegistry} params.oidcAuthenticationServiceRegistry
* @param {AuthenticationMethodRepository} params.authenticationMethodRepository
* @param {UserToCreateRepository} params.userToCreateRepository
* @param {UserLoginRepository} params.userLoginRepository
* @param {{
* identityProvider: string,
* authenticationKey: string,
* localeFromCookie: string,
* language: string,
* authenticationSessionService: AuthenticationSessionService,
* oidcAuthenticationServiceRegistry: OidcAuthenticationServiceRegistry,
* authenticationMethodRepository: AuthenticationMethodRepository,
* userToCreateRepository: UserToCreateRepository,
* userLoginRepository: UserLoginRepository,
* }} params
* @return {Promise<{accessToken: string, logoutUrlUUID: string}>}
*/
async function createOidcUser({
identityProvider,
authenticationKey,
localeFromCookie,
language,
authenticationSessionService,
oidcAuthenticationServiceRegistry,
authenticationMethodRepository,
Expand Down Expand Up @@ -47,6 +49,7 @@ async function createOidcUser({
firstName: userInfo.firstName,
lastName: userInfo.lastName,
locale: localeFromCookie,
lang: language,
});

await oidcAuthenticationServiceRegistry.loadOidcProviderServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ describe('Acceptance | Identity Access Management | Application | Route | oidc-p
method: 'POST',
url: '/api/oidc/users',
headers: {
'accept-language': 'fr',
cookie: 'locale=fr-FR',
},
payload: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ describe('Unit | Identity Access Management | Application | Controller | oidc-pr
// given
const request = {
deserializedPayload: { identityProvider: 'OIDC', authenticationKey: 'abcde' },
headers: {
'accept-language': 'fr',
},
state: {
locale: 'fr-FR',
},
Expand All @@ -132,6 +135,7 @@ describe('Unit | Identity Access Management | Application | Controller | oidc-pr
identityProvider: 'OIDC',
authenticationKey: 'abcde',
localeFromCookie: 'fr-FR',
language: 'fr',
});
expect(response.statusCode).to.equal(200);
expect(response.source).to.deep.equal({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ describe('Unit | Identity Access Management | Domain | UseCase | create-oidc-use
});
});

it('creates the user account and returns an access token, the logout url uuid and update the last logged date with the existing external user id', async function () {
it('creates the user account with given language and returns an access token, the logout url uuid and update the last logged date with the existing external user id', async function () {
// given
const idToken = 'idToken';
const language = 'nl';
authenticationSessionService.getByKey.withArgs('AUTHENTICATION_KEY').resolves({
sessionContent: { idToken, accessToken: 'accessToken' },
userInfo: { firstName: 'Jean', lastName: 'Heymar', externalIdentityId: 'externalId' },
Expand All @@ -107,7 +108,8 @@ describe('Unit | Identity Access Management | Domain | UseCase | create-oidc-use
const result = await createOidcUser({
identityProvider: 'SOME_IDP',
authenticationKey: 'AUTHENTICATION_KEY',
localeFromCookie: 'fr-FR',
localeFromCookie: 'nl-BE',
language,
authenticationSessionService,
oidcAuthenticationServiceRegistry,
authenticationMethodRepository,
Expand All @@ -120,7 +122,8 @@ describe('Unit | Identity Access Management | Domain | UseCase | create-oidc-use
user: {
firstName: 'Jean',
lastName: 'Heymar',
locale: 'fr-FR',
locale: 'nl-BE',
lang: 'nl',
cgu: true,
lastTermsOfServiceValidatedAt: now,
},
Expand Down
4 changes: 3 additions & 1 deletion mon-pix/app/authenticators/oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import { decodeToken } from 'mon-pix/helpers/jwt';
import RSVP from 'rsvp';

export default class OidcAuthenticator extends BaseAuthenticator {
@service session;
@service intl;
@service location;
@service oidcIdentityProviders;
@service session;

async authenticate({ code, state, identityProviderSlug, authenticationKey, hostSlug }) {
const request = {
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-Language': this.intl.primaryLocale,
'Content-Type': 'application/json',
},
};
Expand Down
4 changes: 4 additions & 0 deletions mon-pix/tests/unit/authenticators/oidc_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import * as fetch from 'fetch';
import { module, test } from 'qunit';
import sinon from 'sinon';

import setupIntl from '../../helpers/setup-intl';

module('Unit | Authenticator | oidc', function (hooks) {
setupTest(hooks);
setupIntl(hooks);

module('#authenticate', function (hooks) {
const userId = 1;
Expand Down Expand Up @@ -78,6 +81,7 @@ module('Unit | Authenticator | oidc', function (hooks) {
});

// then
request.headers['Accept-Language'] = 'fr';
request.body = JSON.stringify({
data: {
attributes: {
Expand Down

0 comments on commit e77f98e

Please sign in to comment.