-
-
Notifications
You must be signed in to change notification settings - Fork 261
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?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
13de8f1
to
72b675f
Compare
@@ -64,7 +64,7 @@ def unauthorized_response(self) -> Response: | |||
) | |||
|
|||
@abstractmethod | |||
def authenticate(self, request: Request) -> Optional[Identity]: | |||
def authenticate(self, request: Request) -> Union[Optional[Identity], Coroutine[Any, Any, Optional[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.
Should we use a type alias instead? The return type is not super readable
robyn/router.py
Outdated
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I tested with both sync and async and both worked pretty well...
However, I'm kind of worried not showing support for both sync and async. I'll make some tests and will back with the results
0f96d7b
to
5aa3731
Compare
Hey @IA-PieroCV 👋 I will have a proper review over the weekend 😄 |
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 👋
I really like the implementation. Do we need to add some docs for this?
Also, would love some integration tests
Hey @sansyrox ! About integration test, of course! I'll implement it in the next couple of hours, it doesn't seem like a problem. |
5aa3731
to
a88e96c
Compare
Hey @sansyrox! I'm trying to look for an approach that solves this without the change on the registration approach, but this could take me some time. By now I will convert this PR to draft until I found a solution or having the endpoint registration on the start method (if it's done). |
I like the approach that we basically register routes, middleware, auth, routers after Then in I think it is going to make the whole prepare step much easier. We choose when to process authentication, subrouters, routes etc rather than the order the code got put together. |
07f28de
to
0b766c9
Compare
Description
This PR add the possibility to run async authenticate on AuthenticationHandler.
Summary
This PR changes adds the typing hints for Authentication handler in order to return an Identity that comes from a Sync process or a Coroutine (Async). Also, it modifies the add_auth_middleware function to run this authenticate using async.
FYI: This allows AuthenticationHandler to support async sessions on cache (on memory db like redis).
PR Checklist
Please ensure that:
You have added relevant documentationMinor feature, so current API Reference explains the usage.You have added relevant tests. We prefer integration tests wherever possibleMinor feature, so current auth testing worked for both scenarios (sync and async).Pre-Commit Instructions:
EDIT: Add some code context