Files
m c2c7dddba6 feat(api): identité user-first — login/password, ownership user, fin du token device
- 000006 : users.username/username_normalized/password_hash/is_admin, index
  unique partiel sur username_normalized, DROP idx_users_email (unicité V1 =
  username, jamais email) ; 000007 destructif : resources.owner_id→user_id
  (FK→users, reset base dev), l'unicité racine devient par user
- pkg/passwd : argon2id (t=1,m=64MiB,p=4,k=32), comparaison constant-time,
  VerifyTimedEqual (délai égalisé dummy-hash) ; pkg/auth : subject=user_id +
  claim device_id, TTL 7 j sans refresh
- repository : Users (count/get/resolve-exact/create/update-password/
  mark-deleted), Devices.MarkUser (user_id INFORMATIF uniquement)
- handlers : POST /devices sans token, POST /auth/login (401 indistinguable
  user inconnu/mauvais mdp, device requis), PATCH /users/me/password
  (current_password, tokens non révoqués — limite V1), GET /users/resolve
  (exact lowercase, jamais email/is_admin) ; middleware RequireAuth
  (user authorisant, device porté) ; tout le scoping ressources passe user
- bootstrap admin : users vide + ADMIN_* absents → refus de démarrer ;
  .env.example ; docs/api-v1.md §2/§3/§7/§8
- tests : migrations 000006/000007 (up/down), pkg/auth Identity, handlers
  login/password/resolve, helpers refactorés registerAndLogin, repo tests
  scopés user — suite backend verte
2026-09-10 21:21:36 +02:00

63 lines
1.6 KiB
Go

package passwd
import (
"strings"
"testing"
)
func TestHashVerifyRoundTrip(t *testing.T) {
hash, err := Hash("mon-super-mot-de-passe")
if err != nil {
t.Fatalf("Hash: %v", err)
}
if !strings.HasPrefix(hash, "$argon2id$v=19$m=65536,t=1,p=4$") {
t.Errorf("format inattendu : %s", hash)
}
if err := Verify("mon-super-mot-de-passe", hash); err != nil {
t.Errorf("Verify (bon mot de passe) = %v", err)
}
if err := Verify("mauvais", hash); err != ErrMismatch {
t.Errorf("Verify (mauvais mot de passe) = %v, attendu ErrMismatch", err)
}
}
func TestVerifyTimedEqualUnknownUsername(t *testing.T) {
// username inconnu → stored vide → vérification contre dummyHash.
// Ne doit JAMAIS réussir, et doit retourner ErrMismatch proprement.
if err := VerifyTimedEqual("whatever", ""); err != ErrMismatch {
t.Errorf("VerifyTimedEqual(stored vide) = %v, attendu ErrMismatch", err)
}
}
func TestVerifyMalformed(t *testing.T) {
cases := []string{
"",
"pas-un-hash",
"$argon2id$v=19$m=65536,t=1,p=4$AAAA",
"$argon2id$v=18$m=65536,t=1,p=4$c2FsdA==$a2V5",
}
for _, stored := range cases {
if err := Verify("x", stored); err != ErrMalformed {
t.Errorf("Verify(%q) = %v, attendu ErrMalformed", stored, err)
}
}
}
func TestHashIsSaltRandomized(t *testing.T) {
a, _ := Hash("same")
b, _ := Hash("same")
if a == b {
t.Error("deux hashes du même mot de passe identiques (salt non randomisé ?)")
}
}
func TestHashNeverExposesPassword(t *testing.T) {
hash, err := Hash("secret-password")
if err != nil {
t.Fatalf("Hash: %v", err)
}
if strings.Contains(hash, "secret-password") {
t.Error("le hash contient le mot de passe en clair")
}
}