- Implemented SvelteKit frontend with authentication callback handling. - Created demo routes and paraglide localization functionality. - Added health check and entrypoint scripts for backend services. - Established PostgreSQL and Keycloak initialization scripts with health checks. - Introduced models for database schema using SQLAlchemy. - Configured Vite and SvelteKit for development and testing environments. - Added health check script to verify service statuses and resource usage. - Created Docker entrypoint scripts for seamless service startup.
87 lines
2.1 KiB
Python
87 lines
2.1 KiB
Python
"""
|
|
Endpoints API para autenticación
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user
|
|
from .dto import (
|
|
LoginRequestDTO,
|
|
TokenResponseDTO,
|
|
RefreshTokenRequestDTO,
|
|
UserInfoResponseDTO,
|
|
LogoutRequestDTO
|
|
)
|
|
from .service import AuthService
|
|
|
|
router = APIRouter(prefix="/auth", tags=["Authentication"])
|
|
security = HTTPBearer()
|
|
|
|
|
|
@router.post("/login", response_model=TokenResponseDTO)
|
|
async def login(
|
|
login_data: LoginRequestDTO,
|
|
db: Session = Depends(get_core_db)
|
|
):
|
|
"""
|
|
Autentica usuario con Keycloak y retorna tokens JWT
|
|
|
|
El usuario debe proporcionar:
|
|
- username: Usuario o email
|
|
- password: Contraseña
|
|
- tenant_slug: Slug del tenant al que pertenece
|
|
"""
|
|
service = AuthService(db)
|
|
return service.login(login_data)
|
|
|
|
|
|
@router.post("/refresh", response_model=TokenResponseDTO)
|
|
async def refresh_token(
|
|
refresh_data: RefreshTokenRequestDTO,
|
|
db: Session = Depends(get_core_db)
|
|
):
|
|
"""
|
|
Refresca el access token usando el refresh token
|
|
"""
|
|
service = AuthService(db)
|
|
return service.refresh_token(refresh_data)
|
|
|
|
|
|
@router.get("/me", response_model=UserInfoResponseDTO)
|
|
async def get_current_user_info(
|
|
credentials: HTTPAuthorizationCredentials = Depends(security),
|
|
db: Session = Depends(get_core_db)
|
|
):
|
|
"""
|
|
Obtiene información del usuario actual desde el token
|
|
"""
|
|
service = AuthService(db)
|
|
return service.get_user_info(credentials.credentials)
|
|
|
|
|
|
@router.post("/logout")
|
|
async def logout(
|
|
logout_data: LogoutRequestDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Cierra sesión invalidando el refresh token
|
|
"""
|
|
service = AuthService(db)
|
|
return service.logout(logout_data)
|
|
|
|
|
|
@router.get("/health")
|
|
async def auth_health():
|
|
"""
|
|
Health check del módulo de autenticación
|
|
"""
|
|
return {
|
|
"status": "ok",
|
|
"module": "authentication",
|
|
"provider": "keycloak"
|
|
}
|