@echo off REM Script d'automatisation de la configuration Keycloak pour UnionFlow REM Usage: setup-keycloak.bat echo. echo ======================================================== echo 🔧 Configuration automatique de Keycloak pour UnionFlow echo ======================================================== echo. REM Configuration set KEYCLOAK_URL=http://localhost:8180 set ADMIN_USER=admin set ADMIN_PASS=admin set REALM_NAME=unionflow set CLIENT_ID=unionflow-client echo 📋 Paramètres: echo - Keycloak URL: %KEYCLOAK_URL% echo - Admin User: %ADMIN_USER% echo - Realm: %REALM_NAME% echo - Client ID: %CLIENT_ID% echo. REM Vérifier que Keycloak est accessible echo 🔍 Vérification de Keycloak... curl -s %KEYCLOAK_URL% >nul 2>&1 if errorlevel 1 ( echo ❌ ERREUR: Keycloak n'est pas accessible sur %KEYCLOAK_URL% echo Assurez-vous que Keycloak est démarré. pause exit /b 1 ) echo ✅ Keycloak est accessible echo. REM Étape 1: Obtenir le token admin echo 📝 Étape 1/7: Obtention du token admin... 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_PASS%" ^ -d "grant_type=password" ^ -d "client_id=admin-cli" > token_response.json REM Extraire le token (utilise PowerShell pour parser le JSON) for /f "delims=" %%i in ('powershell -Command "(Get-Content token_response.json | ConvertFrom-Json).access_token"') do set ADMIN_TOKEN=%%i if "%ADMIN_TOKEN%"=="" ( echo ❌ ERREUR: Impossible d'obtenir le token admin echo Vérifiez les identifiants: %ADMIN_USER% / %ADMIN_PASS% del token_response.json 2>nul pause exit /b 1 ) echo ✅ Token admin obtenu echo. REM Étape 2: Créer le realm echo 📝 Étape 2/7: Création du realm '%REALM_NAME%'... curl -s -X POST "%KEYCLOAK_URL%/admin/realms" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"realm\":\"%REALM_NAME%\",\"enabled\":true,\"displayName\":\"UnionFlow\",\"registrationAllowed\":false,\"loginWithEmailAllowed\":true,\"duplicateEmailsAllowed\":false,\"resetPasswordAllowed\":true,\"editUsernameAllowed\":false,\"bruteForceProtected\":true}" > nul 2>&1 if errorlevel 1 ( echo ⚠️ Le realm existe peut-être déjà, continuation... ) else ( echo ✅ Realm créé ) echo. REM Étape 3: Créer les rôles echo 📝 Étape 3/7: Création des rôles... curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/roles" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"name\":\"SUPER_ADMIN\",\"description\":\"Super administrateur système\"}" > nul 2>&1 echo ✅ Rôle SUPER_ADMIN créé curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/roles" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"name\":\"ADMIN_ENTITE\",\"description\":\"Administrateur d'entité\"}" > nul 2>&1 echo ✅ Rôle ADMIN_ENTITE créé curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/roles" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"name\":\"MEMBRE\",\"description\":\"Membre standard\"}" > nul 2>&1 echo ✅ Rôle MEMBRE créé curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/roles" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"name\":\"GESTIONNAIRE_MEMBRE\",\"description\":\"Gestionnaire des membres\"}" > nul 2>&1 echo ✅ Rôle GESTIONNAIRE_MEMBRE créé curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/roles" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"name\":\"GESTIONNAIRE_EVENEMENT\",\"description\":\"Gestionnaire des événements\"}" > nul 2>&1 echo ✅ Rôle GESTIONNAIRE_EVENEMENT créé echo. REM Étape 4: Créer le client echo 📝 Étape 4/7: Création du client '%CLIENT_ID%'... curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/clients" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"clientId\":\"%CLIENT_ID%\",\"enabled\":true,\"protocol\":\"openid-connect\",\"publicClient\":false,\"directAccessGrantsEnabled\":true,\"standardFlowEnabled\":true,\"implicitFlowEnabled\":false,\"serviceAccountsEnabled\":false,\"authorizationServicesEnabled\":false,\"rootUrl\":\"http://localhost:8086\",\"baseUrl\":\"http://localhost:8086\",\"redirectUris\":[\"http://localhost:8086/*\"],\"webOrigins\":[\"http://localhost:8086\"],\"attributes\":{\"post.logout.redirect.uris\":\"http://localhost:8086/*\"}}" > nul 2>&1 if errorlevel 1 ( echo ⚠️ Le client existe peut-être déjà, continuation... ) else ( echo ✅ Client créé ) echo. REM Étape 5: Récupérer le client ID (UUID) et le secret echo 📝 Étape 5/7: Récupération du client secret... curl -s -X GET "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/clients" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" > clients.json REM Extraire le client UUID for /f "delims=" %%i in ('powershell -Command "(Get-Content clients.json | ConvertFrom-Json) | Where-Object {$_.clientId -eq '%CLIENT_ID%'} | Select-Object -ExpandProperty id"') do set CLIENT_UUID=%%i if "%CLIENT_UUID%"=="" ( echo ❌ ERREUR: Impossible de trouver le client UUID del token_response.json clients.json 2>nul pause exit /b 1 ) REM Récupérer le secret du client curl -s -X GET "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/clients/%CLIENT_UUID%/client-secret" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" > secret.json for /f "delims=" %%i in ('powershell -Command "(Get-Content secret.json | ConvertFrom-Json).value"') do set CLIENT_SECRET=%%i if "%CLIENT_SECRET%"=="" ( echo ❌ ERREUR: Impossible de récupérer le client secret del token_response.json clients.json secret.json 2>nul pause exit /b 1 ) echo ✅ Client Secret récupéré: %CLIENT_SECRET% echo. REM Étape 6: Configurer le client scope mapper pour les rôles echo 📝 Étape 6/7: Configuration du mapper de rôles... REM Récupérer le client scope dédié curl -s -X GET "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/clients/%CLIENT_UUID%/default-client-scopes" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" > scopes.json REM Trouver le scope dédié (généralement unionflow-client-dedicated) for /f "delims=" %%i in ('powershell -Command "(Get-Content scopes.json | ConvertFrom-Json) | Where-Object {$_.name -like '*dedicated*'} | Select-Object -ExpandProperty id"') do set SCOPE_ID=%%i if not "%SCOPE_ID%"=="" ( REM Créer le mapper pour les rôles curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/client-scopes/%SCOPE_ID%/protocol-mappers/models" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"name\":\"realm-roles\",\"protocol\":\"openid-connect\",\"protocolMapper\":\"oidc-usermodel-realm-role-mapper\",\"config\":{\"multivalued\":\"true\",\"userinfo.token.claim\":\"true\",\"id.token.claim\":\"true\",\"access.token.claim\":\"true\",\"claim.name\":\"roles\",\"jsonType.label\":\"String\"}}" > nul 2>&1 echo ✅ Mapper de rôles configuré ) else ( echo ⚠️ Impossible de trouver le client scope dédié, le mapper devra être configuré manuellement ) echo. REM Étape 7: Créer un utilisateur test echo 📝 Étape 7/7: Création de l'utilisateur test... curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/users" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"username\":\"test@unionflow.dev\",\"email\":\"test@unionflow.dev\",\"firstName\":\"Test\",\"lastName\":\"User\",\"enabled\":true,\"emailVerified\":true}" > nul 2>&1 if errorlevel 1 ( echo ⚠️ L'utilisateur existe peut-être déjà, continuation... ) else ( echo ✅ Utilisateur créé ) REM Récupérer l'ID de l'utilisateur curl -s -X GET "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/users?username=test@unionflow.dev" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" > user.json for /f "delims=" %%i in ('powershell -Command "(Get-Content user.json | ConvertFrom-Json)[0].id"') do set USER_ID=%%i if not "%USER_ID%"=="" ( REM Définir le mot de passe curl -s -X PUT "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/users/%USER_ID%/reset-password" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "{\"type\":\"password\",\"value\":\"test123\",\"temporary\":false}" > nul 2>&1 echo ✅ Mot de passe défini (test123) REM Récupérer les rôles curl -s -X GET "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/roles" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" > roles.json REM Assigner les rôles MEMBRE et ADMIN_ENTITE for /f "delims=" %%i in ('powershell -Command "(Get-Content roles.json | ConvertFrom-Json) | Where-Object {$_.name -eq 'MEMBRE'} | Select-Object -ExpandProperty id"') do set ROLE_MEMBRE_ID=%%i for /f "delims=" %%i in ('powershell -Command "(Get-Content roles.json | ConvertFrom-Json) | Where-Object {$_.name -eq 'ADMIN_ENTITE'} | Select-Object -ExpandProperty id"') do set ROLE_ADMIN_ID=%%i if not "%ROLE_MEMBRE_ID%"=="" ( curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/users/%USER_ID%/role-mappings/realm" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "[{\"id\":\"%ROLE_MEMBRE_ID%\",\"name\":\"MEMBRE\"}]" > nul 2>&1 echo ✅ Rôle MEMBRE assigné ) if not "%ROLE_ADMIN_ID%"=="" ( curl -s -X POST "%KEYCLOAK_URL%/admin/realms/%REALM_NAME%/users/%USER_ID%/role-mappings/realm" ^ -H "Authorization: Bearer %ADMIN_TOKEN%" ^ -H "Content-Type: application/json" ^ -d "[{\"id\":\"%ROLE_ADMIN_ID%\",\"name\":\"ADMIN_ENTITE\"}]" > nul 2>&1 echo ✅ Rôle ADMIN_ENTITE assigné ) ) else ( echo ⚠️ Impossible de configurer l'utilisateur ) echo. REM Nettoyage des fichiers temporaires del token_response.json clients.json secret.json scopes.json user.json roles.json 2>nul REM Sauvegarder le client secret dans .env echo. echo 💾 Sauvegarde de la configuration... ( echo # Configuration Keycloak générée automatiquement echo # Date: %date% %time% echo. echo KEYCLOAK_CLIENT_SECRET=%CLIENT_SECRET% echo UNIONFLOW_BACKEND_URL=http://localhost:8085 echo. echo # Informations de connexion pour tests echo # Username: test@unionflow.dev echo # Password: test123 ) > .env echo ✅ Fichier .env créé avec le client secret echo. REM Résumé echo ======================================================== echo ✅ Configuration Keycloak terminée avec succès! echo ======================================================== echo. echo 📋 Résumé: echo - Realm: %REALM_NAME% echo - Client ID: %CLIENT_ID% echo - Client Secret: %CLIENT_SECRET% echo - Utilisateur test: test@unionflow.dev echo - Mot de passe: test123 echo - Rôles assignés: MEMBRE, ADMIN_ENTITE echo. echo 📄 Le client secret a été sauvegardé dans le fichier .env echo. echo 🚀 Prochaines étapes: echo 1. Vérifiez le fichier .env echo 2. Lancez l'application avec: start-local.bat echo 3. Accédez à http://localhost:8086 echo 4. Connectez-vous avec test@unionflow.dev / test123 echo. echo ======================================================== echo. pause