python - Django REST API Custom Token Authentication does not recognize token in future requests -


i running following issues django application , django-rest-framework.

i have written customauthtoken view according to: django rest framework: obtain auth token using email instead username

account/views.py

class userview(apiview):          def get(self, request):         users = customer.objects.all()         serializer = customerserializer(users, many=true)         return response(serializer.data)   class obtainauthtoken(apiview):     throttle_classes = ()     permission_classes = ()     parser_classes = (         formparser,         multipartparser,         jsonparser,     )      renderer_classes = (jsonrenderer,)      def post(self, request):         # authenticate user         c_auth = customauthentication()         customer = c_auth.authenticate(request)         token, created = token.objects.get_or_create(user=customer)          content = {             'token': unicode(token.key),         }          return response(content) 

my main urls.py:

    rest_framework.urlpatterns import format_suffix_patterns  account import views user_view  urlpatterns = [     url(r'users/$', user_view.userview.as_view()),     url(r'^api-token-auth/', user_view.obtainauthtoken.as_view()),     url(r'^auth/', include('rest_framework.urls',                                namespace='rest_framework')), ]  urlpatterns = format_suffix_patterns(urlpatterns) 

my custom authentication.py:

    django.contrib.auth.hashers import check_password  rest_framework import authentication rest_framework import exceptions  usercp.models import customer   class customauthentication(authentication.baseauthentication):     def authenticate(self, request):         email = request.post.get('email')         password = request.post.get('password')         if not email:             return none         if not password:             return none          try:             user = customer.objects.get(email=email)             if check_password(password, user.password):                 if not user.is_active:                     msg = _('user account disabled.')                 customer = user             else:                 msg = _('unable log in provided credentials.')                 customer = none          except customer.doesnotexist:             msg = 'no such user'             raise exceptions.authenticationfailed(msg)          return customer 

and taken settings.py:

rest_framework = {     'default_permission_classes': [         'rest_framework.permissions.isauthenticated'     ],     'default_authentication_classes': (         'rest_framework.authentication.tokenauthentication',     ) } 

when send curl request:

curl -h "accept: application/json; indent=4" -h "authorization: token bd97803941a1ede303e4fda9713f7120a1af656c" http://127.0.0.1:8000/users 

i getting "access denied".

the login works fine, receiving said token back.

however, cannot access userview. not quite sure problem is. need change in settings tokenauthentication? don't think so. since user set correctly in database, though use custom user object inherited abstractuser. documentation (http://www.django-rest-framework.org/api-guide/authentication/#setting-the-authentication-scheme) think correctly use same request header, spacing correct , don't think there encoding issue.

after working out, token not forwarded in wsgi configuration re-read documentation bit more carefully.

http://www.django-rest-framework.org/api-guide/authentication/#apache-mod_wsgi-specific-configuration

clearly states wsgipassauthorization on needs configured wsgi.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -