-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Unable to set a custom permissions message for unauthenticated requests #3754
Comments
I don't have anything against provided it's already done with permissions. |
This is also something I would like to see, but perhaps with the addition of a custom response code. It turns out that we need to deny calls for various reasons at a base level (for example, user account status), but the user is authenticated. Right now everything looks like "not logged in" to the frontend. |
Hmm I wonder if the real fix for this is not to add the ability to set a custom message when unauthenticated, but instead to add a property to permissions classes that when set, marks it as one that is unrelated to authentication? (And so the type of response is no longer conditional on the return value of eg: class UserAgentBlacklist(permissions.BasePermission):
expects_authentication = False
message = 'Please set a custom user agent when using scripts with our API.'
... In this case, if Does that sound like something that would be accepted? Many thanks :-) [1] https://github.com/tomchristie/django-rest-framework/blob/3.3.1/rest_framework/views.py#L165-L167 |
Big 👍 My opinion is that if you don't set any authentication class in the view, you don't expect authentication to occur at all. If you set at least one authentication class, then it should happen before checking any permissions. If you hit the permission check, you shouldn't worry about authentication as it should already have been dealt with. @xordoquy, @tomchristie am I over simplifying the subject here? What is the reason we check for |
"but instead to add a property to permissions classes that when set, marks it as one that is unrelated to authentication" - I think that's just too subtle a bit of API at that point. I'd suggest raising a |
So to clarify, in the example |
Correct. |
Such solution could solve your problem, but not sure if it's the best practice. |
You can send more than a single customized message if you want to. Step 1: Create a permissions.py file and write this code.
Here, Step 2: Go to view.py file and add the class
Now if you go to the endpoint and after sending a POST request you'll get this response.
|
where is GenericAPIException coming from? |
#2539 added the ability to set a custom message for permissions classes, by adding a
message
property.I'm attempting to use this with a global permission, similar to the IP blacklisting one in the docs:
http://www.django-rest-framework.org/api-guide/permissions/?q=request.META#examples
...however the custom message I set isn't used.
I have:
This successfully blocks the request, however the response from the API is:
Rather than:
This is because the custom message is only used in the
PermissionDenied
case, and not theNotAuthenticated
case:https://github.com/tomchristie/django-rest-framework/blob/3.3.1/rest_framework/views.py#L165-L167
Would you be open to allowing the same message to be used for both cases? Or else having another property to set a separate custom message for the
PermissionDenied
case?I understand that for permissions classes that are actually checking whether a user has permissions, it may be useful to differentiate between "not logged in" and "user doesn't have permissions", however for generic blacklist permissions that use IP or user agent etc, they still need to be able to set a custom message.
Many thanks! :-)
(Using Python 2.7.11, Django 1.8.7, django-rest-framework 3.3.1)
The text was updated successfully, but these errors were encountered: