-
Notifications
You must be signed in to change notification settings - Fork 155
feat(dart_frog_auth): add custom unauthenticated responses to all aut… #1632
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
Open
mtwichel
wants to merge
4
commits into
VeryGoodOpenSource:main
Choose a base branch
from
mtwichel:feat/custom-unauthenticated-responses
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 am yet not sure if there could be a better API that the one suggested in the PR.
I think the main issue is that the existence of
unauthenticatedResponse
makes the programmer feel there should exist anauthenticatedResponse
counterpart. Then, having both exist makes me question if we could merge them into a single parameter and allow the user to dictate the logic.I've been thinking about how would it look like if instead we had an
responseOverride
parameter where the programmer gets a user and decide how to handle the authorization; but it didn't convince me. Mainly because if you would only like to override the unauthenticated behavior the programmer would be forced to define the current defaulthandler(context.provide(() => user));
for authenticated cases redundantly. In addition I feel that something similar could be achieved by chaining a middleware.I have this other idea, where I think we should make a clear distinction between
authentication
andauthorization
. As of right now, thebearerAuthentication
is doing both, authentication and authorization (despite its name being solelybearerAuthentication
). The authorization logic is quite basic, if the authentication is anull
user then deny access by returning anHttpStatus.unauthorized
response. What we could do is introduce anauthorizer
parameter (that defaults to the current behavioruser != null
and returns anHttpStatus.unauthorized
response). Yet, I'm not sure if we could provide a better API by leveraging the existing Dart Frog structure, for example by defining an Authorizer middleware.Regarding the suggest API in this PR , a change I'm sure about is that if we end up going forward with it is renaming the parameter to remove the "Response" suffix, since it it not of type
Response
butHandler
, here are some name alternatives:onUnauthenticated
unauthenticatedHandler
onAuthenticationFailure
I tend to prefer
onUnauthenticated
.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.
This makes a ton of sense to me and is possibly the root of why I felt this change was needed. But this could also be solved with #1652 (or something similar) by moving the authorization logic to the handler itself. For example
Overall I think this is a very transparent solution and is the most flexible. But currently this won't work because if you are unauthenticated, the middleware will just return a 401 response and your handler will never actually happen.
That would be a pretty big breaking change for the auth handlers, so maybe it could be opt-in with a parameter?
Or maybe it could be a separate middleware for that behavior?