# Script pour créer un utilisateur de test dans le realm unionflow param( [string]$KeycloakUrl = "http://192.168.1.11:8180", [string]$Realm = "unionflow", [string]$AdminUser = "admin", [string]$AdminPassword = "admin", [string]$Username = "testuser", [string]$Password = "password123", [string]$Email = "test@unionflow.com", [string]$FirstName = "Test", [string]$LastName = "User" ) Write-Host "👤 Création d'un utilisateur de test dans Keycloak..." -ForegroundColor Cyan Write-Host "📍 Keycloak URL: $KeycloakUrl" -ForegroundColor Gray Write-Host "🏛️ Realm: $Realm" -ForegroundColor Gray Write-Host "👤 Username: $Username" -ForegroundColor Gray Write-Host "" try { # 1. Obtenir le token d'administration Write-Host "🔑 Obtention du token d'administration..." -ForegroundColor Yellow $tokenBody = @{ username = $AdminUser password = $AdminPassword grant_type = "password" client_id = "admin-cli" } $tokenResponse = Invoke-RestMethod -Uri "$KeycloakUrl/realms/master/protocol/openid-connect/token" ` -Method Post ` -ContentType "application/x-www-form-urlencoded" ` -Body $tokenBody $accessToken = $tokenResponse.access_token Write-Host "✅ Token obtenu avec succès" -ForegroundColor Green # 2. Créer l'utilisateur Write-Host "👤 Création de l'utilisateur '$Username'..." -ForegroundColor Yellow $userConfig = @{ username = $Username email = $Email firstName = $FirstName lastName = $LastName enabled = $true emailVerified = $true } | ConvertTo-Json $headers = @{ "Authorization" = "Bearer $accessToken" "Content-Type" = "application/json" } try { $userResponse = Invoke-RestMethod -Uri "$KeycloakUrl/admin/realms/$Realm/users" ` -Method Post ` -Headers $headers ` -Body $userConfig Write-Host "✅ Utilisateur créé avec succès" -ForegroundColor Green } catch { if ($_.Exception.Response.StatusCode -eq 409) { Write-Host "⚠️ L'utilisateur existe déjà, mise à jour..." -ForegroundColor Yellow } else { throw } } # 3. Obtenir l'ID de l'utilisateur Write-Host "🔍 Recherche de l'utilisateur..." -ForegroundColor Yellow $usersResponse = Invoke-RestMethod -Uri "$KeycloakUrl/admin/realms/$Realm/users?username=$Username" ` -Method Get ` -Headers $headers if ($usersResponse.Count -eq 0) { throw "Utilisateur non trouvé après création" } $userId = $usersResponse[0].id Write-Host "✅ Utilisateur trouvé: $userId" -ForegroundColor Green # 4. Définir le mot de passe Write-Host "🔒 Définition du mot de passe..." -ForegroundColor Yellow $passwordConfig = @{ type = "password" value = $Password temporary = $false } | ConvertTo-Json Invoke-RestMethod -Uri "$KeycloakUrl/admin/realms/$Realm/users/$userId/reset-password" ` -Method Put ` -Headers $headers ` -Body $passwordConfig Write-Host "✅ Mot de passe défini avec succès" -ForegroundColor Green Write-Host "" Write-Host "🎉 UTILISATEUR CRÉÉ AVEC SUCCÈS !" -ForegroundColor Green Write-Host "📧 Email: $Email" -ForegroundColor White Write-Host "👤 Username: $Username" -ForegroundColor White Write-Host "🔒 Password: $Password" -ForegroundColor White Write-Host "" Write-Host "Vous pouvez maintenant vous connecter via l'application mobile !" -ForegroundColor Cyan } catch { Write-Host "" Write-Host "❌ ERREUR lors de la création de l'utilisateur !" -ForegroundColor Red Write-Host "Détails: $($_.Exception.Message)" -ForegroundColor Red if ($_.Exception.Response) { $statusCode = $_.Exception.Response.StatusCode.value__ Write-Host "Code de statut HTTP: $statusCode" -ForegroundColor Red } Write-Host "" Write-Host "Solutions possibles:" -ForegroundColor Yellow Write-Host "1. Verifiez que Keycloak est accessible sur $KeycloakUrl" -ForegroundColor Gray Write-Host "2. Verifiez que le realm '$Realm' existe" -ForegroundColor Gray Write-Host "3. Verifiez les identifiants admin (admin/admin)" -ForegroundColor Gray exit 1 }