Skip to content

Commit

Permalink
chore: adapt files to the new api
Browse files Browse the repository at this point in the history
chore: use folder with route.ts files

fix: migrate back to pages router download/espace-agent/documents/

fix: separate types from routes files

fix: use full url for redirect in app/api

fix
  • Loading branch information
rmonnier9 committed Oct 14, 2024
1 parent 45685e4 commit d22d79d
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 143 deletions.
19 changes: 19 additions & 0 deletions app/api/auth/agent-connect/agent-connect-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Exception } from '#models/exceptions';

export class AgentConnectLogoutFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
...args,
name: 'LogoutFailedException',
});
}
}

export class AgentConnectFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'AgentConnectionFailedException',
...args,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { agentConnectAuthenticate } from '#clients/authentication/agent-connect/strategy';
import { HttpForbiddenError } from '#clients/exceptions';
import { Exception } from '#models/exceptions';
import { getAgent } from '#models/user/agent';
import { logFatalErrorInSentry } from '#utils/sentry';
import { cleanPathFrom, getPathFrom, setAgentSession } from '#utils/session';
import withSession from '#utils/session/with-session';
import { redirectTo } from '../../utils';
import { AgentConnectFailedException } from '../agent-connect-types';

export default withSession(async function callbackRoute(req, res) {
export const GET = withSession(async function callbackRoute(req) {
try {
const userInfo = await agentConnectAuthenticate(req);
const agent = await getAgent(userInfo);
Expand All @@ -17,25 +18,16 @@ export default withSession(async function callbackRoute(req, res) {

if (pathFrom) {
await cleanPathFrom(session);
res.redirect(pathFrom);
return redirectTo(req, pathFrom);
} else {
res.redirect('/');
return redirectTo(req, '/');
}
} catch (e: any) {
logFatalErrorInSentry(new AgentConnectionFailedException({ cause: e }));
logFatalErrorInSentry(new AgentConnectFailedException({ cause: e }));
if (e instanceof HttpForbiddenError) {
res.redirect('/connexion/echec-authorisation-requise');
return redirectTo(req, '/connexion/echec-authorisation-requise');
} else {
res.redirect('/connexion/echec-connexion');
return redirectTo(req, '/connexion/echec-connexion');
}
}
});

export class AgentConnectionFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'AgentConnectionFailedException',
...args,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import { agentConnectAuthorizeUrl } from '#clients/authentication/agent-connect/
import { logFatalErrorInSentry } from '#utils/sentry';
import { setPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { AgentConnectionFailedException } from '../../../../pages/api/auth/agent-connect/callback';
import { NextResponse } from 'next/server';
import { redirectTo } from '../../utils';
import { AgentConnectFailedException } from '../agent-connect-types';

export default withSession(async function loginRoute(req, res) {
export const GET = withSession(async function loginRoute(req) {
try {
// Get the pathFrom from query params or headers
const pathFrom =
req.nextUrl.searchParams.get('pathFrom') ||
req.headers.get('referer') ||
'';

await setPathFrom(req.session, pathFrom);
const url = await agentConnectAuthorizeUrl(req);
res.redirect(url);
return NextResponse.redirect(url);
} catch (e: any) {
logFatalErrorInSentry(new AgentConnectionFailedException({ cause: e }));
res.redirect('/connexion/echec-connexion');
logFatalErrorInSentry(new AgentConnectFailedException({ cause: e }));
return redirectTo(req, '/connexion/echec-connexion');
}
});
21 changes: 0 additions & 21 deletions app/api/auth/agent-connect/logout-callback.ts

This file was deleted.

22 changes: 22 additions & 0 deletions app/api/auth/agent-connect/logout-callback/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logErrorInSentry from '#utils/sentry';
import { cleanAgentSession, cleanPathFrom, getPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { redirectTo } from '../../utils';
import { AgentConnectLogoutFailedException } from '../agent-connect-types';

export const GET = withSession(async function logoutCallbackRoute(req) {
try {
const session = req.session;
await cleanAgentSession(session);
const pathFrom = getPathFrom(session);
if (pathFrom) {
await cleanPathFrom(session);
return redirectTo(req, pathFrom);
} else {
return redirectTo(req, '/connexion/au-revoir');
}
} catch (e: any) {
logErrorInSentry(new AgentConnectLogoutFailedException({ cause: e }));
return redirectTo(req, '/connexion/au-revoir');
}
});
25 changes: 0 additions & 25 deletions app/api/auth/agent-connect/logout.ts

This file was deleted.

18 changes: 18 additions & 0 deletions app/api/auth/agent-connect/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { agentConnectLogoutUrl } from '#clients/authentication/agent-connect/strategy';
import logErrorInSentry from '#utils/sentry';
import { setPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { redirectTo } from '../../utils';
import { AgentConnectLogoutFailedException } from '../agent-connect-types';

export const GET = withSession(async function logoutRoute(req) {
try {
await setPathFrom(req.session, req.headers.get('referer') || '');
const url = await agentConnectLogoutUrl(req);
return NextResponse.redirect(url);
} catch (e: any) {
logErrorInSentry(new AgentConnectLogoutFailedException({ cause: e }));
return redirectTo(req, '/connexion/au-revoir');
}
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { franceConnectAuthenticate } from '#clients/authentication/france-connect/strategy';
import { Exception } from '#models/exceptions';
import logErrorInSentry from '#utils/sentry';
import { setHidePersonalDataRequestFCSession } from '#utils/session';
import withSession from '#utils/session/with-session';
import { redirectTo } from '../../utils';
import { FranceConnectFailedException } from '../france-connect-types';

export default withSession(async function (req, res) {
export const GET = withSession(async function callbackRoute(req) {
try {
const userInfo = await franceConnectAuthenticate(req);
await setHidePersonalDataRequestFCSession(
Expand All @@ -15,18 +16,12 @@ export default withSession(async function (req, res) {
userInfo.sub,
req.session
);
res.redirect('/formulaire/supprimer-donnees-personnelles-entreprise');
return redirectTo(
req,
'/formulaire/supprimer-donnees-personnelles-entreprise'
);
} catch (e: any) {
logErrorInSentry(new FranceConnectFailedException({ cause: e }));
res.redirect('/connexion/echec-connexion');
return redirectTo(req, '/connexion/echec-connexion');
}
});

export class FranceConnectFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'FranceConnectFailedException',
...args,
});
}
}
19 changes: 19 additions & 0 deletions app/api/auth/france-connect/france-connect-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Exception } from '#models/exceptions';

export class FranceConnectFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'FranceConnectFailedException',
...args,
});
}
}

export class FranceConnectLogoutFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
...args,
name: 'LogoutFailedException',
});
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { FranceConnectAuthorizeUrl } from '#clients/authentication/france-connect/strategy';
import { logFatalErrorInSentry } from '#utils/sentry';
import withSession from '#utils/session/with-session';
import { FranceConnectFailedException } from './callback';
import { NextResponse } from 'next/server';
import { redirectTo } from '../../utils';
import { FranceConnectFailedException } from '../france-connect-types';

export default withSession(async function loginRoute(req, res) {
export const GET = withSession(async function loginRoute(req) {
try {
const url = await FranceConnectAuthorizeUrl(req);
res.redirect(url);
return NextResponse.redirect(url);
} catch (e: any) {
logFatalErrorInSentry(new FranceConnectFailedException({ cause: e }));
res.redirect('/connexion/echec-connexion');
return redirectTo(req, '/connexion/echec-connexion');
}
});
22 changes: 0 additions & 22 deletions app/api/auth/france-connect/logout-callback.ts

This file was deleted.

23 changes: 23 additions & 0 deletions app/api/auth/france-connect/logout-callback/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logErrorInSentry from '#utils/sentry';
import { getPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { redirectTo } from '../../utils';
import { FranceConnectLogoutFailedException } from '../france-connect-types';

export const GET = withSession(async function logoutCallbackRoute(req) {
try {
const pathFrom = getPathFrom(req.session);

req.session.destroy();
await req.session.save();

if (pathFrom) {
return redirectTo(req, pathFrom);
} else {
return redirectTo(req, '/connexion/au-revoir');
}
} catch (e: any) {
logErrorInSentry(new FranceConnectLogoutFailedException({ cause: e }));
return redirectTo(req, '/connexion/au-revoir');
}
});
25 changes: 0 additions & 25 deletions app/api/auth/france-connect/logout.ts

This file was deleted.

21 changes: 21 additions & 0 deletions app/api/auth/france-connect/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { franceConnectLogoutUrl } from '#clients/authentication/france-connect/strategy';
import logErrorInSentry from '#utils/sentry';
import { setPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { redirectTo } from '../../utils';
import { FranceConnectLogoutFailedException } from '../france-connect-types';

export const GET = withSession(async function logoutRoute(req) {
try {
await setPathFrom(
req.session,
(req.nextUrl.searchParams.get('pathFrom') || '') as string
);
const url = await franceConnectLogoutUrl(req);
return NextResponse.redirect(url);
} catch (e: any) {
logErrorInSentry(new FranceConnectLogoutFailedException({ cause: e }));
return redirectTo(req, '/connexion/au-revoir');
}
});
8 changes: 8 additions & 0 deletions app/api/auth/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NextRequest, NextResponse } from 'next/server';

export function redirectTo(req: NextRequest, path: string) {
const requestUrl = new URL(req.url);
const targetUrl = new URL(path, requestUrl.origin);

return NextResponse.redirect(targetUrl.toString());
}
4 changes: 2 additions & 2 deletions clients/authentication/agent-connect/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseClient, Issuer, generators } from 'openid-client';
import { HttpForbiddenError } from '#clients/exceptions';
import { IReqWithSession } from '#utils/session/with-session';
import { BaseClient, Issuer, generators } from 'openid-client';

let _client = undefined as BaseClient | undefined;

Expand Down Expand Up @@ -80,7 +80,7 @@ export type IAgentConnectUserInfo = {
export const agentConnectAuthenticate = async (req: IReqWithSession) => {
const client = await getClient();

const params = client.callbackParams(req);
const params = client.callbackParams(req.nextUrl.toString());

const tokenSet = await client.grant({
grant_type: 'authorization_code',
Expand Down
Loading

0 comments on commit d22d79d

Please sign in to comment.