Skip to content

Commit

Permalink
[REMANIEMENT] Déplace code HTML servi dans page Mustache
Browse files Browse the repository at this point in the history
Co-authored-by: Fabien Lamarque <[email protected]>
  • Loading branch information
egaillot and Fabinout committed Jun 6, 2024
1 parent 3f4134f commit ae852db
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
'no-only-tests/no-only-tests': 'error',
'no-param-reassign': ['error', {
props: true,
ignorePropertyModificationsFor: ['requete'],
ignorePropertyModificationsFor: ['requete', 'reponse'],
}],
},
};
4 changes: 2 additions & 2 deletions src/api/connexionFCPlus.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { redirigeDepuisNavigateur, stockeDansCookieSession } = require('../routes/utils');
const { stockeDansCookieSession } = require('../routes/utils');

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

Expand Down
4 changes: 2 additions & 2 deletions src/api/creationSessionFCPlus.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { redirigeDepuisNavigateur, stockeDansCookieSession } = require('../routes/utils');
const { stockeDansCookieSession } = require('../routes/utils');

const creationSessionFCPlus = (config, requete, reponse) => {
const { adaptateurChiffrement, adaptateurEnvironnement, adaptateurFranceConnectPlus } = config;
Expand All @@ -17,7 +17,7 @@ const creationSessionFCPlus = (config, requete, reponse) => {

return stockeDansCookieSession({ etat }, adaptateurChiffrement, requete)
.then(() => construisURL())
.then((url) => redirigeDepuisNavigateur(url, reponse));
.then((url) => reponse.render('redirectionNavigateur', { destination: url }));
};

module.exports = creationSessionFCPlus;
4 changes: 1 addition & 3 deletions src/routes/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { redirigeDepuisNavigateur } = require('./utils');

class Middleware {
constructor(config) {
this.adaptateurChiffrement = config.adaptateurChiffrement;
Expand All @@ -24,7 +22,7 @@ class Middleware {
return this.adaptateurChiffrement.verifieJeton(requete.session.jeton, this.secret)
.then(valide)
.then(suite)
.catch(() => redirigeDepuisNavigateur('/', reponse));
.catch(() => reponse.render('redirectionNavigateur', { destination: '/' }));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/routesAuth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const express = require('express');

const { redirigeDepuisNavigateur } = require('./utils');
const connexionFCPlus = require('../api/connexionFCPlus');
const deconnexionFCPlus = require('../api/deconnexionFCPlus');
const creationSessionFCPlus = require('../api/creationSessionFCPlus');
Expand Down Expand Up @@ -44,7 +43,8 @@ const routesAuth = (config) => {
reponse.status(400).json({ erreur: "Paramètre 'code' absent de la requête" });
} else {
const paramsRequete = new URLSearchParams(requete.query).toString();
redirigeDepuisNavigateur(`/auth/fcplus/connexion_apres_redirection?${paramsRequete}`, reponse);
const destination = `/auth/fcplus/connexion_apres_redirection?${paramsRequete}`;
reponse.render('redirectionNavigateur', { destination });
}
});

Expand Down
10 changes: 1 addition & 9 deletions src/routes/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
const redirigeDepuisNavigateur = (destination, reponse) => reponse.send(`
<!DOCTYPE html>
<html>
<head><meta http-equiv="refresh" content="0; url='${destination}'"></head>
<body></body>
</html>
`);

const stockeDansCookieSession = (infos, adaptateurChiffrement, requete) => adaptateurChiffrement
.genereJeton(infos)
.then((jwt) => { requete.session.jeton = jwt; });

module.exports = { redirigeDepuisNavigateur, stockeDansCookieSession };
module.exports = { stockeDansCookieSession };
2 changes: 2 additions & 0 deletions src/vues/redirectionNavigateur.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
<meta http-equiv="refresh" content="0; url='{{{destination}}}'">
2 changes: 1 addition & 1 deletion test/api/connexionFCPlus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Le requêteur de connexion FC+', () => {
});
requete.session = {};
reponse.json = () => Promise.resolve();
reponse.redirect = () => Promise.resolve();
reponse.render = () => Promise.resolve();
reponse.status = () => reponse;
});

Expand Down
121 changes: 39 additions & 82 deletions test/api/creationSessionFCPlus.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const creationSessionFCPlus = require('../../src/api/creationSessionFCPlus');

