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
}
const (
AccountStateActive = "active"
AccountStateLocked = "locked"
AccountStateDisabled = "disabled"
)
var (
ErrInvalidCredentials = errors.New("identifiants invalides")
ErrAccountLocked = errors.New("compte verrouillé")
+6 -5
View File
@@ -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
}
+3 -3
View File
@@ -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 {
+8 -8
View File
@@ -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:
+9 -9
View File
@@ -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 {
+6
View File
@@ -19,6 +19,12 @@ const (
RoleMember = "member"
)
const (
StatusActive = "active"
StatusDisabled = "disabled"
StatusRevoked = "revoked"
)
type Principal struct {
ID ident.UUID
Kind string
+5 -3
View File
@@ -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
+2 -2
View File
@@ -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)
+14 -13
View File
@@ -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,
},
}
+6 -5
View File
@@ -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")
}