Files
2025-11-02 15:40:51 +00:00

124 lines
3.6 KiB
Python

"""Authentication Schemas
Pydantic schemas for authentication endpoints.
"""
from pydantic import BaseModel, EmailStr, Field, validator
from typing import Optional
from core.security import PasswordPolicy
class UserRegister(BaseModel):
"""User registration schema"""
email: EmailStr = Field(..., description="User email address")
username: str = Field(
...,
min_length=3,
max_length=50,
pattern="^[a-zA-Z0-9_-]+$",
description="Username (alphanumeric, underscore, hyphen)"
)
password: str = Field(..., min_length=8, description="Password")
full_name: Optional[str] = Field(None, max_length=100, description="Full name")
@validator('password')
def validate_password(cls, v):
"""Validate password strength"""
is_valid, message = PasswordPolicy.validate_password(v)
if not is_valid:
raise ValueError(message)
return v
class Config:
json_schema_extra = {
"example": {
"email": "user@example.com",
"username": "johndoe",
"password": "SecurePass123!",
"full_name": "John Doe"
}
}
class UserResponse(BaseModel):
"""User response schema"""
id: int # 修改为 int,因为数据库使用整数ID
email: str
username: str
full_name: Optional[str]
role: str
is_active: bool
class Config:
from_attributes = True
json_schema_extra = {
"example": {
"id": "123e4567-e89b-12d3-a456-426614174000",
"email": "user@example.com",
"username": "johndoe",
"full_name": "John Doe",
"role": "user",
"is_active": True
}
}
class Token(BaseModel):
"""Token response schema"""
access_token: str = Field(..., description="JWT access token")
refresh_token: str = Field(..., description="JWT refresh token")
token_type: str = Field(default="bearer", description="Token type")
class Config:
json_schema_extra = {
"example": {
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}
}
class TokenRefresh(BaseModel):
"""Token refresh request schema"""
refresh_token: str = Field(..., description="Refresh token")
class Config:
json_schema_extra = {
"example": {
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
class PasswordResetRequest(BaseModel):
"""Password reset request schema"""
email: EmailStr = Field(..., description="User email address")
class Config:
json_schema_extra = {
"example": {
"email": "user@example.com"
}
}
class PasswordReset(BaseModel):
"""Password reset schema"""
token: str = Field(..., description="Password reset token")
new_password: str = Field(..., min_length=8, description="New password")
@validator('new_password')
def validate_password(cls, v):
"""Validate password strength"""
is_valid, message = PasswordPolicy.validate_password(v)
if not is_valid:
raise ValueError(message)
return v
class Config:
json_schema_extra = {
"example": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"new_password": "NewSecurePass123!"
}
}