const prepareVerificationPresenceElement = (element, reponse) => {
reponse.render = (_nomPageRedirection, { destination }) => {
try {
expect(destination).toContain(element);
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};
};

describe('Le requêteur de création de session FC+', () => {
const adaptateurChiffrement = {};
const adaptateurEnvironnement = {};
Expand All @@ -18,99 +29,61 @@ describe('Le requêteur de création de session FC+', () => {
adaptateurFranceConnectPlus.urlCreationSession = () => Promise.resolve('');
requete.query = {};
requete.session = {};
reponse.send = () => Promise.resolve();
reponse.render = () => Promise.resolve();
});

it('redirige vers serveur France Connect Plus', () => {
expect.assertions(1);
adaptateurFranceConnectPlus.urlCreationSession = () => Promise.resolve('http://example.com');

reponse.send = (url) => {
try {
expect(url).toContain('http://example.com?');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

prepareVerificationPresenceElement('http://example.com?', reponse);
return creationSessionFCPlus(config, requete, reponse);
});

it('ajoute des paramètres à la requête', () => {
expect.assertions(6);

reponse.send = (url) => {
try {
expect(url).toContain('scope=profile%20openid%20birthcountry%20birthplace');
expect(url).toContain('acr_values=eidas2');
expect(url).toContain('claims={%22id_token%22:{%22amr%22:{%22essential%22:true}}}');
expect(url).toContain('prompt=login%20consent');
expect(url).toContain('response_type=code');
expect(url).toContain('idp_hint=');

return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

const verifiePresenceParametreEnDur = (param) => it(`ajoute le paramètre ${param} à la requête`, () => {
expect.assertions(1);
prepareVerificationPresenceElement(param, reponse);
return creationSessionFCPlus(config, requete, reponse);
});

[
'scope=profile%20openid%20birthcountry%20birthplace',
'acr_values=eidas2',
'claims={%22id_token%22:{%22amr%22:{%22essential%22:true}}}',
'prompt=login%20consent',
'response_type=code',
'idp_hint=',
].forEach(verifiePresenceParametreEnDur);

it("ajoute l'identifiant client FC+ en paramètre", () => {
expect.assertions(1);

adaptateurEnvironnement.identifiantClient = () => '12345';

reponse.send = (url) => {
try {
expect(url).toContain('client_id=12345');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

prepareVerificationPresenceElement('client_id=12345', reponse);
return creationSessionFCPlus(config, requete, reponse);
});

it("ajoute l'URL de redirection post-login en paramètre", () => {
expect.assertions(1);

adaptateurEnvironnement.urlRedirectionConnexion = () => 'http://example.com';

reponse.send = (url) => {
try {
expect(url).toContain('redirect_uri=http://example.com');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

prepareVerificationPresenceElement('redirect_uri=http://example.com', reponse);
return creationSessionFCPlus(config, requete, reponse);
});

it('ajoute un état et un nonce en paramètres de la requête', () => {
expect.assertions(2);
let nbClesGenerees = 0;
it('ajoute un état en paramètre de la requête', () => {
expect.assertions(1);
adaptateurChiffrement.cleHachage = () => '12345';

adaptateurChiffrement.cleHachage = () => {
nbClesGenerees += 1;
return `12345-${nbClesGenerees}`;
};
prepareVerificationPresenceElement('state=12345', reponse);
return creationSessionFCPlus(config, requete, reponse);
});

reponse.send = (url) => {
try {
expect(url).toContain('state=12345-1');
expect(url).toContain('nonce=12345-2');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};
it('ajoute un nonce en paramètre de la requête', () => {
expect.assertions(1);
adaptateurChiffrement.cleHachage = () => '12345';

prepareVerificationPresenceElement('nonce=12345', reponse);
return creationSessionFCPlus(config, requete, reponse);
});

Expand Down Expand Up @@ -144,15 +117,7 @@ describe('Le requêteur de création de session FC+', () => {
expect.assertions(1);
adaptateurEnvironnement.fournisseurIdentiteSuggere = () => 'eidas-bridge';

reponse.send = (url) => {
try {
expect(url).toContain('idp_hint=eidas-bridge');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

prepareVerificationPresenceElement('idp_hint=eidas-bridge', reponse);
return creationSessionFCPlus(config, requete, reponse);
});
});
Expand All @@ -163,15 +128,7 @@ describe('Le requêteur de création de session FC+', () => {
adaptateurEnvironnement.avecMock = () => true;

requete.query.contexteMock = 'unContexte';
reponse.send = (url) => {
try {
expect(url).toContain('contexte_mock=unContexte');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

prepareVerificationPresenceElement('contexte_mock=unContexte', reponse);
return creationSessionFCPlus(config, requete, reponse);
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/routes/middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Le middleware OOTS-France', () => {
adaptateurChiffrement.verifieJeton = () => Promise.resolve();

requete = { query: {}, session: { jeton: '' } };
reponse.send = () => Promise.resolve();
reponse.render = () => Promise.resolve();
});

it('vérifie le jeton stocké en session', (suite) => {
Expand Down Expand Up @@ -82,9 +82,9 @@ describe('Le middleware OOTS-France', () => {
});

it('redirige vers page accueil depuis navigateur si tampon communiqué différent', () => {
reponse.send = (html) => {
reponse.render = (_nomPageRedirection, { destination }) => {
try {
expect(html).toContain('<meta http-equiv="refresh" content="0; url=\'/\'">');
expect(destination).toBe('/');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
Expand Down

0 comments on commit ae852db

Please sign in to comment.