- Created `/frontend/app/api/votes/check/route.ts` to handle GET requests for checking if a user has voted in a specific election. - Added error handling for unauthorized access and missing election ID. - Forwarded requests to the backend API and returned appropriate responses. - Updated `/frontend/app/api/votes/history/route.ts` to fetch user's voting history with error handling. - Ensured both endpoints utilize the authorization token for secure access.
219 lines
6.1 KiB
Markdown
219 lines
6.1 KiB
Markdown
# <20> Étapes de Déploiement - Fix ElGamal Encryption
|
||
|
||
## Résumé des Corrections
|
||
|
||
Ce fix résout l'erreur critique:
|
||
```
|
||
ElGamal encryption failed: Error: Invalid public key format. Expected "p:g:h" but got "pk_ongoing_1"
|
||
Uncaught TypeError: can't access property "length", e is undefined
|
||
```
|
||
|
||
### Fichiers Modifiés
|
||
|
||
1. **`backend/routes/votes.py`**
|
||
- ✅ Ligne 410: `ElGamal()` → `ElGamalEncryption()`
|
||
- ✅ Ligne 425-426: Utilisation correcte de `public_key_bytes`
|
||
|
||
2. **`backend/routes/admin.py`**
|
||
- ✅ Ligne 143-163: Validation et régénération des clés invalides
|
||
|
||
3. **`frontend/lib/crypto-client.ts`**
|
||
- ✅ Lignes 60-127: Gestion d'erreur améliorée pour éviter `undefined` errors
|
||
|
||
4. **`docker/init.sql`**
|
||
- ✅ **MIGRATION AUTOMATIQUE** - Régénère toutes les clés au démarrage
|
||
- ✅ S'exécute UNE SEULE FOIS grâce à la table `migrations`
|
||
|
||
## 🚀 Plan de Déploiement (ULTRA SIMPLE)
|
||
|
||
### Étape 1: Arrêter les Conteneurs
|
||
```bash
|
||
cd /home/paul/CIA/e-voting-system
|
||
docker compose down -v
|
||
```
|
||
|
||
### Étape 2: Restart avec nouveau code
|
||
```bash
|
||
docker compose -f docker-compose.multinode.yml up -d
|
||
|
||
# Attendre l'initialisation (40-60 secondes)
|
||
sleep 50
|
||
```
|
||
|
||
**C'est tout!** ✅
|
||
|
||
La migration SQL dans `docker/init.sql` s'exécute automatiquement et régénère toutes les clés publiques corrompues UNE SEULE FOIS.
|
||
|
||
### Étape 3: Vérifier que Backend est Prêt
|
||
```bash
|
||
# Test simple endpoint
|
||
for i in {1..30}; do
|
||
if curl -s http://localhost:8000/api/elections/debug/all > /dev/null 2>&1; then
|
||
echo "✅ Backend est prêt!"
|
||
break
|
||
fi
|
||
echo "Tentative $i: Backend en initialisation..."
|
||
sleep 2
|
||
done
|
||
```
|
||
|
||
### Étape 4: Tester les Clés Publiques
|
||
```bash
|
||
# Vérifier que les clés ont été régénérées
|
||
curl -s http://localhost:8000/api/votes/public-keys?election_id=1 | jq '.elgamal_pubkey'
|
||
|
||
# Décodage du base64 pour vérifier le format p:g:h
|
||
echo "MjM6NTox[...]==" | base64 -d
|
||
# Résultat attendu: 23:5:13 (p:g:h format)
|
||
```
|
||
|
||
### Étape 5: Vérifier l'État des Élections
|
||
```bash
|
||
curl -s http://localhost:8000/api/admin/elections/elgamal-status | jq '.'
|
||
# Doit montrer: ready_for_voting >= 1, incomplete = 0
|
||
```
|
||
|
||
### Étape 6: Test Frontend
|
||
```bash
|
||
# 1. Se connecter: http://localhost:3000/login
|
||
# 2. Sélectionner l'élection active
|
||
# 3. Voter pour un candidat
|
||
# 4. Vérifier que le vote s'encrypte et se soumet sans erreur
|
||
```
|
||
|
||
## ✅ Checklist de Vérification
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
|
||
echo "🔍 CHECKLIST DE VÉRIFICATION"
|
||
echo "=============================="
|
||
|
||
# 1. Backend actif?
|
||
echo -n "1. Backend actif? "
|
||
if curl -s http://localhost:8000/api/elections/debug/all > /dev/null; then
|
||
echo "✅"
|
||
else
|
||
echo "❌"
|
||
exit 1
|
||
fi
|
||
|
||
# 2. Élections existent?
|
||
echo -n "2. Élections existent? "
|
||
COUNT=$(curl -s http://localhost:8000/api/admin/elections/elgamal-status | jq '.total_elections')
|
||
if [ "$COUNT" -gt 0 ]; then
|
||
echo "✅ ($COUNT élections)"
|
||
else
|
||
echo "❌"
|
||
exit 1
|
||
fi
|
||
|
||
# 3. Élections prêtes au vote?
|
||
echo -n "3. Élections prêtes au vote? "
|
||
READY=$(curl -s http://localhost:8000/api/admin/elections/elgamal-status | jq '.ready_for_voting')
|
||
if [ "$READY" -gt 0 ]; then
|
||
echo "✅ ($READY prêtes)"
|
||
else
|
||
echo "❌"
|
||
exit 1
|
||
fi
|
||
|
||
# 4. Clés publiques valides?
|
||
echo -n "4. Clés publiques valides? "
|
||
PUBKEY=$(curl -s http://localhost:8000/api/votes/public-keys?election_id=1 | jq -r '.elgamal_pubkey')
|
||
DECODED=$(echo "$PUBKEY" | base64 -d 2>/dev/null)
|
||
if [[ "$DECODED" =~ ^[0-9]+:[0-9]+:[0-9]+$ ]]; then
|
||
echo "✅ ($DECODED)"
|
||
else
|
||
echo "❌ (got: $DECODED)"
|
||
exit 1
|
||
fi
|
||
|
||
# 5. Format correct (p:g:h)?
|
||
echo -n "5. Format p:g:h correct? "
|
||
p=$(echo "$DECODED" | cut -d: -f1)
|
||
g=$(echo "$DECODED" | cut -d: -f2)
|
||
h=$(echo "$DECODED" | cut -d: -f3)
|
||
if [ "$p" -eq 23 ] && [ "$g" -eq 5 ] && [ "$h" -gt 0 ]; then
|
||
echo "✅ (p=$p, g=$g, h=$h)"
|
||
else
|
||
echo "❌"
|
||
exit 1
|
||
fi
|
||
|
||
echo ""
|
||
echo "✅ TOUS LES TESTS PASSÉS!"
|
||
echo "Le système est prêt au vote."
|
||
```
|
||
|
||
## 🐛 Troubleshooting
|
||
|
||
### Les clés ne sont pas régénérées?
|
||
```bash
|
||
# Vérifier la base de données
|
||
docker compose exec mariadb mariadb -u evoting_user -pevoting_pass123 evoting_db -e \
|
||
"SELECT * FROM migrations WHERE name LIKE 'fix_elgamal%';"
|
||
|
||
# Si vide = migration n'a pas été exécutée
|
||
# Vérifier les logs MariaDB
|
||
docker compose logs mariadb | grep -i "migration\|error" | tail -20
|
||
```
|
||
|
||
### Keys still show "pk_ongoing"?
|
||
```bash
|
||
# Vérifier directement la base de données
|
||
docker compose exec mariadb mariadb -u evoting_user -pevoting_pass123 evoting_db -e \
|
||
"SELECT id, name, CAST(public_key AS CHAR) as key FROM elections;"
|
||
|
||
# Si toujours "pk_ongoing", c'est que la migration n'a pas tourné
|
||
# Solution: Redémarrer avec -v pour forcer l'initialisation complète
|
||
docker compose down -v
|
||
docker compose -f docker-compose.multinode.yml up -d
|
||
```
|
||
|
||
### Backend refuse les connexions
|
||
```bash
|
||
# Vérifier que les conteneurs sont up
|
||
docker compose ps
|
||
|
||
# Vérifier les logs
|
||
docker compose logs backend | tail -50
|
||
```
|
||
|
||
### Frontend stuck sur "Submitting"
|
||
```bash
|
||
# Ouvrir DevTools (F12) et voir les erreurs JavaScript
|
||
# Vérifier Network tab pour les requêtes API
|
||
# Si erreur 400/500, vérifier les logs backend
|
||
docker compose logs backend | grep -i "error\|exception" | tail -20
|
||
```
|
||
|
||
## 📊 Résumé des Modifications
|
||
|
||
| Composant | Fichier | Changement |
|
||
|-----------|---------|-----------|
|
||
| Backend API | `routes/votes.py` | Import `ElGamalEncryption` + utilisation correcte |
|
||
| Backend Admin | `routes/admin.py` | Validation des clés + nouvel endpoint de régénération |
|
||
| Frontend Crypto | `lib/crypto-client.ts` | Gestion d'erreur améliorée + validation stricte |
|
||
| Database | `elections.public_key` | Sera régénérée par l'endpoint admin |
|
||
|
||
## 🎯 Résultat Attendu
|
||
|
||
Après ces étapes:
|
||
- ✅ Toutes les clés publiques seront au format `p:g:h`
|
||
- ✅ Frontend recevra des clés valides en base64
|
||
- ✅ ElGamal encryption fonctionnera sans erreur
|
||
- ✅ Votes seront soumis avec succès
|
||
- ✅ Votes enregistrés dans la blockchain
|
||
|
||
## ⏱️ Temps Estimé
|
||
- Backend restart: 30-60 secondes
|
||
- Régénération des clés: < 1 seconde
|
||
- Vérification complète: 5-10 minutes
|
||
|
||
---
|
||
|
||
**Statut**: ✅ READY TO DEPLOY
|
||
**Date**: November 7, 2025
|
||
**Version**: 1.0
|