-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathauthorized.py
More file actions
53 lines (42 loc) · 1.87 KB
/
Copy pathauthorized.py
File metadata and controls
53 lines (42 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Handler for REST API call to authorized endpoint."""
from typing import Annotated, Any
from fastapi import APIRouter, Depends
from lightspeed_stack.authentication import get_auth_dependency
from lightspeed_stack.authentication.interface import AuthTuple
from lightspeed_stack.log import get_logger
from lightspeed_stack.models.api.responses.constants import (
UNAUTHORIZED_OPENAPI_EXAMPLES,
)
from lightspeed_stack.models.api.responses.error import (
ForbiddenResponse,
ServiceUnavailableResponse,
UnauthorizedResponse,
)
from lightspeed_stack.models.api.responses.successful import AuthorizedResponse
logger = get_logger(__name__)
router = APIRouter(tags=["authorized"])
authorized_responses: dict[int | str, dict[str, Any]] = {
200: AuthorizedResponse.openapi_response(),
401: UnauthorizedResponse.openapi_response(examples=UNAUTHORIZED_OPENAPI_EXAMPLES),
403: ForbiddenResponse.openapi_response(examples=["endpoint"]),
503: ServiceUnavailableResponse.openapi_response(examples=["kubernetes api"]),
}
@router.post("/authorized", responses=authorized_responses)
async def authorized_endpoint_handler(
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
) -> AuthorizedResponse:
"""
Handle request to the /authorized endpoint.
Process POST requests to the /authorized endpoint, returning
the authenticated user's ID and username.
The response intentionally omits any authentication token.
### Parameters:
- auth: Authentication tuple from the auth dependency (used by middleware).
### Returns:
- AuthorizedResponse: Contains the user ID and username of the authenticated user.
"""
# Ignore the user token, we should not return it in the response
user_id, user_name, skip_userid_check, _ = auth
return AuthorizedResponse(
user_id=user_id, username=user_name, skip_userid_check=skip_userid_check
)