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

Filter agent on Annuaire side rather than ProConnect side #1260

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions pages/api/auth/agent-connect/callback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { agentConnectAuthenticate } from '#clients/authentication/agent-connect/strategy';
import { HttpForbiddenError } from '#clients/exceptions';
import { clientUniteLegaleRechercheEntreprise } from '#clients/recherche-entreprise/siren';
import { isServicePublic } from '#models/core/types';
import { Exception } from '#models/exceptions';
import { getAgent } from '#models/user/agent';
import { isAgentScope } from '#models/user/scopes';
import { extractSirenFromSiret } from '#utils/helpers';
import { logFatalErrorInSentry } from '#utils/sentry';
import { cleanPathFrom, getPathFrom, setAgentSession } from '#utils/session';
import withSession from '#utils/session/with-session';
Expand All @@ -10,6 +14,26 @@ export default withSession(async function callbackRoute(req, res) {
try {
const userInfo = await agentConnectAuthenticate(req);
const agent = await getAgent(userInfo);

const isWhitelisted = agent.scopes.some((scope) => isAgentScope(scope));
const { isMCP } = agent;

if (!isWhitelisted && isMCP) {
const siren = extractSirenFromSiret(agent.siret);
const uniteLegale = await clientUniteLegaleRechercheEntreprise(siren, 0);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather use getUniteLegaleFromSlug here as we want redundancy

Copy link
Contributor Author

@rmonnier9 rmonnier9 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not possible to use getUniteLegaleFromSlug because it uses next/headers and we are inside pages/api.
What do you advise ?
Using Client OR migrating to app/api OR adapt getUniteLegaleFromSlug ?

const isNotServicePublic = !isServicePublic(uniteLegale);
// TODO filter base on uniteLegal if it's not a service public for sure
const couldBeServicePublic = true;
rmonnier9 marked this conversation as resolved.
Show resolved Hide resolved

if (isNotServicePublic) {
if (couldBeServicePublic) {
return res.redirect('/connexion/habilitation-requise');
} else {
return res.redirect('/connexion/echec-autorisation-requise');
}
}
}
const session = req.session;
await setAgentSession(agent, session);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ReactElement } from 'react';
import connexionRefusedPicture from '#components-ui/illustrations/connexion-refused';
import { LayoutConnexion } from '#components/layouts/layout-connexion';
import Meta from '#components/meta/meta-client';
import constants from '#models/constants';
import { NextPageWithLayout } from 'pages/_app';
import { ReactElement } from 'react';

const ConnexionFailure: NextPageWithLayout = () => (
<>
Expand Down
38 changes: 38 additions & 0 deletions pages/connexion/habilitation-requise.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import connexionRefusedPicture from '#components-ui/illustrations/connexion-refused';
import { LayoutConnexion } from '#components/layouts/layout-connexion';
import Meta from '#components/meta/meta-client';
import { NextPageWithLayout } from 'pages/_app';
import { ReactElement } from 'react';

const ConnexionFailure: NextPageWithLayout = () => (
<>
<Meta
title="Vous n’êtes pas autorisé(e) à accéder à cette partie du site"
noIndex={true}
/>
<h1>Vous n’êtes pas autorisé(e) à accéder à cette partie du site</h1>
<p>
Votre compte ProConnect doit être habilité pour être utilisé sur ce site.
</p>
<p>
Vous souhaitez obtenir l‘habilitation&nbsp;:{' '}
<a
href="https://www.demarches-simplifiees.fr/commencer/7991f7ad-97d9-4bf5-a326-ae37cc858081"
target="_blank"
rel="noopener noreferrer"
>
remplissez ce formulaire
</a>
.
</p>
<a href="/">← Retourner au moteur de recherche</a>
</>
);

ConnexionFailure.getLayout = function getLayout(page: ReactElement) {
return (
<LayoutConnexion img={connexionRefusedPicture}>{page}</LayoutConnexion>
);
};

export default ConnexionFailure;
Loading