update api users and groups and users 5

This commit is contained in:
Alma 2025-04-09 20:52:22 +02:00
parent 6a5155e855
commit cfafcb312e

View File

@ -175,6 +175,24 @@ async function getAdminToken() {
} }
} }
// Validate username according to Keycloak requirements
function validateUsername(username: string): { isValid: boolean; error?: string } {
// Keycloak username requirements:
// - Only alphanumeric characters, dots (.), hyphens (-), and underscores (_)
// - Must start with a letter or number
// - Must be between 3 and 255 characters
const usernameRegex = /^[a-zA-Z0-9][a-zA-Z0-9._-]{2,254}$/;
if (!usernameRegex.test(username)) {
return {
isValid: false,
error: "Le nom d'utilisateur doit commencer par une lettre ou un chiffre, ne contenir que des lettres, chiffres, points, tirets et underscores, et faire entre 3 et 255 caractères"
};
}
return { isValid: true };
}
export async function POST(req: Request) { export async function POST(req: Request) {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
@ -186,6 +204,15 @@ export async function POST(req: Request) {
const data = await req.json(); const data = await req.json();
console.log("Creating user:", data); console.log("Creating user:", data);
// Validate username
const usernameValidation = validateUsername(data.username);
if (!usernameValidation.isValid) {
return NextResponse.json(
{ error: usernameValidation.error },
{ status: 400 }
);
}
const token = await getAdminToken(); const token = await getAdminToken();
if (!token) { if (!token) {
return NextResponse.json({ error: "Erreur d'authentification" }, { status: 401 }); return NextResponse.json({ error: "Erreur d'authentification" }, { status: 401 });