is_authenticated returns false in DRF, true outside DRF

55 views
Skip to first unread message

Eric Gustavsson

unread,
Nov 28, 2019, 8:26:29 AM11/28/19
to Django REST framework
Hey,

I've setup mozilla-django-oidc, but I'm struggling to get `request.user.is_authenticated` to be true within DRF. I added a router endpoint to the browseable API:
return Response({"authenticated": request.user.is_authenticated()})

As well as a response outside DRF:
return HttpResponse(request.user.is_authenticated())

After authentication. DRF returns false, outside DRF returns true. mozilla-django-oidc even has a class extending DRF BasicAuthentication

Which I added to the DRF settings:
REST_FRAMEWORK = {
    # ...
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'mozilla_django_oidc.contrib.drf.OIDCAuthentication',
    ]
}

This is all using
Django 1.11.15, but I've tested with 2.2 as well.
djangorestframework==3.10.3

What am I missing to get a user seen as authenticated within DRF?

Thanks,
Eric

Carl Nobile

unread,
Nov 30, 2019, 1:33:44 PM11/30/19
to Django REST framework
The request.user.is_authenticated() is always true unless the requester is the Annonymous user.
Are you sure you logged in and that you are the requester before looking at that value?

Eric Gustavsson

unread,
Dec 2, 2019, 4:24:56 AM12/2/19
to django-res...@googlegroups.com
Hi Carl,

Turns out I was missing 'rest_framework.authentication.SessionAuthentication' from the DEFAULT_AUTHENTICATION_CLASSES.
After adding that it worked within DRF without issues. Such a simple mistake

Thanks for the help nonetheless :)

Eric Gustavsson, RHCSA

He/Him/His

Software Engineer

Red Hat

IM: Telegram: @SpyTec

E1FE 044A E0DE 127D CBCA E7C7 BD1B 8DF2 C5A1 5384



--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/C1ajFUVZG84/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/f49aa3b1-f9b8-4a04-bf45-9657944dca74%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages