Django Rest Framework Behind HTTP Authentication Base

If my web service (powered by Django Rest Framework, v2.3.8) is inside a location protected by basic Nginx HTTP authentication, for example:

location / { auth_basic "Restricted access"; auth_basic_user_file /path/to/htpasswd; uwsgi_pass django; include /etc/uwsgi/config/uwsgi_params; } 

Then, when the user authenticates and tries to access the API, the following response is obtained for all views:

 {"detail": "Invalid username/password"} 

Is the Django Rest Framework an HTTP authorization header (designed for Nginx), although the view does not require authentication? If so, how do I do this?

Any help would be greatly appreciated.

+4
source share
2 answers

By default, the Django Rest Framework has two authentication classes, see here .

 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication' )} 

You can disable authentication for the rest of the infrastructure if you do not need it.

 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': () } 

Or you can only remove BasicAuthentication , as it will work in your case.

 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication' )} 
+5
source

As noted in another post, you must add a comma next to the authentication class, or it can raise a TypeError.

 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', #comma added here ) 

Source: fooobar.com/questions/276541 / ...

0
source

All Articles