diff --git a/app/(header-default)/entreprise/[slug]/page.tsx b/app/(header-default)/entreprise/[slug]/page.tsx index e0123b9a9..379f01d5f 100644 --- a/app/(header-default)/entreprise/[slug]/page.tsx +++ b/app/(header-default)/entreprise/[slug]/page.tsx @@ -56,7 +56,7 @@ export default async function UniteLegalePage(props: AppRouterProps) { const uniteLegale = await cachedGetUniteLegale(slug, isBot, page); const immatriculationEORI = hasRights(session, EScope.eori) - ? await getImmatriculationEORI(uniteLegale.siege.siret, session?.user) + ? await getImmatriculationEORI(uniteLegale.siege.siret) : null; return ( diff --git a/app/api/data-fetching/espace-agent/_helper.ts b/app/api/data-fetching/espace-agent/_helper.ts index adc978271..122f2b15e 100644 --- a/app/api/data-fetching/espace-agent/_helper.ts +++ b/app/api/data-fetching/espace-agent/_helper.ts @@ -10,7 +10,7 @@ export async function ProtectedAPIRoute( slug: string, administration: EAdministration, scope: EScope, - run: (agentSiret: string) => Promise + run: () => Promise ) { const session = await getSession(); try { @@ -18,9 +18,7 @@ export async function ProtectedAPIRoute( throw new HttpForbiddenError('Unauthorized account'); } - const agentSiret = session?.user?.siret || 'Inconnu'; - - const data = await run(agentSiret); + const data = await run(); return Response.json(data, { status: 200 }); } catch (e: any) { const message = `Failed to get donnees ${routeLabel}`; diff --git a/app/api/data-fetching/espace-agent/carte-professionnelle-TP/[slug]/route.ts b/app/api/data-fetching/espace-agent/carte-professionnelle-TP/[slug]/route.ts index 19f04e208..f25633808 100644 --- a/app/api/data-fetching/espace-agent/carte-professionnelle-TP/[slug]/route.ts +++ b/app/api/data-fetching/espace-agent/carte-professionnelle-TP/[slug]/route.ts @@ -1,7 +1,7 @@ import { EAdministration } from '#models/administrations/EAdministration'; import { getCarteProfessionnelleTravauxPublic } from '#models/espace-agent/carte-professionnelle-travaux-publics'; import { EScope } from '#models/user/rights'; -import { Siret, extractSirenFromSiret, verifySiret } from '#utils/helpers'; +import { extractSirenFromSiret, verifySiret } from '#utils/helpers'; import { ProtectedAPIRoute } from '../../_helper'; export async function GET( @@ -14,14 +14,11 @@ export async function GET( slug, EAdministration.FNTP, EScope.carteProfessionnelleTravauxPublics, - async (agentSiret: string) => { + async () => { const siret = verifySiret(slug as string); const siren = extractSirenFromSiret(siret); - return await getCarteProfessionnelleTravauxPublic( - siren, - agentSiret as Siret - ); + return await getCarteProfessionnelleTravauxPublic(siren); } ); } diff --git a/app/api/data-fetching/espace-agent/carte-professionnelle-travaux-public/[slug]/routes.ts b/app/api/data-fetching/espace-agent/carte-professionnelle-travaux-public/[slug]/routes.ts index e268abd7a..c8e9568dc 100644 --- a/app/api/data-fetching/espace-agent/carte-professionnelle-travaux-public/[slug]/routes.ts +++ b/app/api/data-fetching/espace-agent/carte-professionnelle-travaux-public/[slug]/routes.ts @@ -14,9 +14,9 @@ export async function GET( slug, EAdministration.DINUM, EScope.protectedCertificats, - async (agentSiret: string) => { + async () => { const siren = verifySiren(slug as string); - return await getCarteProfessionnelleTravauxPublic(siren, agentSiret); + return await getCarteProfessionnelleTravauxPublic(siren); } ); } diff --git a/app/api/data-fetching/espace-agent/conformite/[slug]/route.ts b/app/api/data-fetching/espace-agent/conformite/[slug]/route.ts index 7d197159c..e116480ad 100644 --- a/app/api/data-fetching/espace-agent/conformite/[slug]/route.ts +++ b/app/api/data-fetching/espace-agent/conformite/[slug]/route.ts @@ -14,11 +14,11 @@ export async function GET( slug, EAdministration.DINUM, EScope.conformite, - async (agentSiret: string) => { + async () => { const siret = verifySiret(slug as string); const siren = extractSirenFromSiret(siret); - return await getConformiteEntreprise(siren, siret, agentSiret); + return await getConformiteEntreprise(siren, siret); } ); } diff --git a/app/api/data-fetching/espace-agent/opqibi/[slug]/route.ts b/app/api/data-fetching/espace-agent/opqibi/[slug]/route.ts index 3230dcba3..56a439ce6 100644 --- a/app/api/data-fetching/espace-agent/opqibi/[slug]/route.ts +++ b/app/api/data-fetching/espace-agent/opqibi/[slug]/route.ts @@ -14,9 +14,9 @@ export async function GET( slug, EAdministration.DINUM, EScope.protectedCertificats, - async (agentSiret: string) => { + async () => { const siren = verifySiren(slug as string); - return await getOpqibi(siren, agentSiret); + return await getOpqibi(siren); } ); } diff --git a/app/api/data-fetching/espace-agent/qualibat/[slug]/route.ts b/app/api/data-fetching/espace-agent/qualibat/[slug]/route.ts index 853ccd0e8..beffdb817 100644 --- a/app/api/data-fetching/espace-agent/qualibat/[slug]/route.ts +++ b/app/api/data-fetching/espace-agent/qualibat/[slug]/route.ts @@ -14,9 +14,9 @@ export async function GET( slug, EAdministration.DINUM, EScope.protectedCertificats, - async (agentSiret: string) => { + async () => { const siret = verifySiret(slug as string); - return await getQualibat(siret, agentSiret); + return await getQualibat(siret); } ); } diff --git a/app/api/data-fetching/espace-agent/qualifelec/[slug]/route.ts b/app/api/data-fetching/espace-agent/qualifelec/[slug]/route.ts index 333999c1c..4f401f147 100644 --- a/app/api/data-fetching/espace-agent/qualifelec/[slug]/route.ts +++ b/app/api/data-fetching/espace-agent/qualifelec/[slug]/route.ts @@ -14,9 +14,9 @@ export async function GET( slug, EAdministration.DINUM, EScope.protectedCertificats, - async (agentSiret: string) => { + async () => { const siret = verifySiret(slug as string); - return await getQualifelec(siret, agentSiret); + return await getQualifelec(siret); } ); } diff --git a/clients/api-entreprise/carte-professionnelle-travaux-publics/index.ts b/clients/api-entreprise/carte-professionnelle-travaux-publics/index.ts index 63d7bd298..7d89b7ab4 100644 --- a/clients/api-entreprise/carte-professionnelle-travaux-publics/index.ts +++ b/clients/api-entreprise/carte-professionnelle-travaux-publics/index.ts @@ -13,8 +13,7 @@ export type IAPIEntrepriseCarteProfessionnelleTravauxPublics = * GET documents from API Entreprise */ export const clientApiEntrepriseCarteProfessionnelleTravauxPublics = async ( - siren: Siren, - recipientSiret?: string + siren: Siren ) => { return await clientAPIEntreprise< IAPIEntrepriseCarteProfessionnelleTravauxPublics, @@ -26,8 +25,7 @@ export const clientApiEntrepriseCarteProfessionnelleTravauxPublics = async ( '{siren}', siren )}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/client.ts b/clients/api-entreprise/client.ts index b90efc509..16f6fc3f4 100644 --- a/clients/api-entreprise/client.ts +++ b/clients/api-entreprise/client.ts @@ -2,6 +2,7 @@ import { HttpUnauthorizedError } from '#clients/exceptions'; import constants from '#models/constants'; import { Information } from '#models/exceptions'; import { httpGet } from '#utils/network'; +import { sensitiveRequestCallerInfos } from '#utils/network/utils/sensitive-request-caller-infos'; import { sensitiveRequestLogger } from '#utils/network/utils/sensitive-request-logger'; import { logInfoInSentry } from '#utils/sentry'; @@ -20,14 +21,16 @@ export type IAPIEntrepriseResponse = { */ export default async function clientAPIEntreprise( route: string, - mapToDomainObject: (e: T) => U, - recipientSiret?: string + mapToDomainObject: (e: T) => U ) { - if (!recipientSiret) { + const callerInfos = await sensitiveRequestCallerInfos(); + sensitiveRequestLogger(route, callerInfos); + + if (!callerInfos.siret) { logInfoInSentry( new Information({ name: 'NoRecipientSiretForAgent', - message: 'Fallback on Dinum siret as recipient', + message: `Fallback on Dinum siret for domain : ${callerInfos.domain}`, }) ); } @@ -36,8 +39,6 @@ export default async function clientAPIEntreprise( throw new HttpUnauthorizedError('Missing API Entreprise credentials'); } - await sensitiveRequestLogger(route); - // never cache any API Entreprise request const useCache = false; @@ -49,7 +50,7 @@ export default async function clientAPIEntreprise( params: { object: 'espace-agent-public', context: 'annuaire-entreprises', - recipient: recipientSiret || '13002526500013', + recipient: callerInfos.siret || '13002526500013', }, useCache, }); diff --git a/clients/api-entreprise/conformite/fiscale.ts b/clients/api-entreprise/conformite/fiscale.ts index bfe150ca8..e6371f81b 100644 --- a/clients/api-entreprise/conformite/fiscale.ts +++ b/clients/api-entreprise/conformite/fiscale.ts @@ -13,10 +13,7 @@ export type IAPIEntrepriseConformiteFiscale = IAPIEntrepriseResponse<{ /** * GET documents from API Entreprise */ -export const clientApiEntrepriseConformiteFiscale = async ( - siren: Siren, - recipientSiret?: string -) => { +export const clientApiEntrepriseConformiteFiscale = async (siren: Siren) => { return await clientAPIEntreprise< IAPIEntrepriseConformiteFiscale, IConformite @@ -24,8 +21,7 @@ export const clientApiEntrepriseConformiteFiscale = async ( `${ process.env.API_ENTREPRISE_URL }${routes.apiEntreprise.conformite.fiscale.replace('{siren}', siren)}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/conformite/msa.ts b/clients/api-entreprise/conformite/msa.ts index 219f48b55..922f1af14 100644 --- a/clients/api-entreprise/conformite/msa.ts +++ b/clients/api-entreprise/conformite/msa.ts @@ -10,16 +10,12 @@ export type IAPIEntrepriseConformiteMSA = IAPIEntrepriseResponse<{ /** * GET documents from API Entreprise */ -export const clientApiEntrepriseConformiteMSA = async ( - siret: Siret, - recipientSiret?: string -) => { +export const clientApiEntrepriseConformiteMSA = async (siret: Siret) => { return await clientAPIEntreprise( `${ process.env.API_ENTREPRISE_URL }${routes.apiEntreprise.conformite.msa.replace('{siret}', siret)}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/conformite/vigilance.ts b/clients/api-entreprise/conformite/vigilance.ts index 62419ea47..15bc9cfba 100644 --- a/clients/api-entreprise/conformite/vigilance.ts +++ b/clients/api-entreprise/conformite/vigilance.ts @@ -18,10 +18,7 @@ export type IAPIEntrepriseConformiteVigilance = IAPIEntrepriseResponse<{ /** * GET documents from API Entreprise */ -export const clientApiEntrepriseConformiteVigilance = async ( - siren: Siren, - recipientSiret?: string -) => { +export const clientApiEntrepriseConformiteVigilance = async (siren: Siren) => { return await clientAPIEntreprise< IAPIEntrepriseConformiteVigilance, IConformite @@ -29,8 +26,7 @@ export const clientApiEntrepriseConformiteVigilance = async ( `${ process.env.API_ENTREPRISE_URL }${routes.apiEntreprise.conformite.vigilance.replace('{siren}', siren)}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/immatriculation-eori/index.ts b/clients/api-entreprise/immatriculation-eori/index.ts index 72a8549b2..37f234541 100644 --- a/clients/api-entreprise/immatriculation-eori/index.ts +++ b/clients/api-entreprise/immatriculation-eori/index.ts @@ -17,10 +17,7 @@ export type IAPIEntrepriseImmatriculationEORI = IAPIEntrepriseResponse<{ /** * GET documents from API Entreprise */ -export const clientApiEntrepriseImmatriculationEORI = async ( - siret: Siret, - recipientSiret: Siret | undefined -) => { +export const clientApiEntrepriseImmatriculationEORI = async (siret: Siret) => { return await clientAPIEntreprise< IAPIEntrepriseImmatriculationEORI, IImmatriculationEORI @@ -28,8 +25,7 @@ export const clientApiEntrepriseImmatriculationEORI = async ( `${ process.env.API_ENTREPRISE_URL }${routes.apiEntreprise.immatriculationEORI.replace('{siret}', siret)}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/mandataires-rcs/index.ts b/clients/api-entreprise/mandataires-rcs/index.ts index 2bc22623e..7cb2618e4 100644 --- a/clients/api-entreprise/mandataires-rcs/index.ts +++ b/clients/api-entreprise/mandataires-rcs/index.ts @@ -4,7 +4,7 @@ import { IEtatCivil, IPersonneMorale, } from '#models/immatriculation'; -import { Siren, Siret } from '#utils/helpers'; +import { Siren } from '#utils/helpers'; import clientAPIEntreprise, { IAPIEntrepriseResponse } from '../client'; export type IAPIEntrepriseMandatairesRCS = IAPIEntrepriseResponse< Array< @@ -37,10 +37,7 @@ export type IAPIEntrepriseMandatairesRCS = IAPIEntrepriseResponse< /** * GET documents from API Entreprise */ -export const clientApiEntrepriseMandatairesRCS = async ( - siren: Siren, - recipientSiret: Siret | undefined -) => { +export const clientApiEntrepriseMandatairesRCS = async (siren: Siren) => { return await clientAPIEntreprise< IAPIEntrepriseMandatairesRCS, Array @@ -48,8 +45,7 @@ export const clientApiEntrepriseMandatairesRCS = async ( `${ process.env.API_ENTREPRISE_URL }${routes.apiEntreprise.mandatairesRCS.replace('{siren}', siren)}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/opqibi/index.ts b/clients/api-entreprise/opqibi/index.ts index 9aa937444..a7766297e 100644 --- a/clients/api-entreprise/opqibi/index.ts +++ b/clients/api-entreprise/opqibi/index.ts @@ -28,16 +28,12 @@ export type IAPIEntrepriseOpqibi = IAPIEntrepriseResponse<{ /** * GET documents from API Entreprise */ -export const clientApiEntrepriseOpqibi = async ( - siren: Siren, - recipientSiret?: string -) => { +export const clientApiEntrepriseOpqibi = async (siren: Siren) => { return await clientAPIEntreprise( `${ process.env.API_ENTREPRISE_URL }${routes.apiEntreprise.certifications.opqibi.replace('{siren}', siren)}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/qualibat/index.ts b/clients/api-entreprise/qualibat/index.ts index fd110215a..b05175715 100644 --- a/clients/api-entreprise/qualibat/index.ts +++ b/clients/api-entreprise/qualibat/index.ts @@ -29,16 +29,12 @@ export type IAPIEntrepriseQualibat = IAPIEntrepriseResponse<{ /** * GET documents from API Entreprise */ -export const clientApiEntrepriseQualibat = async ( - siret: Siret, - recipientSiret?: string -) => { +export const clientApiEntrepriseQualibat = async (siret: Siret) => { return await clientAPIEntreprise( `${ process.env.API_ENTREPRISE_URL }${routes.apiEntreprise.certifications.qualibat.replace('{siret}', siret)}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-entreprise/qualifelec/index.ts b/clients/api-entreprise/qualifelec/index.ts index bbe24fc54..27a7bd679 100644 --- a/clients/api-entreprise/qualifelec/index.ts +++ b/clients/api-entreprise/qualifelec/index.ts @@ -51,10 +51,7 @@ export type IAPIEntrepriseQualifelec = IAPIEntrepriseResponse< /** * GET documents from API Entreprise */ -export const clientApiEntrepriseQualifelec = async ( - siret: Siret, - recipientSiret?: string -) => { +export const clientApiEntrepriseQualifelec = async (siret: Siret) => { return await clientAPIEntreprise( `${ process.env.API_ENTREPRISE_URL @@ -62,8 +59,7 @@ export const clientApiEntrepriseQualifelec = async ( '{siret}', siret )}`, - mapToDomainObject, - recipientSiret + mapToDomainObject ); }; diff --git a/clients/api-proxy/rne/documents.ts b/clients/api-proxy/rne/documents.ts index faed0e900..0b75474bd 100644 --- a/clients/api-proxy/rne/documents.ts +++ b/clients/api-proxy/rne/documents.ts @@ -3,6 +3,7 @@ import stubClientWithSnapshots from '#clients/stub-client-with-snaphots'; import constants from '#models/constants'; import { IActesRNE } from '#models/immatriculation'; import { Siren } from '#utils/helpers'; +import { sensitiveRequestCallerInfos } from '#utils/network/utils/sensitive-request-caller-infos'; import { sensitiveRequestLogger } from '#utils/network/utils/sensitive-request-logger'; import { clientAPIProxy } from '../client'; @@ -12,7 +13,10 @@ import { clientAPIProxy } from '../client'; */ const fetchDocumentsFromRNE = async (siren: Siren, useCache = true) => { const route = routes.proxy.rne.documents.list + siren; - await sensitiveRequestLogger(route); + + const callerInfos = await sensitiveRequestCallerInfos(); + sensitiveRequestLogger(route, callerInfos); + return await clientAPIProxy(route, { timeout: constants.timeout.XXXXL, useCache, diff --git a/models/espace-agent/carte-professionnelle-travaux-publics.ts b/models/espace-agent/carte-professionnelle-travaux-publics.ts index 01d07deaa..3169cec2f 100644 --- a/models/espace-agent/carte-professionnelle-travaux-publics.ts +++ b/models/espace-agent/carte-professionnelle-travaux-publics.ts @@ -6,11 +6,13 @@ export type ICarteProfessionnelleTravauxPublics = { documentUrl: string; }; export const getCarteProfessionnelleTravauxPublic = async ( - siren: Siren, - recipientSiret?: string + siren: Siren ): Promise => { - return clientApiEntrepriseCarteProfessionnelleTravauxPublics( - siren, - recipientSiret - ).catch((error) => handleApiEntrepriseError(error, { siren })); + return clientApiEntrepriseCarteProfessionnelleTravauxPublics(siren).catch( + (error) => + handleApiEntrepriseError(error, { + siren, + apiResource: 'CarteProfessionnelleTravauxPublics', + }) + ); }; diff --git a/models/espace-agent/certificats/opqibi.ts b/models/espace-agent/certificats/opqibi.ts index a7ec3ba0a..12cfc2e81 100644 --- a/models/espace-agent/certificats/opqibi.ts +++ b/models/espace-agent/certificats/opqibi.ts @@ -21,10 +21,9 @@ export type IOpqibi = { }; export const getOpqibi = async ( - siren: Siren, - recipientSiret?: string + siren: Siren ): Promise => { - return clientApiEntrepriseOpqibi(siren, recipientSiret).catch((error) => - handleApiEntrepriseError(error, { siren }) + return clientApiEntrepriseOpqibi(siren).catch((error) => + handleApiEntrepriseError(error, { siren, apiResource: 'Opqibi' }) ); }; diff --git a/models/espace-agent/certificats/qualibat.ts b/models/espace-agent/certificats/qualibat.ts index 8eb231b38..28009f1a5 100644 --- a/models/espace-agent/certificats/qualibat.ts +++ b/models/espace-agent/certificats/qualibat.ts @@ -25,10 +25,9 @@ export type IQualibat = { }; export const getQualibat = async ( - siret: Siret, - recipientSiret?: string + siret: Siret ): Promise => { - return clientApiEntrepriseQualibat(siret, recipientSiret).catch((error) => - handleApiEntrepriseError(error, { siret }) + return clientApiEntrepriseQualibat(siret).catch((error) => + handleApiEntrepriseError(error, { siret, apiResource: 'Qualibat' }) ); }; diff --git a/models/espace-agent/certificats/qualifelec.ts b/models/espace-agent/certificats/qualifelec.ts index 869058613..083853449 100644 --- a/models/espace-agent/certificats/qualifelec.ts +++ b/models/espace-agent/certificats/qualifelec.ts @@ -41,10 +41,9 @@ export type IQualifelec = Array<{ }; }>; export const getQualifelec = async ( - siret: Siret, - recipientSiret?: string + siret: Siret ): Promise => { - return clientApiEntrepriseQualifelec(siret, recipientSiret).catch((error) => - handleApiEntrepriseError(error, { siret }) + return clientApiEntrepriseQualifelec(siret).catch((error) => + handleApiEntrepriseError(error, { siret, apiResource: 'Qualifelec' }) ); }; diff --git a/models/espace-agent/conformite.ts b/models/espace-agent/conformite.ts index 3aec15620..552f5b99d 100644 --- a/models/espace-agent/conformite.ts +++ b/models/espace-agent/conformite.ts @@ -19,18 +19,29 @@ export type IConformiteUniteLegale = { export const getConformiteEntreprise = async ( siren: Siren, - siret: Siret, - recipientSiret?: string + siret: Siret ): Promise => { const [fiscale, vigilance, msa] = await Promise.all([ - clientApiEntrepriseConformiteFiscale(siren, recipientSiret).catch((error) => - handleApiEntrepriseError(error, { siren, siret }) + clientApiEntrepriseConformiteFiscale(siren).catch((error) => + handleApiEntrepriseError(error, { + siren, + siret, + apiResource: 'ConformiteFiscale', + }) ), - clientApiEntrepriseConformiteVigilance(siren, recipientSiret).catch( - (error) => handleApiEntrepriseError(error, { siren, siret }) + clientApiEntrepriseConformiteVigilance(siren).catch((error) => + handleApiEntrepriseError(error, { + siren, + siret, + apiResource: 'ConformiteVigilance', + }) ), - clientApiEntrepriseConformiteMSA(siret, recipientSiret).catch((error) => - handleApiEntrepriseError(error, { siren, siret }) + clientApiEntrepriseConformiteMSA(siret).catch((error) => + handleApiEntrepriseError(error, { + siren, + siret, + apiResource: 'ConformiteMSA', + }) ), ]); diff --git a/models/espace-agent/immatriculation-eori.ts b/models/espace-agent/immatriculation-eori.ts index aead3d093..790559192 100644 --- a/models/espace-agent/immatriculation-eori.ts +++ b/models/espace-agent/immatriculation-eori.ts @@ -1,6 +1,5 @@ import { clientApiEntrepriseImmatriculationEORI } from '#clients/api-entreprise/immatriculation-eori'; import { IAPINotRespondingError } from '#models/api-not-responding'; -import { ISession } from '#models/user/session'; import { Siret } from '#utils/helpers'; import { handleApiEntrepriseError } from './utils'; @@ -11,10 +10,9 @@ export type IImmatriculationEORI = { }; export const getImmatriculationEORI = async ( - siret: Siret, - user: ISession['user'] | null + siret: Siret ): Promise => { - return clientApiEntrepriseImmatriculationEORI(siret, user?.siret).catch( - (error) => handleApiEntrepriseError(error, { siret }) + return clientApiEntrepriseImmatriculationEORI(siret).catch((error) => + handleApiEntrepriseError(error, { siret, apiResource: 'EORI' }) ); }; diff --git a/models/espace-agent/mandataires-rcs.ts b/models/espace-agent/mandataires-rcs.ts index 03a50831f..9cfebf59b 100644 --- a/models/espace-agent/mandataires-rcs.ts +++ b/models/espace-agent/mandataires-rcs.ts @@ -1,14 +1,13 @@ import { clientApiEntrepriseMandatairesRCS } from '#clients/api-entreprise/mandataires-rcs'; import { IAPINotRespondingError } from '#models/api-not-responding'; import { IDirigeant } from '#models/immatriculation'; -import { Siren, Siret } from '#utils/helpers'; +import { Siren } from '#utils/helpers'; import { handleApiEntrepriseError } from './utils'; export const getMandatairesRCS = async ( - siren: Siren, - userSiret?: Siret + siren: Siren ): Promise | IAPINotRespondingError> => { - return clientApiEntrepriseMandatairesRCS(siren, userSiret).catch((error) => - handleApiEntrepriseError(error, { siren }) + return clientApiEntrepriseMandatairesRCS(siren).catch((error) => + handleApiEntrepriseError(error, { siren, apiResource: 'MadatairesRCS' }) ); }; diff --git a/models/espace-agent/utils.ts b/models/espace-agent/utils.ts index 2a22fd7ab..23e156b93 100644 --- a/models/espace-agent/utils.ts +++ b/models/espace-agent/utils.ts @@ -4,7 +4,10 @@ import { APINotRespondingFactory } from '#models/api-not-responding'; import { FetchRessourceException, IExceptionContext } from '#models/exceptions'; import logErrorInSentry from '#utils/sentry'; -export function handleApiEntrepriseError(e: any, context: IExceptionContext) { +export function handleApiEntrepriseError( + e: any, + context: IExceptionContext & { apiResource: string } +) { if (e instanceof HttpNotFound) { return APINotRespondingFactory(EAdministration.DINUM, 404); } diff --git a/utils/network/utils/sensitive-request-caller-infos.ts b/utils/network/utils/sensitive-request-caller-infos.ts new file mode 100644 index 000000000..33e4169d1 --- /dev/null +++ b/utils/network/utils/sensitive-request-caller-infos.ts @@ -0,0 +1,27 @@ +import { HttpUnauthorizedError } from '#clients/exceptions'; +import getSession from '#utils/server-side-helper/app/get-session'; +import { ISensitiveCaller } from './sensitive-request-logger'; + +export async function sensitiveRequestCallerInfos(): Promise { + const session = await getSession(); + + if (session?.user) { + const { email, siret = null, scopes = [] } = session.user; + + if (!email) { + throw new HttpUnauthorizedError('Sensitive requests require an email'); + } + + const domain = (email.match(/@(.*)/) || ['']).shift() || ''; + + return { + email, + siret, + scopes, + domain, + }; + } + throw new HttpUnauthorizedError( + 'Sensitive requests require an authenticated user' + ); +} diff --git a/utils/network/utils/sensitive-request-logger.ts b/utils/network/utils/sensitive-request-logger.ts index 521d8260e..6ac8b58c7 100644 --- a/utils/network/utils/sensitive-request-logger.ts +++ b/utils/network/utils/sensitive-request-logger.ts @@ -1,7 +1,6 @@ import { Exception } from '#models/exceptions'; import { extractSirenOrSiretSlugFromUrl, randomId } from '#utils/helpers'; import logErrorInSentry from '#utils/sentry'; -import getSession from '#utils/server-side-helper/app/get-session'; type ISensitiveLogType = { date: string; @@ -20,12 +19,15 @@ type ISensitiveLogType = { path: string; query: string; }; - user?: { - email?: string; - siret?: string; - scopes?: string[]; - domain?: string; - }; + user: ISensitiveCaller; +}; + +// Elastic Common Schema : https://www.elastic.co/guide/en/ecs/current/ecs-user.html +export type ISensitiveCaller = { + email: string; + siret: string | null; + scopes: string[]; + domain: string; }; /** @@ -35,7 +37,10 @@ type ISensitiveLogType = { * * @param route */ -export const sensitiveRequestLogger = async (route: string) => { +export const sensitiveRequestLogger = ( + route: string, + user: ISensitiveCaller +) => { try { const url = new URL(route); @@ -66,22 +71,9 @@ export const sensitiveRequestLogger = async (route: string) => { path: url.pathname, query: url.search, }, + user, }; - const session = await getSession(); - - if (session?.user) { - // Elastic Common Schema : https://www.elastic.co/guide/en/ecs/current/ecs-user.html - log.user = { - email: session.user.email, - siret: session.user.siret, - scopes: session.user.scopes, - }; - - if (log.user.email) { - log.user.domain = (log.user.email.match(/@(.*)/) || ['']).shift(); - } - } // eslint-disable-next-line no-console console.info(JSON.stringify(log)); } catch (e) {