Skip to content

Commit

Permalink
Transfère un jeton utilisateur lors de la récupération de document
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabinout committed Dec 3, 2024
1 parent 0f56b9f commit b5398c8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
12 changes: 12 additions & 0 deletions src/adaptateurs/adaptateurChiffrement.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ const verifieSignatureJWTDepuisJWKS = (jwt, urlJWKS) => {
return verifieJeton(jwt, jwks);
};

const genereJWT = (infos) => {
const headerJWT = {
alg: 'RS256',
};

return jose.importJWK(adaptateurEnvironnement.clePriveeJWK())
.then((clePrivee) => new jose.SignJWT(infos)
.setProtectedHeader(headerJWT)
.sign(clePrivee));
};

module.exports = {
cleHachage,
dechiffreJWE,
genereJWT,
verifieSignatureJWTDepuisJWKS,
};
12 changes: 11 additions & 1 deletion src/api/urlOOTS.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
const urlOOTS = (adaptateurEnvironnement) => `${adaptateurEnvironnement.urlBaseOOTSFrance()}/requete/pieceJustificative?codeDemarche=00&codePays=FR&idRequeteur=${adaptateurEnvironnement.identifiantRequeteur()}`;
const urlOOTS = (config, requete) => {
const { adaptateurChiffrement, adaptateurEnvironnement } = config;
const { infosUtilisateur } = requete.session;

return adaptateurChiffrement.genereJWT(infosUtilisateur)
.then((jeton) => {
const requeteur = adaptateurEnvironnement.identifiantRequeteur();
const urlOOTSFrance = adaptateurEnvironnement.urlBaseOOTSFrance();
return `${urlOOTSFrance}/requete/pieceJustificative?codeDemarche=00&codePays=FR&idRequeteur=${requeteur}&utilisateur=${jeton}`;
});
};

module.exports = urlOOTS;
20 changes: 12 additions & 8 deletions src/routes/routesOOTS.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const express = require('express');

const urlOOTS = require('../api/urlOOTS');

const routesOOTS = (config) => {
const { adaptateurEnvironnement, depotDonnees } = config;
const { adaptateurChiffrement, adaptateurEnvironnement, depotDonnees } = config;
const routes = express.Router();

routes.get('/document', (requete, reponse) => depotDonnees
.demarreRecuperationDocument()
.then(() => reponse.render('redirectionNavigateur', { destination: urlOOTS(adaptateurEnvironnement, requete) })));
routes.get('/document', (requete, reponse) => (
depotDonnees
.demarreRecuperationDocument()
.then(() => urlOOTS({ adaptateurEnvironnement, adaptateurChiffrement }, requete))
.then((destination) => reponse.render('redirectionNavigateur', { destination }))
));

routes.post('/document', (requete, reponse) => depotDonnees
.termineRecuperationDocument(Buffer.from(requete.body.document))
.then(() => reponse.send()));
routes.post('/document', (requete, reponse) => (
depotDonnees
.termineRecuperationDocument(Buffer.from(requete.body.document))
.then(() => reponse.send())
));

routes.get('/callback', (requete, reponse) => {
reponse.render('redirectionNavigateur', { destination: '/' });
Expand Down
2 changes: 1 addition & 1 deletion src/siteVitrine.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const creeServeur = (config) => {
app.use(
'/oots',
protegeRouteAvecOOTS(adaptateurEnvironnement),
routesOOTS({ adaptateurEnvironnement, depotDonnees }),
routesOOTS({ adaptateurChiffrement, adaptateurEnvironnement, depotDonnees }),
);

app.use('/', routesBase({ adaptateurEnvironnement, depotDonnees, middleware }));
Expand Down
31 changes: 25 additions & 6 deletions test/api/urlOOTS.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
const urlOOTS = require('../../src/api/urlOOTS');

describe("Le constructeur de l'URL de requête OOTS-France", () => {
const adaptateurChiffrement = {};
const adaptateurEnvironnement = {};
const requete = {};

beforeEach(() => {
adaptateurEnvironnement.avecOOTS = () => true;
adaptateurEnvironnement.urlBaseOOTSFrance = () => '';
adaptateurEnvironnement.identifiantRequeteur = () => '';
adaptateurChiffrement.genereJWT = () => Promise.resolve();
requete.session = { infosUtilisateur: {} };
});

it('retourne un lien vers OOTS', () => {
adaptateurEnvironnement.urlBaseOOTSFrance = () => 'http://example.com';
const url = urlOOTS(adaptateurEnvironnement);

expect(url).toMatch(/^http:\/\/example\.com.*/);
urlOOTS(
{ adaptateurChiffrement, adaptateurEnvironnement },
requete,
).then((url) => expect(url).toMatch(/^http:\/\/example\.com.*/));
});

it("contient l'identifiant de requeteur", () => {
it('contient l\'identifiant de requeteur', () => {
adaptateurEnvironnement.identifiantRequeteur = () => 'un-identifiant';
const url = urlOOTS(adaptateurEnvironnement);
urlOOTS(
{ adaptateurChiffrement, adaptateurEnvironnement },
requete,
).then((url) => expect(url).toContain('idRequeteur=un-identifiant'));
});

it('contient le jeton utilisateur', () => {
requete.session.infosUtilisateur = { prenom: 'Pierre', nom: 'Jax' };
adaptateurChiffrement.genereJWT = (infosutilisateur) => {
expect(infosutilisateur).toStrictEqual({ prenom: 'Pierre', nom: 'Jax' });
return Promise.resolve('unJeton');
};

expect(url).toContain('idRequeteur=un-identifiant');
urlOOTS(
{ adaptateurChiffrement, adaptateurEnvironnement },
requete,
).then((url) => expect(url).toContain('unJeton'));
});
});
1 change: 1 addition & 0 deletions test/routes/serveurTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const serveurTest = () => {
adaptateurChiffrement = {
cleHachage: () => '',
dechiffreJWE: () => Promise.resolve(),
genereJWT: () => Promise.resolve(''),
verifieSignatureJWTDepuisJWKS: () => Promise.resolve({}),
};

Expand Down

0 comments on commit b5398c8

Please sign in to comment.