From f16363cb60b66ba73f7c26550cd6cc9e45946d2c Mon Sep 17 00:00:00 2001 From: venture Date: Sat, 14 Nov 2026 09:57:52 +0100 Subject: [PATCH] replace static status definition by const --- auth/auth.go | 6 ++++++ auth/session.go | 11 ++++++----- auth/session_test.go | 6 +++--- auth/user.go | 16 ++++++++-------- auth/user_test.go | 18 +++++++++--------- identity/identity.go | 6 ++++++ identity/principal.go | 8 +++++--- identity/principal_test.go | 4 ++-- node/node_up.go | 27 ++++++++++++++------------- node/node_up_test.go | 11 ++++++----- 10 files changed, 65 insertions(+), 48 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index f020479..b544b50 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -24,6 +24,12 @@ type Account struct { CreatedAt int64 } +const ( + AccountStateActive = "active" + AccountStateLocked = "locked" + AccountStateDisabled = "disabled" +) + var ( ErrInvalidCredentials = errors.New("identifiants invalides") ErrAccountLocked = errors.New("compte verrouillé") diff --git a/auth/session.go b/auth/session.go index 7893be8..528d6e5 100644 --- a/auth/session.go +++ b/auth/session.go @@ -12,6 +12,7 @@ import ( "mediarr/database" "mediarr/ident" + "mediarr/identity" ) const ( @@ -57,7 +58,7 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams) } return nil, "", err } - if principal.Status != "active" { + if principal.Status != identity.StatusActive { return nil, "", ErrInvalidSession } @@ -67,10 +68,10 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams) switch { case err == nil: switch account.State { - case "active": - case "disabled": + case AccountStateActive: + case AccountStateDisabled: return nil, "", ErrAccountDisabled - case "locked": + case AccountStateLocked: if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now { return nil, "", ErrAccountLocked } @@ -152,7 +153,7 @@ func (s *Service) ValidateSession(ctx context.Context, token string) (*Session, } 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 } diff --git a/auth/session_test.go b/auth/session_test.go index 2382cdd..03ad738 100644 --- a/auth/session_test.go +++ b/auth/session_test.go @@ -149,8 +149,8 @@ func TestGuestSession(t *testing.T) { if authenticated.Account.Login != "jean" { t.Fatalf("login = %q, attendu jean", authenticated.Account.Login) } - if authenticated.Account.State != "active" { - t.Fatalf("state = %q, attendu active", authenticated.Account.State) + if authenticated.Account.State != AccountStateActive { + 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) _, err := service.db.WriteQueries.UpdatePrincipalStatus(ctx, database.UpdatePrincipalStatusParams{ - Status: "disabled", + Status: identity.StatusDisabled, ID: created.Principal.ID, }) if err != nil { diff --git a/auth/user.go b/auth/user.go index e7a15dd..8710a0d 100644 --- a/auth/user.go +++ b/auth/user.go @@ -70,7 +70,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut ID: userID, Kind: identity.KindUser, Name: display, - Status: "active", + Status: identity.StatusActive, CreatedAt: now, }); err != nil { return err @@ -89,7 +89,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut Valid: true, }, MustChangePassword: 0, - State: "active", + State: AccountStateActive, FailedAttempts: 0, CreatedAt: now, }); err != nil { @@ -135,23 +135,23 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au } return nil, err } - if principal.Status != "active" { + if principal.Status != identity.StatusActive { return nil, ErrInvalidCredentials } now := time.Now().UnixMilli() switch account.State { - case "disabled": + case AccountStateDisabled: return nil, ErrAccountDisabled - case "locked": + case AccountStateLocked: if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now { return nil, ErrAccountLocked } if _, err := s.db.WriteQueries.UnlockAccount(ctx, account.PrincipalID); err != nil { return nil, err } - account.State = "active" + account.State = AccountStateActive account.FailedAttempts = 0 account.LockedUntil = sql.NullInt64{} } @@ -177,7 +177,7 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au if err != nil { return nil, err } - if updated.State == "locked" { + if updated.State == AccountStateLocked { return nil, ErrAccountLocked } return nil, ErrInvalidCredentials @@ -241,7 +241,7 @@ func (s *Service) loadAuthenticatedUser(ctx context.Context, id ident.UUID) (*Au Account: Account{ PrincipalID: id, Login: principal.Name, - State: "active", + State: AccountStateActive, }, }, nil default: diff --git a/auth/user_test.go b/auth/user_test.go index c9aeb00..b15fe0f 100644 --- a/auth/user_test.go +++ b/auth/user_test.go @@ -32,11 +32,11 @@ func TestCreateUser(t *testing.T) { if created.Principal.Name != "Alice" { t.Fatalf("name = %q, attendu Alice", created.Principal.Name) } - if created.Principal.Status != "active" { - t.Fatalf("status = %q, attendu active", created.Principal.Status) + if created.Principal.Status != identity.StatusActive { + t.Fatalf("status = %q, attendu %q", created.Principal.Status, identity.StatusActive) } - if created.Account.State != "active" { - t.Fatalf("état du compte = %q, attendu active", created.Account.State) + if created.Account.State != AccountStateActive { + t.Fatalf("état du compte = %q, attendu %q", created.Account.State, AccountStateActive) } if len(created.Principal.Groups) != 1 || created.Principal.Groups[0].Name != "members" { t.Fatalf("groupes = %+v, attendu members", created.Principal.Groups) @@ -198,8 +198,8 @@ func TestAuthenticateLocksAccountAfterTooManyFailures(t *testing.T) { if err != nil { t.Fatalf("GetAccount : %v", err) } - if account.State != "locked" { - t.Fatalf("state = %q, attendu locked", account.State) + if account.State != AccountStateLocked { + t.Fatalf("state = %q, attendu %q", account.State, AccountStateLocked) } if !account.LockedUntil.Valid || account.LockedUntil.Int64 <= time.Now().UnixMilli() { t.Fatalf("locked_until = %+v, attendu dans le futur", account.LockedUntil) @@ -232,8 +232,8 @@ func TestAuthenticateUnlocksExpiredAccount(t *testing.T) { if err != nil { t.Fatalf("GetAccount : %v", err) } - if account.State != "active" { - t.Fatalf("state = %q, attendu active", account.State) + if account.State != AccountStateActive { + t.Fatalf("state = %q, attendu %q", account.State, AccountStateActive) } if account.FailedAttempts != 0 { 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) _, err := service.db.WriteQueries.UpdateAccountState(ctx, database.UpdateAccountStateParams{ - State: "disabled", + State: AccountStateDisabled, PrincipalID: created.Principal.ID, }) if err != nil { diff --git a/identity/identity.go b/identity/identity.go index d7628ed..46231db 100644 --- a/identity/identity.go +++ b/identity/identity.go @@ -19,6 +19,12 @@ const ( RoleMember = "member" ) +const ( + StatusActive = "active" + StatusDisabled = "disabled" + StatusRevoked = "revoked" +) + type Principal struct { ID ident.UUID Kind string diff --git a/identity/principal.go b/identity/principal.go index 937632c..8b0f8c3 100644 --- a/identity/principal.go +++ b/identity/principal.go @@ -31,9 +31,11 @@ func (s *Service) CreatePrincipal(ctx context.Context, params CreatePrincipalPar status := params.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) } @@ -87,7 +89,7 @@ func (s *Service) CreateGuest(ctx context.Context, name string) (*Principal, err ID: id, Kind: KindGuest, Name: name, - Status: "active", + Status: StatusActive, CreatedAt: now, }); err != nil { return err diff --git a/identity/principal_test.go b/identity/principal_test.go index 5a67045..44cc483 100644 --- a/identity/principal_test.go +++ b/identity/principal_test.go @@ -27,8 +27,8 @@ func TestCreatePrincipal(t *testing.T) { if created.Name != "alice" { t.Fatalf("name = %q, attendu alice", created.Name) } - if created.Status != "active" { - t.Fatalf("status = %q, attendu active", created.Status) + if created.Status != StatusActive { + t.Fatalf("status = %q, attendu %q", created.Status, StatusActive) } if len(created.Groups) != 0 { t.Fatalf("groupes = %+v, attendu aucun", created.Groups) diff --git a/node/node_up.go b/node/node_up.go index 2260a3d..e3cce76 100644 --- a/node/node_up.go +++ b/node/node_up.go @@ -9,6 +9,7 @@ import ( "mediarr/cluster" "mediarr/database" "mediarr/ident" + "mediarr/identity" "time" ) @@ -42,20 +43,20 @@ func (n *Node) ensureClusterIdentity(ctx context.Context) error { switch { case err == nil: - identity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false) + clusterIdentity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false) if loadErr != nil { 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") } - n.cluster = identity + n.cluster = clusterIdentity return nil case errors.Is(err, sql.ErrNoRows): - identity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true) + clusterIdentity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true) if createErr != nil { return createErr } @@ -69,21 +70,21 @@ func (n *Node) ensureClusterIdentity(ctx context.Context) error { ID: clusterID, Kind: "cluster", Name: "cluster", - PublicKey: identity.PublicKeyBytes(), - Status: "active", + PublicKey: clusterIdentity.PublicKeyBytes(), + Status: identity.StatusActive, CreatedAt: time.Now().UnixMilli(), }) if createErr != nil { existing, getErr := n.db.Queries.GetClusterPrincipal(ctx) - if getErr == nil && bytes.Equal(existing.PublicKey, identity.PublicKey) { - n.cluster = identity + if getErr == nil && bytes.Equal(existing.PublicKey, clusterIdentity.PublicKey) { + n.cluster = clusterIdentity return nil } return fmt.Errorf("cluster : création du principal : %w", createErr) } - n.cluster = identity + n.cluster = clusterIdentity return nil default: @@ -99,28 +100,28 @@ func requiredNodePrincipals(now int64) []database.CreatePrincipalParams { ID: ident.MustParse("00000000-0000-7000-8000-000000000001"), Kind: "system", Name: "system", - Status: "active", + Status: identity.StatusActive, CreatedAt: now, }, { ID: ident.MustParse("00000000-0000-7000-8000-000000000002"), Kind: "group", Name: "admins", - Status: "active", + Status: identity.StatusActive, CreatedAt: now, }, { ID: ident.MustParse("00000000-0000-7000-8000-000000000003"), Kind: "group", Name: "members", - Status: "active", + Status: identity.StatusActive, CreatedAt: now, }, { ID: ident.MustParse("00000000-0000-7000-8000-000000000004"), Kind: "group", Name: "guests", - Status: "active", + Status: identity.StatusActive, CreatedAt: now, }, } diff --git a/node/node_up_test.go b/node/node_up_test.go index b98c3c0..760bbc6 100644 --- a/node/node_up_test.go +++ b/node/node_up_test.go @@ -8,6 +8,7 @@ import ( "testing" "mediarr/cluster" + "mediarr/identity" ) func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) { @@ -25,9 +26,9 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) { t.Fatalf("Up : %v", err) } - identity := node.ClusterIdentity() - if len(identity.PublicKey) != 32 { - t.Fatalf("clé publique de %d octets, attendu 32", len(identity.PublicKey)) + clusterIdentity := node.ClusterIdentity() + if len(clusterIdentity.PublicKey) != 32 { + t.Fatalf("clé publique de %d octets, attendu 32", len(clusterIdentity.PublicKey)) } principal, err := node.db.Queries.GetClusterPrincipal(context.Background()) @@ -40,10 +41,10 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) { if principal.Name != "cluster" { 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) } - 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") }