Repository: paperless-ngx/paperless-ngx

Issue:
[BUG]  ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
### Description

After updating to Paperless 1.14.0, the paperless container would not start up again repeating the following error message:

```
webserver_1  | [2023-04-25 06:40:15,379] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
webserver_1  | [2023-04-25 06:30:11,396] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 121, in login
webserver_1  |     backend = backend or user.backend
webserver_1  | AttributeError: 'User' object has no attribute 'backend'
webserver_1  | 
webserver_1  | During handling of the above exception, another exception occurred:
webserver_1  | 
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
```

After a little debugging it seems that `PAPERLESS_AUTO_LOGIN_USERNAME` is responsible for that behavior. At least removing that config option fixes the issue.


### Steps to reproduce

1. Have installation of paperless 1.13.0 running with `PAPERLESS_AUTO_LOGIN_USERNAME` 
2. Updating to paperless 1.14.0

### Webserver logs

```bash
webserver_1  | Paperless-ngx docker container starting...
webserver_1  | Mapping UID and GID for paperless:paperless to 1002:1002
webserver_1  | Creating directory /tmp/paperless
webserver_1  | Adjusting permissions of paperless files. This may take a while.
webserver_1  | Waiting for PostgreSQL to start...
webserver_1  | Waiting for Redis...
webserver_1  | Connected to Redis broker.
webserver_1  | Apply database migrations...
webserver_1  | Operations to perform:
webserver_1  |   Apply all migrations: admin, auth, authtoken, contenttypes, django_celery_results, documents, guardian, paperless_mail, sessions
webserver_1  | Running migrations:
webserver_1  |   No migrations to apply.
webserver_1  | Running Django checks
webserver_1  | System check identified no issues (0 silenced).
webserver_1  | Executing /usr/local/bin/paperless_cmd.sh
webserver_1  | 2023-04-25 04:28:46,526 INFO Set uid to user 0 succeeded
webserver_1  | 2023-04-25 04:28:46,530 INFO supervisord started with pid 1
webserver_1  | 2023-04-25 04:28:47,534 INFO spawned: 'gunicorn' with pid 81
webserver_1  | 2023-04-25 04:28:47,536 INFO spawned: 'celery' with pid 82
webserver_1  | 2023-04-25 04:28:47,539 INFO spawned: 'celery-beat' with pid 83
webserver_1  | 2023-04-25 04:28:47,541 INFO spawned: 'consumer' with pid 84
webserver_1  | 2023-04-25 04:28:47,543 INFO spawned: 'celery-flower' with pid 85
webserver_1  | Checking if we should start flower...
webserver_1  | Not starting flower
webserver_1  | 2023-04-25 04:28:47,554 INFO success: celery-flower entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
webserver_1  | 2023-04-25 04:28:47,554 INFO exited: celery-flower (exit status 0; expected)
webserver_1  | 2023-04-25 04:28:48,555 INFO success: gunicorn entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
webserver_1  | 2023-04-25 04:28:48,555 INFO success: celery entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
webserver_1  | 2023-04-25 04:28:48,555 INFO success: celery-beat entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
webserver_1  | 2023-04-25 04:28:48,555 INFO success: consumer entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
webserver_1  | [2023-04-25 06:28:49,441] [INFO] [paperless.management.consumer] Using inotify to watch directory for changes: /usr/src/paperless/consume
webserver_1  | [2023-04-25 06:28:49 +0200] [81] [INFO] Starting gunicorn 20.1.0
webserver_1  | [2023-04-25 06:28:49 +0200] [81] [INFO] Listening at: http://[::]:8000 (81)
webserver_1  | [2023-04-25 06:28:49 +0200] [81] [INFO] Using worker: paperless.workers.ConfigurableWorker
webserver_1  | [2023-04-25 06:28:49 +0200] [81] [INFO] Server is ready. Spawning workers
webserver_1  | [2023-04-25 06:28:49,831] [INFO] [celery.beat] beat: Starting...
webserver_1  |  
webserver_1  |  -------------- celery@38f419caad02 v5.2.7 (dawn-chorus)
webserver_1  | --- ***** ----- 
webserver_1  | -- ******* ---- Linux-5.10.0-18-amd64-x86_64-with-glibc2.31 2023-04-25 06:28:49
webserver_1  | - *** --- * --- 
webserver_1  | - ** ---------- [config]
webserver_1  | - ** ---------- .> app:         paperless:0x7f3ae0cb92b0
webserver_1  | - ** ---------- .> transport:   redis://broker:6379//
webserver_1  | - ** ---------- .> results:     
webserver_1  | - *** --- * --- .> concurrency: 1 (prefork)
webserver_1  | -- ******* ---- .> task events: ON
webserver_1  | --- ***** ----- 
webserver_1  |  -------------- [queues]
webserver_1  |                 .> celery           exchange=celery(direct) key=celery
webserver_1  |                 
webserver_1  | 
webserver_1  | [tasks]
webserver_1  |   . documents.tasks.bulk_update_documents
webserver_1  |   . documents.tasks.consume_file
webserver_1  |   . documents.tasks.index_optimize
webserver_1  |   . documents.tasks.sanity_check
webserver_1  |   . documents.tasks.train_classifier
webserver_1  |   . documents.tasks.update_document_archive_file
webserver_1  |   . paperless_mail.mail.apply_mail_action
webserver_1  |   . paperless_mail.mail.error_callback
webserver_1  |   . paperless_mail.tasks.process_mail_accounts
webserver_1  | 
webserver_1  | [2023-04-25 06:28:50,062] [INFO] [celery.worker.consumer.connection] Connected to redis://broker:6379//
webserver_1  | [2023-04-25 06:28:50,125] [INFO] [celery.apps.worker] celery@38f419caad02 ready.
webserver_1  | [2023-04-25 06:29:10,974] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 121, in login
webserver_1  |     backend = backend or user.backend
webserver_1  | AttributeError: 'User' object has no attribute 'backend'
webserver_1  | 
webserver_1  | During handling of the above exception, another exception occurred:
webserver_1  | 
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
webserver_1  | [2023-04-25 06:29:41,201] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 121, in login
webserver_1  |     backend = backend or user.backend
webserver_1  | AttributeError: 'User' object has no attribute 'backend'
webserver_1  | 
webserver_1  | During handling of the above exception, another exception occurred:
webserver_1  | 
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
webserver_1  | [2023-04-25 06:29:43,674] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 121, in login
webserver_1  |     backend = backend or user.backend
webserver_1  | AttributeError: 'User' object has no attribute 'backend'
webserver_1  | 
webserver_1  | During handling of the above exception, another exception occurred:
webserver_1  | 
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
webserver_1  | [2023-04-25 06:30:00,083] [INFO] [celery.beat] Scheduler: Sending due task Check all e-mail accounts (paperless_mail.tasks.process_mail_accounts)
webserver_1  | [2023-04-25 06:30:00,133] [INFO] [celery.worker.strategy] Task paperless_mail.tasks.process_mail_accounts[66396076-5bc3-4d5f-8b6b-6c9eafe819a5] received
webserver_1  | [2023-04-25 06:30:00,887] [INFO] [celery.app.trace] Task paperless_mail.tasks.process_mail_accounts[66396076-5bc3-4d5f-8b6b-6c9eafe819a5] succeeded in 0.7441760115325451s: 'No new documents were added.'
webserver_1  | [2023-04-25 06:30:03,482] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 121, in login
webserver_1  |     backend = backend or user.backend
webserver_1  | AttributeError: 'User' object has no attribute 'backend'
webserver_1  | 
webserver_1  | During handling of the above exception, another exception occurred:
webserver_1  | 
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
webserver_1  | [2023-04-25 06:30:03,631] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 121, in login
webserver_1  |     backend = backend or user.backend
webserver_1  | AttributeError: 'User' object has no attribute 'backend'
webserver_1  | 
webserver_1  | During handling of the above exception, another exception occurred:
webserver_1  | 
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
webserver_1  | [2023-04-25 06:30:11,396] [ERROR] [django.request] Internal Server Error: /
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 121, in login
webserver_1  |     backend = backend or user.backend
webserver_1  | AttributeError: 'User' object has no attribute 'backend'
webserver_1  | 
webserver_1  | During handling of the above exception, another exception occurred:
webserver_1  | 
webserver_1  | Traceback (most recent call last):
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
webserver_1  |     raise exc_info[1]
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
webserver_1  |     response = await get_response(request)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 148, in __acall__
webserver_1  |     response = await sync_to_async(
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 448, in __call__
webserver_1  |     ret = await asyncio.wait_for(future, timeout=None)
webserver_1  |   File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
webserver_1  |     return await fut
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
webserver_1  |     result = self.fn(*self.args, **self.kwargs)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 490, in thread_handler
webserver_1  |     return func(*args, **kwargs)
webserver_1  |   File "/usr/src/paperless/src/paperless/auth.py", line 13, in process_request
webserver_1  |     auth.login(request, request.user)
webserver_1  |   File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 127, in login
webserver_1  |     raise ValueError(
webserver_1  | ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
```


### Browser logs

_No response_

### Paperless-ngx version

1.14.0

### Host OS

Debian 11 (5.10.0-18-amd64) / Docker

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

PAPERLESS_AUTO_LOGIN_USERNAME is used to disable the login system, as upstream proxy is taking care of that

### Other

_No response_


Discussion:

Resolving patch:
diff --git a/src/paperless/auth.py b/src/paperless/auth.py
--- a/src/paperless/auth.py
+++ b/src/paperless/auth.py
@@ -10,7 +10,11 @@ class AutoLoginMiddleware(MiddlewareMixin):
     def process_request(self, request):
         try:
             request.user = User.objects.get(username=settings.AUTO_LOGIN_USERNAME)
-            auth.login(request, request.user)
+            auth.login(
+                request=request,
+                user=request.user,
+                backend="django.contrib.auth.backends.ModelBackend",
+            )
         except User.DoesNotExist:
             pass
 


Test patch: