# 🔐 Guide d'Installation - Secure Proxy OIDC ## Configuration Rapide (DĂ©veloppement) ### 1. Installation des dĂ©pendances ```bash cd /Users/alexandre/projet/openidv2 npm install ``` ### 2. Configuration de base ```bash cp .env.example .env ``` Pour le dĂ©veloppement simple (sans Keycloak), modifier `.env` : ```env PORT=3000 NODE_ENV=development PROXY_URL=http://localhost:3000 # Ces valeurs peuvent rester par dĂ©faut pour le dev OIDC_ISSUER=http://localhost:8080/auth/realms/master OIDC_CLIENT_ID=openidv2-client OIDC_CLIENT_SECRET=dev-secret OIDC_CALLBACK_URL=http://localhost:3000/callback ADMIN_USERNAME=admin@example.com ADMIN_PASSWORD=password123 DB_PATH=./db/services.db SESSION_SECRET=dev-secret-very-secure LOG_LEVEL=info ``` ### 3. Initialiser la base de donnĂ©es ```bash npm run init-db ``` ### 4. DĂ©marrer le serveur **Mode dĂ©veloppement (avec auto-reload)** : ```bash npm run dev ``` **Mode production** : ```bash npm start ``` Le serveur dĂ©marre sur http://localhost:3000 ## Configuration ComplĂšte avec Keycloak ### Setup Keycloak local (Docker) ```bash docker run -p 8080:8080 \ -e KEYCLOAK_ADMIN=admin \ -e KEYCLOAK_ADMIN_PASSWORD=admin \ -e KC_HTTP_ENABLED=true \ quay.io/keycloak/keycloak:latest start-dev ``` AccĂ©der Ă  : http://localhost:8080 - Login : admin / admin ### CrĂ©er un Realm Keycloak 1. Aller Ă  la console d'administration 2. Hover sur "Master" → "Create realm" 3. Nommer le realm : `master` (dĂ©jĂ  créé par dĂ©faut) ### CrĂ©er un Client OIDC 1. Dans le menu gauche : **Clients** 2. **Create** 3. **Client ID** : `openidv2-client` 4. **Client Protocol** : `openid-connect` 5. **Root URL** : `http://localhost:3000` 6. **Save** #### Configurer le Client Aller dans l'onglet **Settings** du client créé : - **Valid Redirect URIs** : `http://localhost:3000/callback` - **Valid Post Logout Redirect URIs** : `http://localhost:3000/` - **Access Type** : `confidential` - **Standard Flow Enabled** : `ON` Aller dans l'onglet **Credentials** : - Noter le **Client Secret** ### CrĂ©er un utilisateur 1. Menu gauche : **Users** → **Add User** 2. **Username** : `testuser` 3. **Email** : `testuser@example.com` 4. **Email Verified** : `ON` 5. **Save** Onglet **Credentials** : - **Set Password** : dĂ©finir un mot de passe - **Temporary** : OFF ### Mettre Ă  jour .env ```env OIDC_ISSUER=http://localhost:8080/auth/realms/master OIDC_CLIENT_ID=openidv2-client OIDC_CLIENT_SECRET= OIDC_CALLBACK_URL=http://localhost:3000/callback ADMIN_USERNAME=testuser@example.com ``` ### RedĂ©marrer le serveur ```bash npm run dev ``` ## Exemples d'Utilisation ### Exemple 1 : Proxifier Grafana local **Setup Grafana** : ```bash docker run -p 3001:3000 grafana/grafana ``` **Ajouter via Panel Admin** : 1. Aller Ă  `/admin` 2. Login avec Keycloak 3. "+ New Service" 4. Remplir : - Name: `Grafana` - Path: `/grafana` - Target URL: `http://localhost:3001` - Require Auth: ✓ CochĂ©e **AccĂšs** : - Avant : http://localhost:3001 - AprĂšs : https://secure.k2r.ovh/grafana (ou http://localhost:3000/grafana en dev) ### Exemple 2 : Proxifier un service public (pas d'auth) ```bash # Service simple docker run -p 5000:80 nginx # Dans l'admin : # Name: Public Docs # Path: /docs # Target URL: http://localhost:5000 # Require Auth: ❌ DĂ©cochĂ©e # AccĂšs sans login : http://localhost:3000/docs ``` ### Exemple 3 : Proxifier une app React locale ```bash # L'app React en dev npm start # sur http://localhost:3000 # Dans l'admin : # Name: My React App # Path: /app # Target URL: http://localhost:3000 # Require Auth: ✓ # AccĂšs : http://localhost:3000/app ``` ## Test de l'API Script shell disponible : ```bash chmod +x test-api.sh ./test-api.sh ``` Ou manuellement avec curl : ```bash # Test de login curl http://localhost:3000/auth/login-page # Test des services (aprĂšs auth) curl http://localhost:3000/api/services \ -H "Authorization: Bearer YOUR_TOKEN" ``` ## AccĂšs au Panel Admin 1. **Sans Keycloak** (dev) : - URL : http://localhost:3000/admin - Email : admin@example.com 2. **Avec Keycloak** : - URL : http://localhost:3000/auth/login - Authentifier avec Keycloak - Doit avoir l'email configurĂ© en `ADMIN_USERNAME` ## DĂ©ploiement Production ### Configuration DNS CrĂ©er un record DNS : ``` secure.k2r.ovh A YOUR_IP ``` ### Certificat SSL Avec Let's Encrypt : ```bash sudo certbot certonly --standalone -d secure.k2r.ovh ``` ### Configuration Nginx Copier et adapter `nginx.example.conf` : ```bash sudo cp nginx.example.conf /etc/nginx/sites-available/secure-proxy sudo ln -s /etc/nginx/sites-available/secure-proxy /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx ``` ### Configurer .env production ```env NODE_ENV=production PORT=3000 PROXY_URL=https://secure.k2r.ovh OIDC_ISSUER=https://keycloak.example.com/auth/realms/master OIDC_CLIENT_ID=openidv2-client OIDC_CLIENT_SECRET=changeme_secure OIDC_CALLBACK_URL=https://secure.k2r.ovh/callback ADMIN_USERNAME=admin@example.com ADMIN_PASSWORD=changeme_secure DB_PATH=/var/lib/openidv2/services.db SESSION_SECRET=changeme_very_long_random_string LOG_LEVEL=info ``` ### Lancer avec systemd CrĂ©er `/etc/systemd/system/openidv2.service` : ```ini [Unit] Description=Secure Proxy OIDC After=network.target [Service] Type=simple User=www-data WorkingDirectory=/var/www/openidv2 Environment="NODE_ENV=production" ExecStart=/usr/bin/node src/server.js Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target ``` Lancer : ```bash sudo systemctl daemon-reload sudo systemctl start openidv2 sudo systemctl enable openidv2 ``` VĂ©rifier : ```bash sudo systemctl status openidv2 sudo journalctl -u openidv2 -f ``` ## DĂ©pannage ### Port dĂ©jĂ  utilisĂ© ```bash # Trouver le processus lsof -i :3000 # Changer le port dans .env PORT=3001 ``` ### Erreur OIDC ``` ✗ OIDC initialization failed ``` - VĂ©rifier que Keycloak est accessible - VĂ©rifier `OIDC_ISSUER` dans `.env` - Le serveur continue sans OIDC en dev ### Base de donnĂ©es verrouillĂ©e ```bash rm db/services.db npm run init-db ``` ### Logs dĂ©taillĂ©s ```env LOG_LEVEL=debug ``` ## Variables d'Environnement ComplĂštes ```env # Serveur PORT=3000 NODE_ENV=development|production PROXY_URL=https://secure.k2r.ovh # OIDC/Keycloak OIDC_ISSUER=https://keycloak.example.com/auth/realms/master OIDC_CLIENT_ID=openidv2-client OIDC_CLIENT_SECRET=your_secret OIDC_CALLBACK_URL=https://secure.k2r.ovh/callback # Admin ADMIN_USERNAME=admin@example.com ADMIN_PASSWORD=your_password # Base de donnĂ©es DB_PATH=./db/services.db # Sessions SESSION_SECRET=random_string_minimum_32_chars # SĂ©curitĂ© RATE_LIMIT_WINDOW_MS=900000 # 15 minutes RATE_LIMIT_MAX_REQUESTS=100 # Logging LOG_LEVEL=debug|info|warn|error ``` ## Commandes Utiles ```bash # Installation complĂšte npm install && npm run init-db # DĂ©marrage dev npm run dev # Production npm start # Seed de donnĂ©es d'exemple npm run seed-db # Test API ./test-api.sh # Voir les sessions ls -la sessions/ # Voir la DB sqlite3 db/services.db "SELECT * FROM services;" # Logs (systemd) journalctl -u openidv2 -f ``` --- **Questions ? Consultez README.md pour plus d'infos**