feat(request): add service accounts and unified authorization
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
"""ServiceAccount model — separate Principal type.
|
||||
|
||||
ServiceAccount is NOT a User subtype.
|
||||
It is a separate Principal for machine-to-machine authentication.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, String, Boolean, Integer, DateTime, Text, Enum as SQLEnum
|
||||
from sqlalchemy.orm import relationship
|
||||
from request_app.models.base import Base, TimestampMixin
|
||||
import enum
|
||||
|
||||
|
||||
class ServiceAccountState(str, enum.Enum):
|
||||
"""Service Account lifecycle states."""
|
||||
CREATED = "CREATED"
|
||||
ACTIVE = "ACTIVE"
|
||||
DISABLED = "DISABLED"
|
||||
REVOKED = "REVOKED"
|
||||
|
||||
|
||||
class ServiceAccount(Base, TimestampMixin):
|
||||
"""Service Account — separate Principal for machine-to-machine auth.
|
||||
|
||||
This is NOT a User subtype. It is a separate entity type.
|
||||
Module-owned. Platform does not directly manage this.
|
||||
"""
|
||||
|
||||
__tablename__ = "service_accounts"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
# Stable identifier for API reference (not email)
|
||||
identifier = Column(String(100), unique=True, nullable=False, index=True)
|
||||
display_name = Column(String(200), nullable=False)
|
||||
description = Column(Text, nullable=True)
|
||||
|
||||
# Lifecycle state
|
||||
state = Column(
|
||||
SQLEnum(ServiceAccountState),
|
||||
nullable=False,
|
||||
default=ServiceAccountState.CREATED,
|
||||
)
|
||||
|
||||
# Module ownership
|
||||
module_id = Column(String(50), nullable=False, default="request")
|
||||
|
||||
# Audit metadata
|
||||
created_by = Column(String(200), nullable=True) # Who created this SA
|
||||
last_authenticated_at = Column(DateTime, nullable=True)
|
||||
|
||||
# Relationships
|
||||
credentials = relationship("ServiceAccountCredential", back_populates="service_account", cascade="all, delete-orphan")
|
||||
roles = relationship("ServiceAccountRole", back_populates="service_account", cascade="all, delete-orphan")
|
||||
audit_events = relationship("ServiceAccountAuditEvent", back_populates="service_account", cascade="all, delete-orphan")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<ServiceAccount(id={self.id}, identifier='{self.identifier}', state='{self.state}')>"
|
||||
|
||||
@property
|
||||
def is_active(self) -> bool:
|
||||
"""Check if account can authenticate."""
|
||||
return self.state == ServiceAccountState.ACTIVE
|
||||
|
||||
@property
|
||||
def is_revoked(self) -> bool:
|
||||
"""Check if account is permanently revoked."""
|
||||
return self.state == ServiceAccountState.REVOKED
|
||||
|
||||
def can_transition_to(self, new_state: ServiceAccountState) -> bool:
|
||||
"""Check if transition to new_state is allowed."""
|
||||
allowed = {
|
||||
ServiceAccountState.CREATED: {ServiceAccountState.ACTIVE, ServiceAccountState.REVOKED},
|
||||
ServiceAccountState.ACTIVE: {ServiceAccountState.DISABLED, ServiceAccountState.REVOKED},
|
||||
ServiceAccountState.DISABLED: {ServiceAccountState.ACTIVE, ServiceAccountState.REVOKED},
|
||||
ServiceAccountState.REVOKED: set(), # Terminal state
|
||||
}
|
||||
return new_state in allowed.get(self.state, set())
|
||||
Reference in New Issue
Block a user