166 lines
4.4 KiB
TypeScript
166 lines
4.4 KiB
TypeScript
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
|
import { NextResponse } from "next/server";
|
|
|
|
async function getAdminToken() {
|
|
try {
|
|
const tokenResponse = await fetch(
|
|
`${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: new URLSearchParams({
|
|
grant_type: 'client_credentials',
|
|
client_id: process.env.KEYCLOAK_CLIENT_ID!,
|
|
client_secret: process.env.KEYCLOAK_CLIENT_SECRET!,
|
|
}),
|
|
}
|
|
);
|
|
|
|
const data = await tokenResponse.json();
|
|
|
|
// Log the response for debugging
|
|
console.log('Token Response:', {
|
|
status: tokenResponse.status,
|
|
ok: tokenResponse.ok,
|
|
data: data
|
|
});
|
|
|
|
if (!tokenResponse.ok || !data.access_token) {
|
|
// Log the error details
|
|
console.error('Token Error Details:', {
|
|
status: tokenResponse.status,
|
|
data: data
|
|
});
|
|
return null;
|
|
}
|
|
|
|
return data.access_token;
|
|
} catch (error) {
|
|
console.error('Token Error:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) {
|
|
return NextResponse.json({ message: "Non autorisé" }, { status: 401 });
|
|
}
|
|
|
|
const token = await getAdminToken();
|
|
if (!token) {
|
|
return NextResponse.json({ message: "Erreur d'authentification" }, { status: 401 });
|
|
}
|
|
|
|
const response = await fetch(
|
|
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/groups`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
return NextResponse.json({ message: "Échec de la récupération des groupes" }, { status: response.status });
|
|
}
|
|
|
|
const groups = await response.json();
|
|
|
|
// Return empty array if no groups
|
|
if (!Array.isArray(groups)) {
|
|
return NextResponse.json([]);
|
|
}
|
|
|
|
const groupsWithCounts = await Promise.all(
|
|
groups.map(async (group: any) => {
|
|
try {
|
|
const countResponse = await fetch(
|
|
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/groups/${group.id}/members/count`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
}
|
|
);
|
|
|
|
let count = 0;
|
|
if (countResponse.ok) {
|
|
count = await countResponse.json();
|
|
}
|
|
|
|
return {
|
|
id: group.id,
|
|
name: group.name,
|
|
path: group.path,
|
|
membersCount: count,
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
id: group.id,
|
|
name: group.name,
|
|
path: group.path,
|
|
membersCount: 0,
|
|
};
|
|
}
|
|
})
|
|
);
|
|
|
|
return NextResponse.json(groupsWithCounts);
|
|
} catch (error) {
|
|
console.error('Groups API Error:', error);
|
|
return NextResponse.json({ message: "Une erreur est survenue" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) {
|
|
return NextResponse.json({ message: "Non autorisé" }, { status: 401 });
|
|
}
|
|
|
|
const { name } = await req.json();
|
|
if (!name?.trim()) {
|
|
return NextResponse.json(
|
|
{ message: "Le nom du groupe est requis" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const token = await getAdminToken();
|
|
|
|
const response = await fetch(
|
|
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/groups`,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ name }),
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Échec de la création du groupe');
|
|
}
|
|
|
|
return NextResponse.json({
|
|
id: Date.now().toString(),
|
|
name,
|
|
path: `/${name}`,
|
|
membersCount: 0
|
|
});
|
|
} catch (error) {
|
|
console.error('Create Group Error:', error);
|
|
return NextResponse.json(
|
|
{ message: error instanceof Error ? error.message : "Une erreur est survenue" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|