package handlers import ( "errors" "net/http" "strings" "sync" "time" "github.com/gin-gonic/gin" "github.com/kazier/backend/pkg/api" "github.com/kazier/backend/pkg/passwd" "github.com/kazier/backend/repository" "github.com/kazier/backend/service" ) type LoginRequest struct { Username string `json:"username"` Password string `json:"password"` DeviceID string `json:"device_id"` } type LoginUserDTO struct { ID string `json:"id"` Username string `json:"username"` IsAdmin bool `json:"is_admin"` } type LoginResponse struct { Token string `json:"token"` ExpiresAt int64 `json:"expires_at"` User LoginUserDTO `json:"user"` } // Anti-bruteforce du login (FEEDBACK #12) : limiteur basique en mémoire, // N tentatives / minute / IP sur POST /auth/login, placé AVANT la vérification // argon2 (coûteuse) pour protéger le CPU. Au-delà → 429 RATE_LIMITED. // TODO(suivi) : rate-limit distribué (ex. Redis) + métriques pour les déploiements // multi-réplicas, et budget global coordonné avec le rate-limit POST /devices // (LOT 6) contre le remplissage de table. var ( loginRateLimitPerMinute = 60 loginRateWindow = time.Minute loginAttemptsMu sync.Mutex loginAttempts = map[string][]time.Time{} ) // OnLoginRateLimited est un hook d'observabilité appelé quand le limiteur // rejette une tentative (nil par défaut = no-op). Les tests peuvent l'utiliser // pour asserter le déclenchement sans dépendre du timing. var OnLoginRateLimited func(ip string) // ResetLoginRateLimit vide le compteur (tests uniquement). func ResetLoginRateLimit() { loginAttemptsMu.Lock() defer loginAttemptsMu.Unlock() loginAttempts = map[string][]time.Time{} } // loginRateLimited enregistre une tentative et retourne true si le quota // IP est dépassé (fenêtre glissante). func loginRateLimited(ip string, now time.Time) bool { loginAttemptsMu.Lock() defer loginAttemptsMu.Unlock() cutoff := now.Add(-loginRateWindow) kept := make([]time.Time, 0, len(loginAttempts[ip])+1) for _, t := range loginAttempts[ip] { if t.After(cutoff) { kept = append(kept, t) } } if len(kept) >= loginRateLimitPerMinute { loginAttempts[ip] = kept return true } loginAttempts[ip] = append(kept, now) return false } // AuthLogin est la SEULE porte d'émission de token (V1). L'erreur est // volontairement indistinguable entre « username inconnu », « mauvais mot de // passe » et « device non enregistré » — même code 401, même message, délai // égalisé via passwd.VerifyTimedEqual (anti-oracle, FEEDBACK #12). func AuthLogin(c *gin.Context) { if Store == nil || Store.Repository == nil || Auth == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } var req LoginRequest if err := c.ShouldBindJSON(&req); err != nil { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body") return } if strings.TrimSpace(req.Username) == "" || req.Password == "" || req.DeviceID == "" { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "username, password and device_id are required") return } if !deviceIDPattern.MatchString(req.DeviceID) { api.Error(c, http.StatusBadRequest, "INVALID_DEVICE_ID", "device_id must be 32 lowercase hex chars") return } if loginRateLimited(c.ClientIP(), time.Now()) { if OnLoginRateLimited != nil { OnLoginRateLimited(c.ClientIP()) } api.Error(c, http.StatusTooManyRequests, "RATE_LIMITED", "too many login attempts, retry later") return } normalized := service.NormalizeUsername(req.Username) user, userErr := Store.Repository.Users.GetByUsernameNormalized(normalized) // Même durée de traitement que le compte existe ou non (username inconnu => // stored vide → vérification contre dummyHash aléatoire par boot). stored := "" if userErr == nil && user != nil { stored = user.PasswordHash } if err := passwd.VerifyTimedEqual(req.Password, stored); err != nil { if errors.Is(err, passwd.ErrMismatch) { api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials") return } api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not verify credentials") return } // A1 : ne jamais déréférencer user si inconnu — même si Verify (par // extraordinaire) réussissait, on reste en 401. if userErr != nil || user == nil { api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials") return } exists, err := Store.Repository.Devices.Exists(req.DeviceID) if err != nil { api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not check device") return } if !exists { // Volontairement indistinguable d'un mauvais password (anti-oracle). api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials") return } if err := Store.Repository.Devices.MarkUser(req.DeviceID, user.ID); err != nil { if errors.Is(err, repository.ErrNotFound) { // Course : device supprimé entre Exists et MarkUser → même 401 // indistinguable, jamais de token pour un device fantôme. api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials") return } api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not update device") return } token, expiresAt, err := Auth.IssueWithExpiry(user.ID, req.DeviceID) if err != nil { api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not issue token") return } api.OK(c, LoginResponse{ Token: token, ExpiresAt: expiresAt.UnixMilli(), User: LoginUserDTO{ ID: user.ID, Username: user.Username, IsAdmin: user.IsAdmin, }, }) } type changePasswordRequest struct { CurrentPassword string `json:"current_password"` NewPassword string `json:"new_password"` } // ChangePassword met à jour le hash du compte connecté. Le mot de passe // courant est requis. Limite connue V1 : les tokens déjà émis restent valides // jusqu'à expiration (7 j, pas de révocation) — cf. docs/api-v1.md §7. func ChangePassword(c *gin.Context) { if Store == nil || Store.Repository == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) var req changePasswordRequest if err := c.ShouldBindJSON(&req); err != nil { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body") return } if len(req.NewPassword) < 8 { api.Error(c, http.StatusBadRequest, "INVALID_PASSWORD", "new password must be at least 8 characters") return } user, err := Store.Repository.Users.GetByID(userID) if err != nil { api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "account not found") return } if err := passwd.Verify(req.CurrentPassword, user.PasswordHash); err != nil { api.Error(c, http.StatusForbidden, "INVALID_PASSWORD", "current password is incorrect") return } newHash, err := passwd.Hash(req.NewPassword) if err != nil { api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not hash new password") return } if err := Store.Repository.Users.UpdatePassword(userID, newHash); err != nil { api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not update password") return } api.OK(c, gin.H{"id": userID}) } // ResolveUser résout UN destinataire par username EXACT (normalisé lowercase). // Jamais de listing ni de préfixe (pas d'énumération de comptes). Ne renvoie // que {id, username} — jamais email/is_admin/created_at. func ResolveUser(c *gin.Context) { if Store == nil || Store.Repository == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } username := service.NormalizeUsername(c.Query("username")) if username == "" { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "username is required") return } user, err := Store.Repository.Users.ResolveExact(username) if errors.Is(err, repository.ErrNotFound) { api.Error(c, http.StatusNotFound, "NOT_FOUND", "user not found") return } if err != nil { api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not resolve user") return } api.OK(c, gin.H{"id": user.ID, "username": user.Username}) }