question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support authentication for django-rest-framework

See original GitHub issue

Hello! django-rest-framework uses its own authentication system, so calling set_user or get(url, user=...) has no effect for drf views that need authentication.

I am writing a custom drf auth backend that will work with the WEBTEST_USER header, and configure my settings manually to have it used. Would you be interested in that class + an option to make WebTestMixin patch it in automatically?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:21 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
sloriacommented, Oct 25, 2017

Yep, it worked!

# core/settings/test.py
from rest_framework.authentication import RemoteUserAuthentication

class WebTestAuthentication(RemoteUserAuthentication):
    header = 'WEBTEST_USER'

AUTHENTICATION_BACKENDS.insert(0, 'django.contrib.auth.backends.RemoteUserBackend')
REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].insert(0, 'core.settings.test.WebTestAuthentication')

1reaction
merwokcommented, Oct 24, 2017

DRF is adding support for authn based on REMOTE_USER, which will make my small class obsolete: https://github.com/encode/django-rest-framework/pull/5306/files

Do you think there is value in adding my code to the docs in the meantime, or should we just close this ticket?

*edit* I forgot that this module uses WEBTEST_USER, not REMOTE_USER, so something is still needed.

*2nd edit* That something will probably be:


class WebtestAuthentication(RemoteUserAuthentication):
    header = 'WEBTEST_USER'

+ settings for django auth backend and drf auth backend.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authentication - Django REST framework
Authentication is the mechanism of associating an incoming request with a set of identifying credentials, such as the user the request came from, ......
Read more >
Django Rest Framework authentication: the easy way
In this tutorial you'll learn how to implement Django Rest Framework authentication in your web application by leveraging the built-in ...
Read more >
How to Implement Token Authentication using Django REST ...
In this tutorial you are going to learn how to implement Token-based authentication using Django REST Framework (DRF).
Read more >
User Authentication with the Django Rest Framework and ...
In this tutorial, we'll build a user authentication system for a web application with Django, the Django Rest Framework (DRF), and Angular.
Read more >
Token Based Authentication for Django Rest Framework
Token Based Authentication for Django Rest Framework. Django is of the popular web development framework based on python having a large community and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found