fix: session F5 + token/password modifiables dans les paramètres

Session F5 :
- auth.store: restoreSession() essaie fetchMe() avec le token existant
  (< 15 min → fonctionne sans cookie), puis tryRefresh() en fallback
- router: appelle restoreSession() au premier chargement au lieu de tryRefresh()

Paramètres infrastructure :
- Champs ssh_password et proxmox_token en write-only (vide = pas de changement)
- SettingsHandler: accepte les clés chiffrées, chiffre avant stockage
- Permet de corriger le token Proxmox invalide sans réinstallation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
enzo 2026-03-21 00:17:12 +01:00
parent 1886071922
commit d55ecdcd97
7 changed files with 111 additions and 16 deletions

View file

@ -6,6 +6,7 @@ import (
"strconv"
"git.geronzi.fr/proxmoxPanel/core/backend/internal/audit"
"git.geronzi.fr/proxmoxPanel/core/backend/internal/crypto"
"git.geronzi.fr/proxmoxPanel/core/backend/internal/db"
"git.geronzi.fr/proxmoxPanel/core/backend/internal/logbuffer"
"github.com/go-chi/chi/v5"
@ -15,11 +16,12 @@ import (
type SettingsHandler struct {
db *db.DB
auditLogger *audit.Logger
encryptor *crypto.Encryptor
}
// NewSettingsHandler crée un SettingsHandler.
func NewSettingsHandler(database *db.DB, auditLog *audit.Logger) *SettingsHandler {
return &SettingsHandler{db: database, auditLogger: auditLog}
func NewSettingsHandler(database *db.DB, auditLog *audit.Logger, enc *crypto.Encryptor) *SettingsHandler {
return &SettingsHandler{db: database, auditLogger: auditLog, encryptor: enc}
}
// paramètres publics (non-sensibles) accessibles par les admins.
@ -32,6 +34,9 @@ var publicSettings = []string{
"ssh_username",
}
// paramètres sensibles : modifiables en écriture seule, stockés chiffrés.
var encryptedSettings = []string{"ssh_password", "proxmox_token"}
// GetAll retourne tous les paramètres publics de l'application.
// GET /api/settings
func (h *SettingsHandler) GetAll(w http.ResponseWriter, r *http.Request) {
@ -57,15 +62,22 @@ func (h *SettingsHandler) UpdateSetting(w http.ResponseWriter, r *http.Request)
return
}
// Vérifier que la clé est modifiable
allowed := false
// Vérifier que la clé est modifiable (publique ou chiffrée)
isPublic := false
for _, k := range publicSettings {
if k == key {
allowed = true
isPublic = true
break
}
}
if !allowed {
isEncrypted := false
for _, k := range encryptedSettings {
if k == key {
isEncrypted = true
break
}
}
if !isPublic && !isEncrypted {
JSONError(w, "Paramètre non modifiable via l'API", http.StatusForbidden)
return
}
@ -78,6 +90,27 @@ func (h *SettingsHandler) UpdateSetting(w http.ResponseWriter, r *http.Request)
return
}
// Paramètres chiffrés : valeur vide = ne pas modifier
if isEncrypted {
if body.Value == "" {
JSONResponse(w, http.StatusOK, map[string]string{"message": "Aucun changement"})
return
}
encrypted, err := h.encryptor.Encrypt(body.Value)
if err != nil {
JSONError(w, "Erreur chiffrement", http.StatusInternalServerError)
return
}
if err := h.db.SetSetting(key, encrypted, true); err != nil {
JSONError(w, "Erreur sauvegarde paramètre", http.StatusInternalServerError)
return
}
h.auditLogger.Log(&claims.UserID, claims.Username, "setting_update", key,
map[string]string{"key": key}, clientIP(r))
JSONResponse(w, http.StatusOK, map[string]string{"message": "Paramètre mis à jour"})
return
}
if err := h.db.SetSetting(key, body.Value, false); err != nil {
JSONError(w, "Erreur sauvegarde paramètre", http.StatusInternalServerError)
return