#!/bin/bash # Script pour créer les utilisateurs de test dans Keycloak # Usage: ./setup_keycloak_test_users.sh set -e KEYCLOAK_URL="http://localhost:8180" REALM="unionflow" ADMIN_USER="admin" ADMIN_PASSWORD="admin" echo "🔐 Configuration des utilisateurs de test Keycloak" echo "==================================================" echo "" # 1. Obtenir le token admin echo "1️⃣ Obtention du token admin..." TOKEN_RESPONSE=$(curl -s -X POST "$KEYCLOAK_URL/realms/master/protocol/openid-connect/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=$ADMIN_USER" \ -d "password=$ADMIN_PASSWORD" \ -d "grant_type=password" \ -d "client_id=admin-cli") ADMIN_TOKEN=$(echo $TOKEN_RESPONSE | grep -o '"access_token":"[^"]*' | cut -d'"' -f4) if [ -z "$ADMIN_TOKEN" ]; then echo "❌ Échec obtention token admin" echo "Réponse: $TOKEN_RESPONSE" exit 1 fi echo "✅ Token admin obtenu: ${ADMIN_TOKEN:0:30}..." echo "" # 2. Vérifier si le realm unionflow existe echo "2️⃣ Vérification du realm '$REALM'..." REALM_CHECK=$(curl -s -o /dev/null -w "%{http_code}" -X GET \ "$KEYCLOAK_URL/admin/realms/$REALM" \ -H "Authorization: Bearer $ADMIN_TOKEN") if [ "$REALM_CHECK" != "200" ]; then echo "❌ Realm '$REALM' n'existe pas (HTTP $REALM_CHECK)" echo " Créez d'abord le realm via l'interface admin Keycloak" exit 1 fi echo "✅ Realm '$REALM' existe" echo "" # 3. Lister les utilisateurs existants echo "3️⃣ Liste des utilisateurs existants..." EXISTING_USERS=$(curl -s -X GET \ "$KEYCLOAK_URL/admin/realms/$REALM/users?max=100" \ -H "Authorization: Bearer $ADMIN_TOKEN") echo "$EXISTING_USERS" | grep -q '"username"' && echo " Utilisateurs trouvés:" && echo "$EXISTING_USERS" | grep -o '"username":"[^"]*' | cut -d'"' -f4 || echo " Aucun utilisateur existant" echo "" # 4. Créer l'utilisateur ORG_ADMIN echo "4️⃣ Création utilisateur orgadmin@unionflow.test..." ORG_ADMIN_PAYLOAD='{ "username": "orgadmin@unionflow.test", "email": "orgadmin@unionflow.test", "emailVerified": true, "enabled": true, "firstName": "Org", "lastName": "Admin", "credentials": [{ "type": "password", "value": "OrgAdmin@123", "temporary": false }] }' ORG_ADMIN_CREATE=$(curl -s -o /dev/null -w "%{http_code}" -X POST \ "$KEYCLOAK_URL/admin/realms/$REALM/users" \ -H "Authorization: Bearer $ADMIN_TOKEN" \ -H "Content-Type: application/json" \ -d "$ORG_ADMIN_PAYLOAD") if [ "$ORG_ADMIN_CREATE" = "201" ]; then echo "✅ Utilisateur orgadmin@unionflow.test créé (HTTP 201)" elif [ "$ORG_ADMIN_CREATE" = "409" ]; then echo "⚠️ Utilisateur orgadmin@unionflow.test existe déjà (HTTP 409)" else echo "❌ Échec création orgadmin@unionflow.test (HTTP $ORG_ADMIN_CREATE)" fi echo "" # 5. Créer l'utilisateur SUPER_ADMIN echo "5️⃣ Création utilisateur admin@unionflow.test..." SUPER_ADMIN_PAYLOAD='{ "username": "admin@unionflow.test", "email": "admin@unionflow.test", "emailVerified": true, "enabled": true, "firstName": "Super", "lastName": "Admin", "credentials": [{ "type": "password", "value": "Admin@123", "temporary": false }] }' SUPER_ADMIN_CREATE=$(curl -s -o /dev/null -w "%{http_code}" -X POST \ "$KEYCLOAK_URL/admin/realms/$REALM/users" \ -H "Authorization: Bearer $ADMIN_TOKEN" \ -H "Content-Type: application/json" \ -d "$SUPER_ADMIN_PAYLOAD") if [ "$SUPER_ADMIN_CREATE" = "201" ]; then echo "✅ Utilisateur admin@unionflow.test créé (HTTP 201)" elif [ "$SUPER_ADMIN_CREATE" = "409" ]; then echo "⚠️ Utilisateur admin@unionflow.test existe déjà (HTTP 409)" else echo "❌ Échec création admin@unionflow.test (HTTP $SUPER_ADMIN_CREATE)" fi echo "" # 6. Récupérer les IDs des utilisateurs créés echo "6️⃣ Récupération des IDs utilisateurs..." ORG_ADMIN_ID=$(curl -s -X GET \ "$KEYCLOAK_URL/admin/realms/$REALM/users?username=orgadmin@unionflow.test" \ -H "Authorization: Bearer $ADMIN_TOKEN" | grep -o '"id":"[^"]*' | head -1 | cut -d'"' -f4) SUPER_ADMIN_ID=$(curl -s -X GET \ "$KEYCLOAK_URL/admin/realms/$REALM/users?username=admin@unionflow.test" \ -H "Authorization: Bearer $ADMIN_TOKEN" | grep -o '"id":"[^"]*' | head -1 | cut -d'"' -f4) echo " orgadmin@unionflow.test ID: $ORG_ADMIN_ID" echo " admin@unionflow.test ID: $SUPER_ADMIN_ID" echo "" # 7. Assigner les rôles (si les rôles existent) echo "7️⃣ Attribution des rôles..." echo " ℹ️ Attribution manuelle requise via Keycloak Admin Console:" echo " - Aller à: $KEYCLOAK_URL/admin/master/console/#/unionflow/users" echo " - Sélectionner l'utilisateur orgadmin@unionflow.test" echo " - Onglet 'Role mapping' > Assigner le rôle ORG_ADMIN" echo " - Faire de même pour admin@unionflow.test avec SUPER_ADMIN" echo "" echo "==================================================" echo "✅ Configuration terminée!" echo "" echo "Utilisateurs créés:" echo " - orgadmin@unionflow.test / OrgAdmin@123 (ORG_ADMIN)" echo " - admin@unionflow.test / Admin@123 (SUPER_ADMIN)" echo "" echo "Prochaine étape:" echo " 1. Assigner les rôles manuellement (voir ci-dessus)" echo " 2. Exécuter: flutter test integration_test/" echo "=================================================="