-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Add custom HTTP handler for authentication errors #211
base: master
Are you sure you want to change the base?
Conversation
This patch gives ability to configure the custom HTTP handlers, which render authentication and authorization errors. In some use-cases it is necessary to produce different Content-Type, body, HTTP headers and status codes than ones provided by default. It is possible to configure one global handler, which is same for all web routes in the Authenticator, and multiple on-demand handlers for different middleware instances and web routes.
Pull Request Test Coverage Report for Build 10662414915Details
💛 - Coveralls |
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.
Overall looks good, just two minor nitpicks.
I don't really get it. What are those cases that need a custom body in response, in addition to the status code? So far, I have seen one semi-convincing case where UI code expected some JSON body in all responses, including 401/403, because it had some trouble handling content-type. To me, it is not a good reason for the change, but in case you have some valid use cases in mind, please share. |
I am trying to create an application where server side page rendering is used (go This scenario is possible because there is an HttpOnly JWT cookie, Right now it is not possible to output a custom response to user It may be possible to wrap auth middleware and pass a fake In addition, i want to replace the Such proposed This is what i want to achieve by introducing this change. |
I don't get this part. There is nothing in the auth middleware forcing you to put it first in the chain of middlewares. You can have others, like logging, panic/recovery, and the custom 4xx status code handling before the auth middleware in your chain. Unless I'm missing something, I think you can just add your own middleware prior to the auth middleware in your chain and implement your own handling of status codes with a custom implementation of the ResponseWriter. I don't see why it would need to care about the underlying auth middleware, as all you do here is inspect the status code set by auth (or any other middleware in the chain it is wrapping). Generally speaking, to me, the auth library seems to be the wrong place to provide rendering-related customizations. |
This patch gives a possibility to configure custom HTTP handlers, which can change the default authentication error responses in the middlewares.
The default implementation uses
http.Error()
method, which produces content typetext/plain
and simple textual messages rendered directly into the response body.
In some applications this behavior may not be enough,
and there may be a need to produce different responses like JSON or HTML.
The
AuthErrorHTTPHandler
interface allows to intercept authentication errors and write HTTP responses according to specific application requirements.I also added some additional middleware methods, which allow to configure different
AuthErrorHTTPHandler
implementations for different HTTP routes. Some routes in the applications may require different response bodies or attributes.