Skip to content
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 auth subroutes capabilities #1056

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

IA-PieroCV
Copy link

@IA-PieroCV IA-PieroCV commented Nov 26, 2024

Description

This PR fixes #708

Summary

This PR does handle authentication for subroutes having different levels of precedence:

  1. Endpoint decorators have highest precedence.
  2. include_router has the second highest precedence.
  3. SubRoute instance has the lowest precedence.
  4. If None of the above is specified, the default is False.

This PR change auth logic:

  • It does add an auth_required parameter for Route objects.
  • The authentication middleware is evaluated first and added before starting the server.
    • This ensures that precedence is made correctly.

Several integration tests were provided. MDX documentation was also updated. To be discussed on #1026

PR Checklist

Please ensure that:

  • The PR contains a descriptive title
  • The PR contains a descriptive summary of the changes
  • You build and test your changes before submitting a PR.
  • You have added relevant documentation
  • You have added relevant tests. We prefer integration tests wherever possible

Pre-Commit Instructions:

Copy link

vercel bot commented Nov 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
robyn ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 26, 2024 8:00am

@sansyrox
Copy link
Member

Hey @IA-PieroCV 👋

Is the PR still a WIP? Or is it up for review?

@IA-PieroCV
Copy link
Author

Hey @sansyrox !
I set this PR as draft because I don't really know if the proposal of precedence is correct. I'll change it to "ready for review" in order you to review it. It's functional, but as I said, it changes the way how the authentication register the auth middlewares. Let me know any observation!

@IA-PieroCV IA-PieroCV marked this pull request as ready for review November 26, 2024 19:48
def default_secured(request : Request) -> str:
return "Authentication is set by include_router!"

app.include_router(sub_router, auth_required=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @IA-PieroCV 👋

Thank you for the PR 😄 Overall I like the theme of the PR but will have to a review of the code.

However, why do we need an auth_required flag in include_router? Is it not the same as the SubRouter Class?

Copy link
Author

@IA-PieroCV IA-PieroCV Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, actually it is!
This is just to give developers options for including auth_required due to their own criteria.
Of course we can remove any of the auth_required instance level parameters, from include_router or the SubRoute object instance.

However, include_router have higher precedence. My logic here is because developers face this method more often than the instantiation of the SubRoute. The same logic for endpoint decorators.

Copy link
Member

@sansyrox sansyrox Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IA-PieroCV , apologies for the late revert. But, I think this is a redundancy. If someone wants to allow auth, they can do it the subrouter class. I'd suggest that we remove it from here.

Let me know if you have anymore thoughts 😄


> **Important**: If any of these methods are used, the authentication for the endpoint is set to `False` unless explicitly overridden.

---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this. Right?

Comment on lines +1112 to +1118
app.include_router(sub_router)
app.include_router(di_subrouter)
app.include_router(auth_subrouter_endpoint)
app.include_router(auth_subrouter_include, auth_required=True)
app.include_router(auth_subrouter_instance)
app.include_router(auth_subrouter_include_false, auth_required=False)
app.include_router(auth_subrouter_include_true, auth_required=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IA-PieroCV , I believe auth_required should either be removed from include_router or the SubRouter() class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like all the test cases!!

Comment on lines +293 to +307
def inner_handler(request: Request, *args):
if not self.authentication_handler:
raise AuthenticationNotConfiguredError()
identity = self.authentication_handler.authenticate(request)
if identity is None:
return self.authentication_handler.unauthorized_response
request.identity = identity
return request # Proceed to the next middleware or handler

self.add_route(
MiddlewareType.BEFORE_REQUEST,
endpoint,
inner_handler,
injected_dependencies,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicee

Copy link
Member

@sansyrox sansyrox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @IA-PieroCV 👋

Thank you for the PR 😄

Overall the changes look great. I just have some suggestions/comments 😄

Merry shipmas! 🎄

@sansyrox sansyrox force-pushed the main branch 5 times, most recently from 07f28de to 0b766c9 Compare January 7, 2025 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sub-router Authentication Capabilities
2 participants