-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(variant_study_service): correct comments, add middleware for curr…
…ent user identity, update execute_or_add_commands function, update tests
- Loading branch information
Showing
10 changed files
with
279 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import typing as t | ||
from contextvars import ContextVar | ||
from optparse import Option | ||
from typing import Optional | ||
|
||
from starlette.requests import Request | ||
|
||
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint | ||
from starlette.responses import Response | ||
from starlette.types import ASGIApp | ||
|
||
from antarest.core.jwt import JWTUser | ||
from antarest.core.serialization import from_json | ||
from antarest.fastapi_jwt_auth import AuthJWT | ||
|
||
_current_user: ContextVar[t.Optional[AuthJWT]] = ContextVar("_current_user", default=None) | ||
|
||
class CurrentUserMiddleware(BaseHTTPMiddleware): | ||
def __init__(self, app: t.Optional[ASGIApp]) -> None: | ||
super().__init__(app) | ||
|
||
async def dispatch( | ||
self, | ||
request: Request, | ||
call_next: RequestResponseEndpoint, | ||
) -> Response: | ||
global _current_user | ||
auth_jwt = AuthJWT(Request(request.scope)) | ||
_current_user.set(auth_jwt) | ||
|
||
response = await call_next(request) | ||
return response | ||
|
||
|
||
def get_current_user() -> Optional[JWTUser]: | ||
auth_jwt = _current_user.get() | ||
if auth_jwt: | ||
json_data = from_json(auth_jwt.get_jwt_subject()) | ||
current_user = JWTUser.model_validate(json_data) | ||
else: | ||
current_user = None | ||
return current_user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.