Skip to content

Commit

Permalink
fix(auth): Implement trusted origin handling for authentication requests
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixpereira committed Feb 5, 2025
1 parent 3a3549b commit e024c15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import { handlers } from '@/auth';
import { NextRequest } from 'next/server';

export const { GET, POST } = handlers;
const reqWithTrustedOrigin = (req: NextRequest): NextRequest => {
if (process.env.AUTH_TRUST_HOST !== 'true') return req;
const proto = req.headers.get('x-forwarded-proto');
const host = req.headers.get('x-forwarded-host');
if (!proto || !host) {
console.warn('Missing x-forwarded-proto or x-forwarded-host headers.');
return req;
}
const envOrigin = `${proto}://${host}`;
const { href, origin } = req.nextUrl;
return new NextRequest(href.replace(origin, envOrigin), req);
};

export const GET = (req: NextRequest) => {
return handlers.GET(reqWithTrustedOrigin(req));
};

export const POST = (req: NextRequest) => {
return handlers.POST(reqWithTrustedOrigin(req));
};
5 changes: 1 addition & 4 deletions src/components/Header/components/SignInJoin.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { env } from '@/env.mjs';
import { signIn } from 'next-auth/react';
import Link from 'next/link';
import Button from '../../Button';

export function SignInJoin() {
const redirectUri = `${env.NEXT_PUBLIC_KEYCLOAK_REDIRECT_URI}?registered`;
const authUrl = `${env.NEXT_PUBLIC_AUTH_KEYCLOAK_ISSUER}/protocol/openid-connect/auth?response_type=code&client_id=website&redirect_uri=${redirectUri}&scope=openid+profile+email`;
return (
<>
<Button colour="orange" href={authUrl}>
<Button colour="orange" onClick={() => signIn('keycloak')}>
Sign In
</Button>
<Button colour="purple" href="/join">
Expand Down

0 comments on commit e024c15

Please sign in to comment.