auth.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from pydantic import BaseModel
  2. class GroupBrief(BaseModel):
  3. """Brief group info for embedding in user responses."""
  4. id: int
  5. name: str
  6. class Config:
  7. from_attributes = True
  8. class LoginRequest(BaseModel):
  9. username: str
  10. password: str
  11. class LoginResponse(BaseModel):
  12. access_token: str
  13. token_type: str = "bearer"
  14. user: "UserResponse"
  15. class UserCreate(BaseModel):
  16. username: str
  17. password: str | None = None # Optional when advanced auth is enabled
  18. email: str | None = None
  19. role: str = "user"
  20. group_ids: list[int] | None = None
  21. class UserUpdate(BaseModel):
  22. username: str | None = None
  23. password: str | None = None
  24. email: str | None = None
  25. role: str | None = None
  26. is_active: bool | None = None
  27. group_ids: list[int] | None = None
  28. class UserResponse(BaseModel):
  29. id: int
  30. username: str
  31. email: str | None = None
  32. role: str # Deprecated, kept for backward compatibility
  33. is_active: bool
  34. is_admin: bool # Computed from role and group membership
  35. groups: list[GroupBrief] = []
  36. permissions: list[str] = [] # All permissions from groups
  37. created_at: str
  38. class Config:
  39. from_attributes = True
  40. class ChangePasswordRequest(BaseModel):
  41. current_password: str
  42. new_password: str
  43. class SetupRequest(BaseModel):
  44. auth_enabled: bool
  45. admin_username: str | None = None
  46. admin_password: str | None = None
  47. class SetupResponse(BaseModel):
  48. auth_enabled: bool
  49. admin_created: bool | None = None
  50. class ForgotPasswordRequest(BaseModel):
  51. email: str
  52. class ForgotPasswordResponse(BaseModel):
  53. message: str
  54. class ResetPasswordRequest(BaseModel):
  55. user_id: int
  56. class ResetPasswordResponse(BaseModel):
  57. message: str
  58. class SMTPSettings(BaseModel):
  59. smtp_host: str
  60. smtp_port: int
  61. smtp_username: str | None = None # Optional when auth is disabled
  62. smtp_password: str | None = None # Optional for read operations or when auth is disabled
  63. smtp_security: str = "starttls" # 'starttls', 'ssl', 'none'
  64. smtp_auth_enabled: bool = True
  65. smtp_from_email: str
  66. smtp_from_name: str = "BamBuddy"
  67. # Deprecated field for backward compatibility
  68. smtp_use_tls: bool | None = None
  69. class TestSMTPRequest(BaseModel):
  70. smtp_host: str
  71. smtp_port: int
  72. smtp_username: str | None = None # Optional when auth is disabled
  73. smtp_password: str | None = None # Optional when auth is disabled
  74. smtp_security: str = "starttls" # 'starttls', 'ssl', 'none'
  75. smtp_auth_enabled: bool = True
  76. smtp_from_email: str
  77. test_recipient: str
  78. # Deprecated field for backward compatibility
  79. smtp_use_tls: bool | None = None
  80. class TestSMTPResponse(BaseModel):
  81. success: bool
  82. message: str