Skip to content

Commit

Permalink
Redirige vers page accueil après authentification
Browse files Browse the repository at this point in the history
Note – En cas de mise à jour d'un cookie de session pendant une
redirection, il n'est pas garanti que le navigateur relise le cookie au
service de la page vers laquelle on est redirigé. Pour assurer ce
comportement, il faut que le cookie de session soit mis à jour sur un
HTTP 200, page depuis laquelle on effectue une redirection depuis le
navigateur.

(source : https://stackoverflow.com/a/71467131)

Co-authored-by: Fabien Lamarque <[email protected]>
  • Loading branch information
egaillot and Fabinout committed May 14, 2024
1 parent 0d3b027 commit 58fb4e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
12 changes: 10 additions & 2 deletions src/api/connexionFCPlus.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const redirigeDepuisNavigateur = (destination, reponse) => reponse.send(`
<!DOCTYPE html>
<html>
<head><meta http-equiv="refresh" content="0; url='${destination}'"></head>
<body></body>
</html>
`);

const connexionFCPlus = (config, code, requete, reponse) => {
const { adaptateurChiffrement, fabriqueSessionFCPlus } = config;

Expand All @@ -6,8 +14,8 @@ const connexionFCPlus = (config, code, requete, reponse) => {
return fabriqueSessionFCPlus.nouvelleSession(code)
.then((session) => session.enJSON())
.then((infos) => adaptateurChiffrement.genereJeton(infos)
.then((jwt) => { requete.session.jeton = jwt; })
.then(() => reponse.json(infos)))
.then((jwt) => { requete.session.jeton = jwt; }))
.then(() => redirigeDepuisNavigateur('/', reponse))
.catch((e) => reponse.status(502).json({ erreur: `Échec authentification (${e.message})` }));
};

Expand Down
16 changes: 1 addition & 15 deletions test/api/connexionFCPlus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,10 @@ describe('Le requêteur de connexion FC+', () => {
});
requete.session = {};
reponse.json = () => Promise.resolve();
reponse.redirect = () => Promise.resolve();
reponse.status = () => reponse;
});

it('retourne les infos de la session FC+ en JSON', () => {
expect.assertions(1);

fabriqueSessionFCPlus.nouvelleSession = () => Promise.resolve({
enJSON: () => Promise.resolve({ infos: 'des infos' }),
});

reponse.json = (donnees) => {
expect(donnees).toEqual({ infos: 'des infos' });
return Promise.resolve();
};

return connexionFCPlus(config, 'unCode', requete, reponse);
});

it('conserve les infos utilisateurs dans un cookie de session', () => {
adaptateurChiffrement.genereJeton = () => Promise.resolve('XXX');

Expand Down
39 changes: 15 additions & 24 deletions test/routes/routesAuth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,23 @@ describe('Le serveur des routes `/auth`', () => {

describe('sur GET /auth/fcplus/connexion', () => {
describe('lorsque les paramètres `code` et `state` sont présents', () => {
it('sert les infos de la session FC+', () => {
serveur.fabriqueSessionFCPlus().nouvelleSession = (code) => {
try {
expect(code).toBe('unCode');
return Promise.resolve({
enJSON: () => Promise.resolve({ infos: 'des infos' }),
});
} catch (e) {
return Promise.reject(e);
}
};

return axios.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`)
.then((reponse) => {
expect(reponse.status).toBe(200);
expect(reponse.data).toEqual({ infos: 'des infos' });
})
.catch(leveErreur);
});
it('redirige vers page accueil depuis navigateur', () => (
axios.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`)
.then((reponse) => expect(reponse.data).toContain('<meta http-equiv="refresh" content="0; url=\'/\'">'))
.catch(leveErreur)
));

it('stocke les infos dans un cookie sans attribut `Secure` si autorisé', () => {
it('stocke les infos dans un cookie sans attribut `Secure` si autorisé avant la redirection', () => {
serveur.adaptateurEnvironnement().avecEnvoiCookieSurHTTP = () => true;
return axios.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`)
.then((reponse) => {
expect(reponse.headers).toHaveProperty('set-cookie');
const valeurEnteteSetCookie = reponse
return axios({
method: 'get',
url: `http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`,
maxRedirects: 0,
})
.catch(({ response }) => {
expect(response.status).toBe(302);
expect(response.headers).toHaveProperty('set-cookie');
const valeurEnteteSetCookie = response
.headers['set-cookie']
.find((h) => h.match(/session=/));
expect(valeurEnteteSetCookie).not.toContain('secure');
Expand Down

0 comments on commit 58fb4e2

Please sign in to comment.