Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Journalise les erreurs renvoyées par FranceConnect+ #38

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"axios": "^1.6.0",
"cookie-session": "^2.1.0",
"diary": "^0.4.5",
"express": "^4.19.2",
"jose": "^5.2.0",
"nodemon": "^3.0.1",
Expand Down
10 changes: 5 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const SiteVitrine = require('./src/siteVitrine');
const adaptateurChiffrement = require('./src/adaptateurs/adaptateurChiffrement');
const adaptateurEnvironnement = require('./src/adaptateurs/adaptateurEnvironnement');
const adaptateurFranceConnectPlus = require('./src/adaptateurs/adaptateurFranceConnectPlus');
const journal = require('./src/adaptateurs/journal');
const FabriqueSessionFCPlus = require('./src/modeles/fabriqueSessionFCPlus');
const Middleware = require('./src/routes/middleware');

Expand All @@ -11,20 +12,19 @@ const fabriqueSessionFCPlus = new FabriqueSessionFCPlus({
});
const middleware = new Middleware({ adaptateurChiffrement, adaptateurEnvironnement });

journal.active();

const serveur = SiteVitrine.creeServeur({
adaptateurChiffrement,
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
});

const port = process.env.PORT || 3000;

serveur.ecoute(port, () => {
/* eslint-disable no-console */

console.log(`Le site vitrine est démarré et écoute le port ${port} !…`);

/* eslint-enable no-console */
journal.consigne(`Le site vitrine est démarré et écoute le port ${port} !…`);
});
6 changes: 6 additions & 0 deletions src/adaptateurs/journal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { enable, info } = require('diary');

const active = () => enable('*');
const consigne = info;

module.exports = { active, consigne };
10 changes: 8 additions & 2 deletions src/api/connexionFCPlus.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const { stockeDansCookieSession } = require('../routes/utils');

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

const secret = adaptateurEnvironnement.secretJetonSession();

Expand All @@ -13,8 +18,9 @@ const connexionFCPlus = (config, code, requete, reponse) => {
return stockeDansCookieSession(infos, adaptateurChiffrement, requete);
}))
.then(() => reponse.render('redirectionNavigateur', { destination: '/' }))
.catch(() => {
.catch((e) => {
requete.session.jeton = undefined;
journal.consigne(`Échec authentification (${e.message})`);
reponse.render('redirectionNavigateur', { destination: '/auth/fcplus/destructionSession' });
});
};
Expand Down
14 changes: 8 additions & 6 deletions src/routes/routesAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const routesAuth = (config) => {
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
} = config;

Expand Down Expand Up @@ -56,12 +57,13 @@ const routesAuth = (config) => {
(...args) => middleware.verifieTamponUnique(...args),
(requete, reponse) => {
const { code } = requete.query;
connexionFCPlus(
{ adaptateurChiffrement, adaptateurEnvironnement, fabriqueSessionFCPlus },
code,
requete,
reponse,
);
const adaptateurs = {
adaptateurChiffrement,
adaptateurEnvironnement,
fabriqueSessionFCPlus,
journal,
};
connexionFCPlus(adaptateurs, code, requete, reponse);
},
);

Expand Down
2 changes: 2 additions & 0 deletions src/siteVitrine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const creeServeur = (config) => {
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
} = config;
let serveur;
Expand All @@ -35,6 +36,7 @@ const creeServeur = (config) => {
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
}));

Expand Down
56 changes: 38 additions & 18 deletions test/api/connexionFCPlus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ describe('Le requêteur de connexion FC+', () => {
const adaptateurChiffrement = {};
const adaptateurEnvironnement = {};
const fabriqueSessionFCPlus = {};
const config = { adaptateurChiffrement, adaptateurEnvironnement, fabriqueSessionFCPlus };
const journal = {};
const config = {
adaptateurChiffrement,
adaptateurEnvironnement,
fabriqueSessionFCPlus,
journal,
};
const requete = {};
const reponse = {};

Expand All @@ -15,6 +21,7 @@ describe('Le requêteur de connexion FC+', () => {
fabriqueSessionFCPlus.nouvelleSession = () => Promise.resolve({
enJSON: () => Promise.resolve({}),
});
journal.consigne = () => {};
requete.session = {};
reponse.render = () => Promise.resolve();
reponse.status = () => reponse;
Expand All @@ -39,25 +46,38 @@ describe('Le requêteur de connexion FC+', () => {
.then(() => expect(requete.session.jeton).toBeUndefined());
});

it('redirige vers la destruction de session FC+ si le nonce retourné est différent du nonce en session', () => {
expect.assertions(2);
adaptateurChiffrement.verifieJeton = () => Promise.resolve({ nonce: 'unNonce' });
describe('quand nonce retourné diffère du nonce en session', () => {
beforeEach(() => {
adaptateurChiffrement.verifieJeton = () => Promise.resolve({ nonce: 'unNonce' });

requete.session.jeton = { nonce: 'abcde' };
fabriqueSessionFCPlus.nouvelleSession = () => Promise.resolve({
enJSON: () => Promise.resolve({ nonce: 'oups' }),
requete.session.jeton = { nonce: 'abcde' };
fabriqueSessionFCPlus.nouvelleSession = () => Promise.resolve({
enJSON: () => Promise.resolve({ nonce: 'oups' }),
});
});

it("journalise l'erreur", () => {
expect.assertions(1);

journal.consigne = (entree) => { expect(entree).toBe('Échec authentification (nonce invalide)'); };

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

reponse.render = (nomModelePage, { destination }) => {
try {
expect(nomModelePage).toBe('redirectionNavigateur');
expect(destination).toBe('/auth/fcplus/destructionSession');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

return connexionFCPlus(config, 'unCode', requete, reponse);
it('redirige vers la destruction de session FC+', () => {
expect.assertions(2);

reponse.render = (nomModelePage, { destination }) => {
try {
expect(nomModelePage).toBe('redirectionNavigateur');
expect(destination).toBe('/auth/fcplus/destructionSession');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

return connexionFCPlus(config, 'unCode', requete, reponse);
});
});
});
7 changes: 7 additions & 0 deletions test/routes/serveurTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const serveurTest = () => {
let adaptateurEnvironnement;
let adaptateurFranceConnectPlus;
let fabriqueSessionFCPlus;
let journal;
let middleware;

let serveur;
Expand Down Expand Up @@ -44,13 +45,18 @@ const serveurTest = () => {
nouvelleSession: () => Promise.resolve({ enJSON: () => Promise.resolve({}) }),
};

journal = {
consigne: () => {},
};

middleware = new MiddlewareFantaisie({});

serveur = OOTS_FRANCE.creeServeur({
adaptateurChiffrement,
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
});

Expand All @@ -65,6 +71,7 @@ const serveurTest = () => {
adaptateurFranceConnectPlus: () => adaptateurFranceConnectPlus,
arrete,
fabriqueSessionFCPlus: () => fabriqueSessionFCPlus,
journal: () => journal,
initialise,
middleware: () => middleware,
port,
Expand Down
Loading