replace static status definition by const

This commit is contained in:
venture
2026-11-14 09:57:52 +01:00
parent 276a9fe320
commit f16363cb60
10 changed files with 65 additions and 48 deletions
+6
View File
@@ -24,6 +24,12 @@ type Account struct {
CreatedAt int64 CreatedAt int64
} }
const (
AccountStateActive = "active"
AccountStateLocked = "locked"
AccountStateDisabled = "disabled"
)
var ( var (
ErrInvalidCredentials = errors.New("identifiants invalides") ErrInvalidCredentials = errors.New("identifiants invalides")
ErrAccountLocked = errors.New("compte verrouillé") ErrAccountLocked = errors.New("compte verrouillé")
+6 -5
View File
@@ -12,6 +12,7 @@ import (
"mediarr/database" "mediarr/database"
"mediarr/ident" "mediarr/ident"
"mediarr/identity"
) )
const ( const (
@@ -57,7 +58,7 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams)
} }
return nil, "", err return nil, "", err
} }
if principal.Status != "active" { if principal.Status != identity.StatusActive {
return nil, "", ErrInvalidSession return nil, "", ErrInvalidSession
} }
@@ -67,10 +68,10 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams)
switch { switch {
case err == nil: case err == nil:
switch account.State { switch account.State {
case "active": case AccountStateActive:
case "disabled": case AccountStateDisabled:
return nil, "", ErrAccountDisabled return nil, "", ErrAccountDisabled
case "locked": case AccountStateLocked:
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now { if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
return nil, "", ErrAccountLocked return nil, "", ErrAccountLocked
} }
@@ -152,7 +153,7 @@ func (s *Service) ValidateSession(ctx context.Context, token string) (*Session,
} }
return nil, nil, err return nil, nil, err
} }
if user.Principal.Status != "active" || user.Account.State == "disabled" { if user.Principal.Status != identity.StatusActive || user.Account.State == AccountStateDisabled {
return nil, nil, ErrInvalidSession return nil, nil, ErrInvalidSession
} }
+3 -3
View File
@@ -149,8 +149,8 @@ func TestGuestSession(t *testing.T) {
if authenticated.Account.Login != "jean" { if authenticated.Account.Login != "jean" {
t.Fatalf("login = %q, attendu jean", authenticated.Account.Login) t.Fatalf("login = %q, attendu jean", authenticated.Account.Login)
} }
if authenticated.Account.State != "active" { if authenticated.Account.State != AccountStateActive {
t.Fatalf("state = %q, attendu active", authenticated.Account.State) t.Fatalf("state = %q, attendu %q", authenticated.Account.State, AccountStateActive)
} }
} }
@@ -161,7 +161,7 @@ func TestCreateSessionRejectsInactivePrincipal(t *testing.T) {
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember) created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
_, err := service.db.WriteQueries.UpdatePrincipalStatus(ctx, database.UpdatePrincipalStatusParams{ _, err := service.db.WriteQueries.UpdatePrincipalStatus(ctx, database.UpdatePrincipalStatusParams{
Status: "disabled", Status: identity.StatusDisabled,
ID: created.Principal.ID, ID: created.Principal.ID,
}) })
if err != nil { if err != nil {
+8 -8
View File
@@ -70,7 +70,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut
ID: userID, ID: userID,
Kind: identity.KindUser, Kind: identity.KindUser,
Name: display, Name: display,
Status: "active", Status: identity.StatusActive,
CreatedAt: now, CreatedAt: now,
}); err != nil { }); err != nil {
return err return err
@@ -89,7 +89,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut
Valid: true, Valid: true,
}, },
MustChangePassword: 0, MustChangePassword: 0,
State: "active", State: AccountStateActive,
FailedAttempts: 0, FailedAttempts: 0,
CreatedAt: now, CreatedAt: now,
}); err != nil { }); err != nil {
@@ -135,23 +135,23 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au
} }
return nil, err return nil, err
} }
if principal.Status != "active" { if principal.Status != identity.StatusActive {
return nil, ErrInvalidCredentials return nil, ErrInvalidCredentials
} }
now := time.Now().UnixMilli() now := time.Now().UnixMilli()
switch account.State { switch account.State {
case "disabled": case AccountStateDisabled:
return nil, ErrAccountDisabled return nil, ErrAccountDisabled
case "locked": case AccountStateLocked:
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now { if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
return nil, ErrAccountLocked return nil, ErrAccountLocked
} }
if _, err := s.db.WriteQueries.UnlockAccount(ctx, account.PrincipalID); err != nil { if _, err := s.db.WriteQueries.UnlockAccount(ctx, account.PrincipalID); err != nil {
return nil, err return nil, err
} }
account.State = "active" account.State = AccountStateActive
account.FailedAttempts = 0 account.FailedAttempts = 0
account.LockedUntil = sql.NullInt64{} account.LockedUntil = sql.NullInt64{}
} }
@@ -177,7 +177,7 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au
if err != nil { if err != nil {
return nil, err return nil, err
} }
if updated.State == "locked" { if updated.State == AccountStateLocked {
return nil, ErrAccountLocked return nil, ErrAccountLocked
} }
return nil, ErrInvalidCredentials return nil, ErrInvalidCredentials
@@ -241,7 +241,7 @@ func (s *Service) loadAuthenticatedUser(ctx context.Context, id ident.UUID) (*Au
Account: Account{ Account: Account{
PrincipalID: id, PrincipalID: id,
Login: principal.Name, Login: principal.Name,
State: "active", State: AccountStateActive,
}, },
}, nil }, nil
default: default:
+9 -9
View File
@@ -32,11 +32,11 @@ func TestCreateUser(t *testing.T) {
if created.Principal.Name != "Alice" { if created.Principal.Name != "Alice" {
t.Fatalf("name = %q, attendu Alice", created.Principal.Name) t.Fatalf("name = %q, attendu Alice", created.Principal.Name)
} }
if created.Principal.Status != "active" { if created.Principal.Status != identity.StatusActive {
t.Fatalf("status = %q, attendu active", created.Principal.Status) t.Fatalf("status = %q, attendu %q", created.Principal.Status, identity.StatusActive)
} }
if created.Account.State != "active" { if created.Account.State != AccountStateActive {
t.Fatalf("état du compte = %q, attendu active", created.Account.State) t.Fatalf("état du compte = %q, attendu %q", created.Account.State, AccountStateActive)
} }
if len(created.Principal.Groups) != 1 || created.Principal.Groups[0].Name != "members" { if len(created.Principal.Groups) != 1 || created.Principal.Groups[0].Name != "members" {
t.Fatalf("groupes = %+v, attendu members", created.Principal.Groups) t.Fatalf("groupes = %+v, attendu members", created.Principal.Groups)
@@ -198,8 +198,8 @@ func TestAuthenticateLocksAccountAfterTooManyFailures(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("GetAccount : %v", err) t.Fatalf("GetAccount : %v", err)
} }
if account.State != "locked" { if account.State != AccountStateLocked {
t.Fatalf("state = %q, attendu locked", account.State) t.Fatalf("state = %q, attendu %q", account.State, AccountStateLocked)
} }
if !account.LockedUntil.Valid || account.LockedUntil.Int64 <= time.Now().UnixMilli() { if !account.LockedUntil.Valid || account.LockedUntil.Int64 <= time.Now().UnixMilli() {
t.Fatalf("locked_until = %+v, attendu dans le futur", account.LockedUntil) t.Fatalf("locked_until = %+v, attendu dans le futur", account.LockedUntil)
@@ -232,8 +232,8 @@ func TestAuthenticateUnlocksExpiredAccount(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("GetAccount : %v", err) t.Fatalf("GetAccount : %v", err)
} }
if account.State != "active" { if account.State != AccountStateActive {
t.Fatalf("state = %q, attendu active", account.State) t.Fatalf("state = %q, attendu %q", account.State, AccountStateActive)
} }
if account.FailedAttempts != 0 { if account.FailedAttempts != 0 {
t.Fatalf("failed_attempts = %d, attendu 0", account.FailedAttempts) t.Fatalf("failed_attempts = %d, attendu 0", account.FailedAttempts)
@@ -247,7 +247,7 @@ func TestAuthenticateRejectsDisabledAccount(t *testing.T) {
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember) created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
_, err := service.db.WriteQueries.UpdateAccountState(ctx, database.UpdateAccountStateParams{ _, err := service.db.WriteQueries.UpdateAccountState(ctx, database.UpdateAccountStateParams{
State: "disabled", State: AccountStateDisabled,
PrincipalID: created.Principal.ID, PrincipalID: created.Principal.ID,
}) })
if err != nil { if err != nil {
+6
View File
@@ -19,6 +19,12 @@ const (
RoleMember = "member" RoleMember = "member"
) )
const (
StatusActive = "active"
StatusDisabled = "disabled"
StatusRevoked = "revoked"
)
type Principal struct { type Principal struct {
ID ident.UUID ID ident.UUID
Kind string Kind string
+5 -3
View File
@@ -31,9 +31,11 @@ func (s *Service) CreatePrincipal(ctx context.Context, params CreatePrincipalPar
status := params.Status status := params.Status
if status == "" { if status == "" {
status = "active" status = StatusActive
} }
if status != "active" && status != "disabled" && status != "revoked" { switch status {
case StatusActive, StatusDisabled, StatusRevoked:
default:
return nil, fmt.Errorf("%w: %q", ErrInvalidStatus, status) return nil, fmt.Errorf("%w: %q", ErrInvalidStatus, status)
} }
@@ -87,7 +89,7 @@ func (s *Service) CreateGuest(ctx context.Context, name string) (*Principal, err
ID: id, ID: id,
Kind: KindGuest, Kind: KindGuest,
Name: name, Name: name,
Status: "active", Status: StatusActive,
CreatedAt: now, CreatedAt: now,
}); err != nil { }); err != nil {
return err return err
+2 -2
View File
@@ -27,8 +27,8 @@ func TestCreatePrincipal(t *testing.T) {
if created.Name != "alice" { if created.Name != "alice" {
t.Fatalf("name = %q, attendu alice", created.Name) t.Fatalf("name = %q, attendu alice", created.Name)
} }
if created.Status != "active" { if created.Status != StatusActive {
t.Fatalf("status = %q, attendu active", created.Status) t.Fatalf("status = %q, attendu %q", created.Status, StatusActive)
} }
if len(created.Groups) != 0 { if len(created.Groups) != 0 {
t.Fatalf("groupes = %+v, attendu aucun", created.Groups) t.Fatalf("groupes = %+v, attendu aucun", created.Groups)
+14 -13
View File
@@ -9,6 +9,7 @@ import (
"mediarr/cluster" "mediarr/cluster"
"mediarr/database" "mediarr/database"
"mediarr/ident" "mediarr/ident"
"mediarr/identity"
"time" "time"
) )
@@ -42,20 +43,20 @@ func (n *Node) ensureClusterIdentity(ctx context.Context) error {
switch { switch {
case err == nil: case err == nil:
identity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false) clusterIdentity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false)
if loadErr != nil { if loadErr != nil {
return fmt.Errorf("cluster : principal présent en base mais clé indisponible : %w", loadErr) return fmt.Errorf("cluster : principal présent en base mais clé indisponible : %w", loadErr)
} }
if !bytes.Equal(principal.PublicKey, identity.PublicKey) { if !bytes.Equal(principal.PublicKey, clusterIdentity.PublicKey) {
return errors.New("cluster : la clé publique de cluster.key ne correspond pas au principal en base") return errors.New("cluster : la clé publique de cluster.key ne correspond pas au principal en base")
} }
n.cluster = identity n.cluster = clusterIdentity
return nil return nil
case errors.Is(err, sql.ErrNoRows): case errors.Is(err, sql.ErrNoRows):
identity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true) clusterIdentity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true)
if createErr != nil { if createErr != nil {
return createErr return createErr
} }
@@ -69,21 +70,21 @@ func (n *Node) ensureClusterIdentity(ctx context.Context) error {
ID: clusterID, ID: clusterID,
Kind: "cluster", Kind: "cluster",
Name: "cluster", Name: "cluster",
PublicKey: identity.PublicKeyBytes(), PublicKey: clusterIdentity.PublicKeyBytes(),
Status: "active", Status: identity.StatusActive,
CreatedAt: time.Now().UnixMilli(), CreatedAt: time.Now().UnixMilli(),
}) })
if createErr != nil { if createErr != nil {
existing, getErr := n.db.Queries.GetClusterPrincipal(ctx) existing, getErr := n.db.Queries.GetClusterPrincipal(ctx)
if getErr == nil && bytes.Equal(existing.PublicKey, identity.PublicKey) { if getErr == nil && bytes.Equal(existing.PublicKey, clusterIdentity.PublicKey) {
n.cluster = identity n.cluster = clusterIdentity
return nil return nil
} }
return fmt.Errorf("cluster : création du principal : %w", createErr) return fmt.Errorf("cluster : création du principal : %w", createErr)
} }
n.cluster = identity n.cluster = clusterIdentity
return nil return nil
default: default:
@@ -99,28 +100,28 @@ func requiredNodePrincipals(now int64) []database.CreatePrincipalParams {
ID: ident.MustParse("00000000-0000-7000-8000-000000000001"), ID: ident.MustParse("00000000-0000-7000-8000-000000000001"),
Kind: "system", Kind: "system",
Name: "system", Name: "system",
Status: "active", Status: identity.StatusActive,
CreatedAt: now, CreatedAt: now,
}, },
{ {
ID: ident.MustParse("00000000-0000-7000-8000-000000000002"), ID: ident.MustParse("00000000-0000-7000-8000-000000000002"),
Kind: "group", Kind: "group",
Name: "admins", Name: "admins",
Status: "active", Status: identity.StatusActive,
CreatedAt: now, CreatedAt: now,
}, },
{ {
ID: ident.MustParse("00000000-0000-7000-8000-000000000003"), ID: ident.MustParse("00000000-0000-7000-8000-000000000003"),
Kind: "group", Kind: "group",
Name: "members", Name: "members",
Status: "active", Status: identity.StatusActive,
CreatedAt: now, CreatedAt: now,
}, },
{ {
ID: ident.MustParse("00000000-0000-7000-8000-000000000004"), ID: ident.MustParse("00000000-0000-7000-8000-000000000004"),
Kind: "group", Kind: "group",
Name: "guests", Name: "guests",
Status: "active", Status: identity.StatusActive,
CreatedAt: now, CreatedAt: now,
}, },
} }
+6 -5
View File
@@ -8,6 +8,7 @@ import (
"testing" "testing"
"mediarr/cluster" "mediarr/cluster"
"mediarr/identity"
) )
func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) { func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
@@ -25,9 +26,9 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
t.Fatalf("Up : %v", err) t.Fatalf("Up : %v", err)
} }
identity := node.ClusterIdentity() clusterIdentity := node.ClusterIdentity()
if len(identity.PublicKey) != 32 { if len(clusterIdentity.PublicKey) != 32 {
t.Fatalf("clé publique de %d octets, attendu 32", len(identity.PublicKey)) t.Fatalf("clé publique de %d octets, attendu 32", len(clusterIdentity.PublicKey))
} }
principal, err := node.db.Queries.GetClusterPrincipal(context.Background()) principal, err := node.db.Queries.GetClusterPrincipal(context.Background())
@@ -40,10 +41,10 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
if principal.Name != "cluster" { if principal.Name != "cluster" {
t.Fatalf("name = %q, attendu cluster", principal.Name) t.Fatalf("name = %q, attendu cluster", principal.Name)
} }
if principal.Status != "active" { if principal.Status != identity.StatusActive {
t.Fatalf("status = %q, attendu active", principal.Status) t.Fatalf("status = %q, attendu active", principal.Status)
} }
if !bytes.Equal(principal.PublicKey, identity.PublicKey) { if !bytes.Equal(principal.PublicKey, clusterIdentity.PublicKey) {
t.Fatal("la clé publique en base ne correspond pas à la clé chargée") t.Fatal("la clé publique en base ne correspond pas à la clé chargée")
} }