-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication_docs.py
More file actions
50 lines (40 loc) · 1.45 KB
/
Copy pathapplication_docs.py
File metadata and controls
50 lines (40 loc) · 1.45 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
from typing import Annotated
from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter
from fastapi.params import Depends
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.openapi.docs import get_swagger_ui_html
from apps.api.auth.exception import PermissionDeniedException
from apps.api.user.repository import UserRepository
from apps.containers import Application
from apps.core.auth.exception import InvalidCredentialException
from apps.core.auth.hash import verify_password
security = HTTPBasic()
document_router = APIRouter(
prefix="/docs",
)
@document_router.get(path="/swagger", include_in_schema=False)
@inject
async def swagger_auth(
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
user_repository: Annotated[
UserRepository, Depends(Provide[Application.user.repository])
],
):
typed_username = credentials.username
typed_userpassword = credentials.password
user = await user_repository.retrieve_superuser_or_staff(
username=typed_username,
)
if not user:
raise PermissionDeniedException()
if not verify_password(typed_userpassword, user.hashed_password):
raise InvalidCredentialException()
return get_swagger_ui_html(
openapi_url="/docs/openapi.json",
title="Swagger UI",
swagger_ui_parameters={
"displayRequestDuration": True,
"persistAuthorization": True,
},
)