paperless-ngx/src/paperless/auth.py

42 lines
1.4 KiB
Python
Raw Normal View History

from django.conf import settings
2021-03-14 14:17:21 +01:00
from django.contrib import auth
from django.contrib.auth.models import User
from django.utils.deprecation import MiddlewareMixin
from rest_framework import authentication
from django.contrib.auth.middleware import RemoteUserMiddleware
class AutoLoginMiddleware(MiddlewareMixin):
def process_request(self, request):
try:
2022-02-27 15:26:41 +01:00
request.user = User.objects.get(username=settings.AUTO_LOGIN_USERNAME)
2021-03-14 14:17:21 +01:00
auth.login(request, request.user)
except User.DoesNotExist:
pass
class AngularApiAuthenticationOverride(authentication.BaseAuthentication):
2022-02-27 15:26:41 +01:00
"""This class is here to provide authentication to the angular dev server
during development. This is disabled in production.
"""
def authenticate(self, request):
2022-02-27 15:26:41 +01:00
if (
settings.DEBUG
and "Referer" in request.headers
and request.headers["Referer"].startswith("http://localhost:4200/")
): # NOQA: E501
user = User.objects.filter(is_staff=True).first()
print("Auto-Login with user {}".format(user))
return (user, None)
else:
return None
class HttpRemoteUserMiddleware(RemoteUserMiddleware):
2022-02-27 15:26:41 +01:00
"""This class allows authentication via HTTP_REMOTE_USER which is set for
example by certain SSO applications.
"""
2022-02-27 15:26:41 +01:00
header = settings.HTTP_REMOTE_USER_HEADER_NAME