widget email 10

This commit is contained in:
Alma 2025-04-13 21:18:32 +02:00
parent 1e592fe87c
commit 89028f3852
2 changed files with 25 additions and 41 deletions

View File

@ -14,8 +14,6 @@ declare module "next-auth" {
role: string[]; role: string[];
}; };
accessToken: string; accessToken: string;
rocketChatToken: string | null;
rocketChatUserId: string | null;
} }
interface JWT { interface JWT {
@ -26,8 +24,6 @@ declare module "next-auth" {
username: string; username: string;
first_name: string; first_name: string;
last_name: string; last_name: string;
rocketChatToken: string | null;
rocketChatUserId: string | null;
} }
} }
@ -46,7 +42,6 @@ export const authOptions: NextAuthOptions = {
clientSecret: getRequiredEnvVar("KEYCLOAK_CLIENT_SECRET"), clientSecret: getRequiredEnvVar("KEYCLOAK_CLIENT_SECRET"),
issuer: getRequiredEnvVar("KEYCLOAK_ISSUER"), issuer: getRequiredEnvVar("KEYCLOAK_ISSUER"),
profile(profile) { profile(profile) {
console.log("Keycloak profile received for user:", profile.preferred_username);
return { return {
id: profile.sub, id: profile.sub,
name: profile.name ?? profile.preferred_username, name: profile.name ?? profile.preferred_username,

View File

@ -6,62 +6,51 @@ export async function GET(req: NextRequest) {
try { try {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
if (!session?.user?.email || !session?.accessToken) { if (!session?.user?.email) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
} }
const nextcloudUrl = process.env.NEXTCLOUD_URL; const nextcloudUrl = process.env.NEXTCLOUD_URL;
if (!nextcloudUrl) { const clientId = process.env.NEXTCLOUD_CLIENT_ID;
console.error('Missing Nextcloud URL'); const clientSecret = process.env.NEXTCLOUD_CLIENT_SECRET;
if (!nextcloudUrl || !clientId || !clientSecret) {
console.error('Missing Nextcloud configuration');
return NextResponse.json( return NextResponse.json(
{ error: 'Nextcloud configuration is missing' }, { error: 'Nextcloud configuration is missing' },
{ status: 500 } { status: 500 }
); );
} }
// First, try to get the user's Nextcloud ID using the OCS API // First, get a Nextcloud OIDC token using client credentials
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/user`, { const tokenResponse = await fetch(`${nextcloudUrl}/index.php/apps/oauth2/api/v1/token`, {
method: 'POST',
headers: { headers: {
'Authorization': `Bearer ${session.accessToken}`, 'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json', 'Authorization': `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
'OCS-APIRequest': 'true',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
}, },
body: new URLSearchParams({
grant_type: 'client_credentials',
scope: 'ocs',
}),
}); });
if (!userInfoResponse.ok) { if (!tokenResponse.ok) {
console.error('Failed to get user info:', { const errorData = await tokenResponse.json();
status: userInfoResponse.status, console.error('Failed to get Nextcloud token:', {
statusText: userInfoResponse.statusText, status: tokenResponse.status,
url: userInfoResponse.url, statusText: tokenResponse.statusText,
error: errorData
}); });
return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 });
if (userInfoResponse.status === 401) {
return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 });
}
return NextResponse.json(
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
{ status: 404 }
);
} }
const userInfo = await userInfoResponse.json(); const { access_token } = await tokenResponse.json();
const userId = userInfo?.ocs?.data?.id;
if (!userId) { // Now try to access the Mail app using the Nextcloud token
console.error('Failed to get user ID from Nextcloud');
return NextResponse.json(
{ error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
{ status: 404 }
);
}
// Now try to access the Mail app using OCS API
const response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, { const response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, {
headers: { headers: {
'Authorization': `Bearer ${session.accessToken}`, 'Authorization': `Bearer ${access_token}`,
'Accept': 'application/json', 'Accept': 'application/json',
'OCS-APIRequest': 'true', 'OCS-APIRequest': 'true',
'Content-Type': 'application/json', 'Content-Type': 'application/json',