Skip to content

Commit

Permalink
Configure le type de requête pour chaque middleware
Browse files Browse the repository at this point in the history
...afin de pouvoir ensuite connaître le type de navigation pour chaque requête
  • Loading branch information
Nephtys authored and ThibaudMZN committed Jan 31, 2025
1 parent 7b4ba62 commit 7c4d4ac
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/http/configurationServeur.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const DUREE_SESSION = 60 * 60 * 1000;

const TYPES_REQUETES = {
API: 'API',
NAVIGATION: 'NAVIGATION',
RESSOURCE: 'RESSOURCE',
};

const ENDPOINTS_SANS_CSRF = [
// Inspiration : https://stackoverflow.com/a/60941601
// L'obtention du token nécessite une action utilisateur (saisie Login + MDP) donc on la protège pas.
Expand All @@ -16,6 +22,7 @@ const ENDPOINTS_SANS_CSRF = [
const { CACHE_CONTROL_FICHIERS_STATIQUES } = process.env;

module.exports = {
TYPES_REQUETES,
CACHE_CONTROL_FICHIERS_STATIQUES,
DUREE_SESSION,
ENDPOINTS_SANS_CSRF,
Expand Down
19 changes: 17 additions & 2 deletions src/mss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
CACHE_CONTROL_FICHIERS_STATIQUES,
DUREE_SESSION,
ENDPOINTS_SANS_CSRF,
TYPES_REQUETES,
} = require('./http/configurationServeur');
const routesConnecteApi = require('./routes/connecte/routesConnecteApi');
const routesNonConnecteApi = require('./routes/nonConnecte/routesNonConnecteApi');
Expand Down Expand Up @@ -89,6 +90,7 @@ const creeServeur = ({

app.use(
'',
middleware.chargeTypeRequete(TYPES_REQUETES.NAVIGATION),
routesNonConnectePage({
adaptateurCmsCrisp,
adaptateurEnvironnement,
Expand All @@ -104,6 +106,7 @@ const creeServeur = ({
);
app.use(
'',
middleware.chargeTypeRequete(TYPES_REQUETES.NAVIGATION),
routesConnectePage({
depotDonnees,
middleware,
Expand All @@ -116,6 +119,7 @@ const creeServeur = ({
);
app.use(
'/api',
middleware.chargeTypeRequete(TYPES_REQUETES.API),
routesNonConnecteApi({
middleware,
referentiel,
Expand All @@ -131,6 +135,7 @@ const creeServeur = ({
);
app.use(
'/oidc',
middleware.chargeTypeRequete(TYPES_REQUETES.NAVIGATION),
routesNonConnecteOidc({
adaptateurOidc,
adaptateurJWT,
Expand All @@ -142,13 +147,15 @@ const creeServeur = ({
);
app.use(
'/oidc',
middleware.chargeTypeRequete(TYPES_REQUETES.NAVIGATION),
routesConnecteOidc({
middleware,
adaptateurOidc,
})
);
app.use(
'/api',
middleware.chargeTypeRequete(TYPES_REQUETES.API),
middleware.verificationJWT,
routesConnecteApi({
middleware,
Expand All @@ -167,8 +174,16 @@ const creeServeur = ({
serviceCgu,
})
);
app.use('/bibliotheques', routesNonConnecteApiBibliotheques());
app.use('/styles', routesNonConnecteApiStyles());
app.use(
'/bibliotheques',
middleware.chargeTypeRequete(TYPES_REQUETES.RESSOURCE),
routesNonConnecteApiBibliotheques()
);
app.use(
'/styles',
middleware.chargeTypeRequete(TYPES_REQUETES.RESSOURCE),
routesNonConnecteApiStyles()
);

app.use(
'/statique',
Expand Down
17 changes: 15 additions & 2 deletions test/mocks/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ let traficProtege = false;
let verificationJWTMenee = false;
let verificationCGUMenee = false;
let versionBuildeeChargee = false;
let typeRequeteCharge = null;
let idTokenAgentConnect;
let fonctionDeposeCookie;

Expand Down Expand Up @@ -107,6 +108,7 @@ const middlewareFantaisie = {
sourceAuthentification = authentificationAUtiliser;
idTokenAgentConnect = idTokenAgentConnectAUtiliser;
fonctionDeposeCookie = fonctionDeposeCookieAAppeler;
typeRequeteCharge = null;
},

ajouteVersionFichierCompiles: (_requete, _reponse, suite) => {
Expand Down Expand Up @@ -234,8 +236,8 @@ const middlewareFantaisie = {
suite();
},

chargeTypeRequete: (typeRequete) => (requete, _reponse, suite) => {
requete.typeRequete = typeRequete;
chargeTypeRequete: (typeRequete) => (_requete, _reponse, suite) => {
typeRequeteCharge = typeRequete;
suite();
},

Expand Down Expand Up @@ -395,6 +397,17 @@ const middlewareFantaisie = {
...params
);
},

verifieTypeRequeteCharge: (typeRequeteAttendu, ...params) => {
verifieRequeteChangeEtat(
{
lectureEtat: () => typeRequeteCharge,
etatInitial: null,
etatFinal: typeRequeteAttendu,
},
...params
);
},
};

module.exports = middlewareFantaisie;
79 changes: 79 additions & 0 deletions test/mss.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const axios = require('axios');
const expect = require('expect.js');
const testeurMSS = require('./routes/testeurMSS');
const { TYPES_REQUETES } = require('../src/http/configurationServeur');
const { unUtilisateur } = require('./constructeurs/constructeurUtilisateur');

describe('Le serveur MSS', () => {
const testeur = testeurMSS();
Expand Down Expand Up @@ -42,4 +44,81 @@ describe('Le serveur MSS', () => {
.verifieRequeteRepousseExpirationCookie('http://localhost:1234/', done);
});
});

describe('sur configuration des types de requête', () => {
[
{
url: '/',
typeAttendu: TYPES_REQUETES.NAVIGATION,
routeur: "'non connecté' de page",
},
{
url: '/motDePasse/edition',
typeAttendu: TYPES_REQUETES.NAVIGATION,
routeur: "'connecté' de page",
callbackInitialisation: () => {
testeur.depotDonnees().utilisateur = async () =>
unUtilisateur().construis();
},
},
{
url: '/api/sante',
typeAttendu: TYPES_REQUETES.API,
routeur: "'non connecté' d'API",
callbackInitialisation: () => {
testeur.depotDonnees().santeDuDepot = async () => {};
},
},
{
url: '/oidc/connexion',
typeAttendu: TYPES_REQUETES.NAVIGATION,
routeur: "'non connecté' d'OIDC",
callbackInitialisation: () => {
testeur.adaptateurOidc().genereDemandeAutorisation = async () => ({
nonce: 'unNonce',
state: 'unState',
url: 'http',
});
},
},
{
url: '/oidc/deconnexion',
typeAttendu: TYPES_REQUETES.NAVIGATION,
routeur: "'connecté' d'OIDC",
callbackInitialisation: () => {
testeur.adaptateurOidc().genereDemandeDeconnexion = async () => ({
state: 'unState',
url: 'http',
});
},
},
{
url: '/api/services',
typeAttendu: TYPES_REQUETES.API,
routeur: "'connecté' d'API",
},
{
url: '/bibliotheques/uneBibliotheque.js',
typeAttendu: TYPES_REQUETES.RESSOURCE,
routeur: 'de bibliotheques',
},
{
url: '/styles/feuilleDeStyle.css',
typeAttendu: TYPES_REQUETES.RESSOURCE,
routeur: 'de feuilles de styles',
},
].forEach(({ url, typeAttendu, routeur, callbackInitialisation }) => {
it(`identifie la requête comme ${typeAttendu} sur les routes ${routeur}`, (done) => {
callbackInitialisation?.();
testeur.middleware().verifieTypeRequeteCharge(
typeAttendu,
{
method: 'GET',
url: `http://localhost:1234${url}`,
},
done
);
});
});
});
});

0 comments on commit 7c4d4ac

Please sign in to comment.