From 0a9a09ebebe74da1717d2d89fe0a9e1a52f08376 Mon Sep 17 00:00:00 2001 From: Emmanuel Gaillot Date: Thu, 6 Jun 2024 10:55:11 +0200 Subject: [PATCH] [CORRECTION] redirige depuis navigateur MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … pour que le cookie de session soit correctement retransmis au serveur. Co-authored-by: Fabien Lamarque --- src/routes/routesAuth.js | 38 +++++++++++++++++++++------------- test/routes/routesAuth.spec.js | 24 +++++++++++++++------ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/routes/routesAuth.js b/src/routes/routesAuth.js index 1923933..3745c11 100644 --- a/src/routes/routesAuth.js +++ b/src/routes/routesAuth.js @@ -1,5 +1,6 @@ const express = require('express'); +const { redirigeDepuisNavigateur } = require('./utils'); const connexionFCPlus = require('../api/connexionFCPlus'); const deconnexionFCPlus = require('../api/deconnexionFCPlus'); const creationSessionFCPlus = require('../api/creationSessionFCPlus'); @@ -35,22 +36,31 @@ const routesAuth = (config) => { .send(clePubliqueDansJWKSet); }); - routes.get('/fcplus/connexion', (...args) => middleware.verifieTamponUnique(...args), (requete, reponse) => { - const { code, state } = requete.query; - if (typeof state === 'undefined' || state === '') { - reponse.status(400).json({ erreur: "Paramètre 'state' absent de la requête" }); - } else if (typeof code === 'undefined' || code === '') { - reponse.status(400).json({ erreur: "Paramètre 'code' absent de la requête" }); - } else { - connexionFCPlus( - { adaptateurChiffrement, fabriqueSessionFCPlus }, - code, - requete, - reponse, - ); - } + routes.get('/fcplus/connexion', (requete, reponse) => { + const paramsRequete = new URLSearchParams(requete.query).toString(); + redirigeDepuisNavigateur(`/auth/fcplus/connexion_apres_redirection?${paramsRequete}`, reponse); }); + routes.get( + '/fcplus/connexion_apres_redirection', + (...args) => middleware.verifieTamponUnique(...args), + (requete, reponse) => { + const { code, state } = requete.query; + if (typeof state === 'undefined' || state === '') { + reponse.status(400).json({ erreur: "Paramètre 'state' absent de la requête" }); + } else if (typeof code === 'undefined' || code === '') { + reponse.status(400).json({ erreur: "Paramètre 'code' absent de la requête" }); + } else { + connexionFCPlus( + { adaptateurChiffrement, fabriqueSessionFCPlus }, + code, + requete, + reponse, + ); + } + }, + ); + routes.get('/fcplus/deconnexion', (requete, reponse) => ( deconnexionFCPlus(requete, reponse) )); diff --git a/test/routes/routesAuth.spec.js b/test/routes/routesAuth.spec.js index b44296a..87d5fbe 100644 --- a/test/routes/routesAuth.spec.js +++ b/test/routes/routesAuth.spec.js @@ -37,6 +37,18 @@ describe('Le serveur des routes `/auth`', () => { }); describe('sur GET /auth/fcplus/connexion', () => { + it('redirige vers `/auth/fcplus/connexion_apres_redirection', () => axios + .get(`http://localhost:${port}/auth/fcplus/connexion`) + .then((reponse) => expect(reponse.data).toContain('')) .catch(leveErreur) )); @@ -59,7 +71,7 @@ describe('Le serveur des routes `/auth`', () => { serveur.adaptateurEnvironnement().avecEnvoiCookieSurHTTP = () => true; return axios({ method: 'get', - url: `http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`, + url: `http://localhost:${port}/auth/fcplus/connexion_apres_redirection?state=unState&code=unCode`, maxRedirects: 0, }) .catch(({ response }) => { @@ -80,7 +92,7 @@ describe('Le serveur des routes `/auth`', () => { enJSON: () => Promise.reject(new Error('Oups')), }); - return axios.get(`http://localhost:${port}/auth/fcplus/connexion?code=unCode&state=unState`) + return axios.get(`http://localhost:${port}/auth/fcplus/connexion_apres_redirection?code=unCode&state=unState`) .catch(({ response }) => { expect(response.status).toBe(502); expect(response.data).toEqual({ erreur: 'Échec authentification (Oups)' }); @@ -91,7 +103,7 @@ describe('Le serveur des routes `/auth`', () => { it("sert une erreur HTTP 400 (Bad Request) si le paramètre 'code' est manquant", () => { expect.assertions(2); - return axios.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState`) + return axios.get(`http://localhost:${port}/auth/fcplus/connexion_apres_redirection?state=unState`) .catch(({ response }) => { expect(response.status).toBe(400); expect(response.data).toEqual({ erreur: "Paramètre 'code' absent de la requête" }); @@ -101,7 +113,7 @@ describe('Le serveur des routes `/auth`', () => { it("sert une erreur HTTP 400 (Bad Request) si le paramètre 'state' est manquant", () => { expect.assertions(2); - return axios.get(`http://localhost:${port}/auth/fcplus/connexion?code=unCode`) + return axios.get(`http://localhost:${port}/auth/fcplus/connexion_apres_redirection?code=unCode`) .catch(({ response }) => { expect(response.status).toBe(400); expect(response.data).toEqual({ erreur: "Paramètre 'state' absent de la requête" });