I want to create an authentication for my Django 1.11 project. A user will be authenticated if the request contains the header : X_USERNAME.
I am working with generic views so I use LoginRequiredMixin to control access.
I made this Custom authentication class:
class CustomAuthentication:
def authenticate(self, request):
username = request.META.get('X_USERNAME')
logging.warning(username)
if not username:
return None
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User(username=username)
user.is_staff = False
user.is_superuser = False
if request.META.get('X_GROUPNAME') == 'administrator':
user.is_staff = True
user.is_superuser = True
user.save()
return user, None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
I added it in my settings :
AUTHENTICATION_BACKENDS = ['path.to.file.CustomAuthentication']
But I can't make it work. I am redirected to /accounts/login/?next= which doesn't exist.
Thanks in advance!
EDIT:
I also tried to create a subclass as described here : Django RemoteUser documentation since it looks like what I want to achieve:
class CustomAuthentication(RemoteUserMiddleware):
header = 'HTTP_X_USERNAME'
It gave me the same result.
authenticate()method to see what code path is being executed?authenticate()method should be called manually within a login page ? Which is not what I want since I don't want to use a login page.