mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Refactor to extend RemoteUserMiddleware & add authentication for Django
This commit is contained in:
parent
f0a1aed029
commit
426ad30a52
@ -2,7 +2,7 @@ from django.conf import settings
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.utils.deprecation import MiddlewareMixin
|
from django.utils.deprecation import MiddlewareMixin
|
||||||
from rest_framework import authentication
|
from rest_framework import authentication
|
||||||
from rest_framework import exceptions
|
from django.contrib.auth.middleware import RemoteUserMiddleware
|
||||||
|
|
||||||
|
|
||||||
class AutoLoginMiddleware(MiddlewareMixin):
|
class AutoLoginMiddleware(MiddlewareMixin):
|
||||||
@ -29,19 +29,9 @@ class AngularApiAuthenticationOverride(authentication.BaseAuthentication):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class HttpRemoteUserAuthentication(authentication.BaseAuthentication):
|
class HttpRemoteUserMiddleware(RemoteUserMiddleware):
|
||||||
""" This class allows authentication via HTTP_REMOTE_USER which is set for
|
""" This class allows authentication via HTTP_REMOTE_USER which is set for
|
||||||
example by certain SSO applications.
|
example by certain SSO applications.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def authenticate(self, request):
|
header = 'HTTP_REMOTE_USER'
|
||||||
username = request.META.get('HTTP_REMOTE_USER')
|
|
||||||
if not username:
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
user = User.objects.get(username=username)
|
|
||||||
except User.DoesNotExist:
|
|
||||||
raise exceptions.AuthenticationFailed('No such user')
|
|
||||||
|
|
||||||
return (user, None)
|
|
||||||
|
@ -115,13 +115,6 @@ if DEBUG:
|
|||||||
'paperless.auth.AngularApiAuthenticationOverride'
|
'paperless.auth.AngularApiAuthenticationOverride'
|
||||||
)
|
)
|
||||||
|
|
||||||
ENABLE_HTTP_REMOTE_USER = __get_boolean("PAPERLESS_ENABLE_HTTP_REMOTE_USER")
|
|
||||||
|
|
||||||
if ENABLE_HTTP_REMOTE_USER:
|
|
||||||
REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].append(
|
|
||||||
'paperless.auth.HttpRemoteUserAuthentication'
|
|
||||||
)
|
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
'whitenoise.middleware.WhiteNoiseMiddleware',
|
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||||
@ -135,6 +128,20 @@ MIDDLEWARE = [
|
|||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
ENABLE_HTTP_REMOTE_USER = __get_boolean("PAPERLESS_ENABLE_HTTP_REMOTE_USER")
|
||||||
|
|
||||||
|
if ENABLE_HTTP_REMOTE_USER:
|
||||||
|
MIDDLEWARE.append(
|
||||||
|
'paperless.auth.HttpRemoteUserMiddleware'
|
||||||
|
)
|
||||||
|
AUTHENTICATION_BACKENDS = [
|
||||||
|
'django.contrib.auth.backends.RemoteUserBackend',
|
||||||
|
'django.contrib.auth.backends.ModelBackend'
|
||||||
|
]
|
||||||
|
REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].append(
|
||||||
|
'rest_framework.authentication.RemoteUserAuthentication'
|
||||||
|
)
|
||||||
|
|
||||||
ROOT_URLCONF = 'paperless.urls'
|
ROOT_URLCONF = 'paperless.urls'
|
||||||
|
|
||||||
FORCE_SCRIPT_NAME = os.getenv("PAPERLESS_FORCE_SCRIPT_NAME")
|
FORCE_SCRIPT_NAME = os.getenv("PAPERLESS_FORCE_SCRIPT_NAME")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user