Files
eventspace/backend/app/schemas/token.py
Felipe Cardoso 43df9d73b0 Add foundational user authentication and registration system
Introduces schemas for user management, token handling, and password hashing. Implements routes for user registration, login, token refresh, and user info retrieval. Sets up authentication dependencies and integrates the API router with the application.
2025-02-28 16:18:03 +01:00

54 lines
2.1 KiB
Python

from typing import Optional
from datetime import datetime
from pydantic import BaseModel, Field
class TokenBase(BaseModel):
"""Base token schema with common attributes."""
token_type: str = Field(default="bearer", description="Type of authentication token")
expires_in: int = Field(description="Token expiration time in seconds")
class Token(TokenBase):
"""Schema for authentication response containing both access and refresh tokens."""
access_token: str = Field(description="JWT access token")
refresh_token: str = Field(description="JWT refresh token for obtaining new access tokens")
class TokenPayload(BaseModel):
"""Schema representing the decoded JWT token payload."""
sub: str = Field(description="Subject identifier (user ID)")
type: str = Field(description="Token type (access or refresh)")
exp: datetime = Field(description="Token expiration timestamp")
iat: datetime = Field(description="Token issued at timestamp")
jti: Optional[str] = Field(None, description="JWT ID - unique identifier for the token")
class RefreshToken(BaseModel):
"""Schema for refresh token requests."""
refresh_token: str = Field(
...,
description="JWT refresh token used to obtain new access tokens"
)
class TokenResponse(BaseModel):
"""Schema for detailed token information response."""
access_token: str = Field(description="JWT access token")
refresh_token: str = Field(description="JWT refresh token")
token_type: str = Field(default="bearer")
expires_in: int = Field(description="Token expiration time in seconds")
scope: Optional[str] = Field(None, description="Token scope")
user_id: str = Field(description="ID of the authenticated user")
class Config:
json_schema_extra = {
"example": {
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer",
"expires_in": 1800,
"scope": "read write",
"user_id": "123e4567-e89b-12d3-a456-426614174000"
}
}