|
| 1 | +"""Handler for REST API call to authorized endpoint.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from fastapi import APIRouter, Request |
| 8 | + |
| 9 | +from auth import get_auth_dependency |
| 10 | +from models.responses import AuthorizedResponse, UnauthorizedResponse, ForbiddenResponse |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | +router = APIRouter(tags=["authorized"]) |
| 14 | +auth_dependency = get_auth_dependency() |
| 15 | + |
| 16 | + |
| 17 | +authorized_responses: dict[int | str, dict[str, Any]] = { |
| 18 | + 200: { |
| 19 | + "description": "The user is logged-in and authorized to access OLS", |
| 20 | + "model": AuthorizedResponse, |
| 21 | + }, |
| 22 | + 400: { |
| 23 | + "description": "Missing or invalid credentials provided by client", |
| 24 | + "model": UnauthorizedResponse, |
| 25 | + }, |
| 26 | + 403: { |
| 27 | + "description": "User is not authorized", |
| 28 | + "model": ForbiddenResponse, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +@router.post("/authorized", responses=authorized_responses) |
| 34 | +def authorized_endpoint_handler(_request: Request) -> AuthorizedResponse: |
| 35 | + """Handle request to the /authorized endpoint.""" |
| 36 | + # Ignore the user token, we should not return it in the response |
| 37 | + user_id, user_name, _ = asyncio.run(auth_dependency(_request)) |
| 38 | + return AuthorizedResponse(user_id=user_id, username=user_name) |
0 commit comments