Skip to content

Commit

Permalink
Merge pull request #1105 from annuaire-entreprises-data-gouv-fr/1094-…
Browse files Browse the repository at this point in the history
…API-entreprise

Improve API Entreprise safety
  • Loading branch information
johangirod authored Jun 11, 2024
2 parents ba223ba + 8231686 commit fba58d7
Show file tree
Hide file tree
Showing 29 changed files with 136 additions and 141 deletions.
2 changes: 1 addition & 1 deletion app/(header-default)/entreprise/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
6 changes: 2 additions & 4 deletions app/api/data-fetching/espace-agent/_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ export async function ProtectedAPIRoute<T>(
slug: string,
administration: EAdministration,
scope: EScope,
run: (agentSiret: string) => Promise<T>
run: () => Promise<T>
) {
const session = await getSession();
try {
if (!hasRights(session, scope)) {
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}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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);
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
4 changes: 2 additions & 2 deletions app/api/data-fetching/espace-agent/conformite/[slug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
4 changes: 2 additions & 2 deletions app/api/data-fetching/espace-agent/opqibi/[slug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
4 changes: 2 additions & 2 deletions app/api/data-fetching/espace-agent/qualibat/[slug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
4 changes: 2 additions & 2 deletions app/api/data-fetching/espace-agent/qualifelec/[slug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,8 +25,7 @@ export const clientApiEntrepriseCarteProfessionnelleTravauxPublics = async (
'{siren}',
siren
)}`,
mapToDomainObject,
recipientSiret
mapToDomainObject
);
};

Expand Down
15 changes: 8 additions & 7 deletions clients/api-entreprise/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -20,14 +21,16 @@ export type IAPIEntrepriseResponse<T> = {
*/
export default async function clientAPIEntreprise<T, U>(
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}`,
})
);
}
Expand All @@ -36,8 +39,6 @@ export default async function clientAPIEntreprise<T, U>(
throw new HttpUnauthorizedError('Missing API Entreprise credentials');
}

await sensitiveRequestLogger(route);

// never cache any API Entreprise request
const useCache = false;

Expand All @@ -49,7 +50,7 @@ export default async function clientAPIEntreprise<T, U>(
params: {
object: 'espace-agent-public',
context: 'annuaire-entreprises',
recipient: recipientSiret || '13002526500013',
recipient: callerInfos.siret || '13002526500013',
},
useCache,
});
Expand Down
8 changes: 2 additions & 6 deletions clients/api-entreprise/conformite/fiscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ 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
>(
`${
process.env.API_ENTREPRISE_URL
}${routes.apiEntreprise.conformite.fiscale.replace('{siren}', siren)}`,
mapToDomainObject,
recipientSiret
mapToDomainObject
);
};

Expand Down
8 changes: 2 additions & 6 deletions clients/api-entreprise/conformite/msa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAPIEntrepriseConformiteMSA, IConformite>(
`${
process.env.API_ENTREPRISE_URL
}${routes.apiEntreprise.conformite.msa.replace('{siret}', siret)}`,
mapToDomainObject,
recipientSiret
mapToDomainObject
);
};

Expand Down
8 changes: 2 additions & 6 deletions clients/api-entreprise/conformite/vigilance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ 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
>(
`${
process.env.API_ENTREPRISE_URL
}${routes.apiEntreprise.conformite.vigilance.replace('{siren}', siren)}`,
mapToDomainObject,
recipientSiret
mapToDomainObject
);
};

Expand Down
8 changes: 2 additions & 6 deletions clients/api-entreprise/immatriculation-eori/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ 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
>(
`${
process.env.API_ENTREPRISE_URL
}${routes.apiEntreprise.immatriculationEORI.replace('{siret}', siret)}`,
mapToDomainObject,
recipientSiret
mapToDomainObject
);
};

Expand Down
10 changes: 3 additions & 7 deletions clients/api-entreprise/mandataires-rcs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -37,19 +37,15 @@ 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<IDirigeant>
>(
`${
process.env.API_ENTREPRISE_URL
}${routes.apiEntreprise.mandatairesRCS.replace('{siren}', siren)}`,
mapToDomainObject,
recipientSiret
mapToDomainObject
);
};

Expand Down
8 changes: 2 additions & 6 deletions clients/api-entreprise/opqibi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};

Expand Down
8 changes: 2 additions & 6 deletions clients/api-entreprise/qualibat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};

Expand Down
8 changes: 2 additions & 6 deletions clients/api-entreprise/qualifelec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,15 @@ 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<IAPIEntrepriseQualifelec, IQualifelec>(
`${
process.env.API_ENTREPRISE_URL
}${routes.apiEntreprise.certifications.qualifelec.replace(
'{siret}',
siret
)}`,
mapToDomainObject,
recipientSiret
mapToDomainObject
);
};

Expand Down
6 changes: 5 additions & 1 deletion clients/api-proxy/rne/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<IActesRNE>(route, {
timeout: constants.timeout.XXXXL,
useCache,
Expand Down
14 changes: 8 additions & 6 deletions models/espace-agent/carte-professionnelle-travaux-publics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export type ICarteProfessionnelleTravauxPublics = {
documentUrl: string;
};
export const getCarteProfessionnelleTravauxPublic = async (
siren: Siren,
recipientSiret?: string
siren: Siren
): Promise<ICarteProfessionnelleTravauxPublics | IAPINotRespondingError> => {
return clientApiEntrepriseCarteProfessionnelleTravauxPublics(
siren,
recipientSiret
).catch((error) => handleApiEntrepriseError(error, { siren }));
return clientApiEntrepriseCarteProfessionnelleTravauxPublics(siren).catch(
(error) =>
handleApiEntrepriseError(error, {
siren,
apiResource: 'CarteProfessionnelleTravauxPublics',
})
);
};
Loading

0 comments on commit fba58d7

Please sign in to comment.