courrier msft oauth

This commit is contained in:
alma 2025-05-02 10:32:19 +02:00
parent 92b439dabf
commit 0ab1441fe4
3 changed files with 137 additions and 3 deletions

View File

@ -0,0 +1,126 @@
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
// Map of service prefixes to their base URLs
const SERVICE_URLS: Record<string, string> = {
'parole': process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL || '',
'alma': process.env.NEXT_PUBLIC_IFRAME_AI_ASSISTANT_URL || '',
'crm': process.env.NEXT_PUBLIC_IFRAME_MEDIATIONS_URL || '',
'vision': process.env.NEXT_PUBLIC_IFRAME_CONFERENCE_URL || '',
'showcase': process.env.NEXT_PUBLIC_IFRAME_SHOWCASE_URL || '',
'agilite': process.env.NEXT_PUBLIC_IFRAME_AGILITY_URL || '',
'dossiers': process.env.NEXT_PUBLIC_IFRAME_DRIVE_URL || '',
'the-message': process.env.NEXT_PUBLIC_IFRAME_THEMESSAGE_URL || '',
'qg': process.env.NEXT_PUBLIC_IFRAME_MISSIONVIEW_URL || ''
};
export async function GET(
request: NextRequest,
{ params }: { params: { path: string[] } }
) {
// Get the service prefix (first part of the path)
const serviceName = params.path[0];
const restOfPath = params.path.slice(1).join('/');
// Get the base URL for this service
const baseUrl = SERVICE_URLS[serviceName];
if (!baseUrl) {
return NextResponse.json({ error: 'Service not found' }, { status: 404 });
}
// Get the user's session
const session = await getServerSession(authOptions);
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// Get authentication token from session
const accessToken = session.accessToken;
if (!accessToken) {
return NextResponse.json({ error: 'No access token available' }, { status: 401 });
}
try {
// Extract search parameters
const searchParams = new URL(request.url).searchParams.toString();
const targetUrl = `${baseUrl}/${restOfPath}${searchParams ? `?${searchParams}` : ''}`;
// Forward the request to the target service with the authentication token
const response = await fetch(targetUrl, {
headers: {
'Authorization': `Bearer ${accessToken}`,
// Add other headers as needed by your services
}
});
// Get response data
const data = await response.arrayBuffer();
// Create response with the same status and headers
const newResponse = new NextResponse(data, {
status: response.status,
statusText: response.statusText,
headers: {
'Content-Type': response.headers.get('Content-Type') || 'application/octet-stream',
// Add other headers as needed
}
});
return newResponse;
} catch (error) {
console.error('Proxy error:', error);
return NextResponse.json({ error: 'Proxy error' }, { status: 500 });
}
}
export async function POST(
request: NextRequest,
{ params }: { params: { path: string[] } }
) {
// Similar implementation as GET but for POST requests
// This is a simplified version - you'd need to handle the request body
const serviceName = params.path[0];
const restOfPath = params.path.slice(1).join('/');
const baseUrl = SERVICE_URLS[serviceName];
if (!baseUrl) {
return NextResponse.json({ error: 'Service not found' }, { status: 404 });
}
const session = await getServerSession(authOptions);
if (!session || !session.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
try {
const searchParams = new URL(request.url).searchParams.toString();
const targetUrl = `${baseUrl}/${restOfPath}${searchParams ? `?${searchParams}` : ''}`;
// Get the request body
const body = await request.arrayBuffer();
const response = await fetch(targetUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${session.accessToken}`,
'Content-Type': request.headers.get('Content-Type') || 'application/json',
},
body: body
});
const data = await response.arrayBuffer();
return new NextResponse(data, {
status: response.status,
statusText: response.statusText,
headers: {
'Content-Type': response.headers.get('Content-Type') || 'application/octet-stream',
}
});
} catch (error) {
console.error('Proxy error:', error);
return NextResponse.json({ error: 'Proxy error' }, { status: 500 });
}
}

View File

@ -7,11 +7,16 @@ interface ResponsiveIframeProps {
className?: string;
allow?: string;
style?: React.CSSProperties;
token?: string;
}
export function ResponsiveIframe({ src, className = '', allow, style }: ResponsiveIframeProps) {
export function ResponsiveIframe({ src, className = '', allow, style, token }: ResponsiveIframeProps) {
const iframeRef = useRef<HTMLIFrameElement>(null);
const fullSrc = token ?
`${src}${src.includes('?') ? '&' : '?'}token=${encodeURIComponent(token)}` :
src;
useEffect(() => {
const iframe = iframeRef.current;
if (!iframe) return;
@ -58,7 +63,7 @@ export function ResponsiveIframe({ src, className = '', allow, style }: Responsi
<iframe
ref={iframeRef}
id="myFrame"
src={src}
src={fullSrc}
className={`w-full border-none ${className}`}
style={{
display: 'block',

View File

@ -10,11 +10,14 @@ export default async function Page() {
redirect("/signin");
}
// Use the proxy URL instead of the direct service URL
const proxyUrl = `/api/proxy/parole`;
return (
<main className="w-full h-screen bg-black">
<div className="w-full h-full px-4 pt-12 pb-4">
<ResponsiveIframe
src={process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL || ''}
src={proxyUrl}
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
/>
</div>