-
-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: add async support for auth handler #1045
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
IA-PieroCV marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,10 +290,15 @@ def add_auth_middleware(self, endpoint: str): | |
|
||
def decorator(handler): | ||
@wraps(handler) | ||
def inner_handler(request: Request, *args): | ||
async def inner_handler(request: Request, *args): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and what about sync support? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested with both sync and async and both worked pretty well... |
||
if not self.authentication_handler: | ||
raise AuthenticationNotConfiguredError() | ||
identity = self.authentication_handler.authenticate(request) | ||
|
||
if inspect.iscoroutinefunction(self.authentication_handler.authenticate): | ||
identity = await self.authentication_handler.authenticate(request) | ||
else: | ||
identity = self.authentication_handler.authenticate(request) | ||
|
||
if identity is None: | ||
return self.authentication_handler.unauthorized_response | ||
request.identity = identity | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @IA-PieroCV 👋
Thanks for the PR but why do we need this type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @sansyrox !
This is for typing complying. I'm using pyright and this is making it. Guess with mypy, pyre and others will work as well.
As the child classes can override sync method to async methods, authenticate should be awared of returning both sync result (Identity | None) and async result (Coroutine[Any, Any, Identity | None]).
Edit: Adding more context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@IA-PieroCV 👋
Should we use a type alias instead? The return type is not super readable