Repository: paperless-ngx/paperless-ngx
Pull request: Fix: remove auth tokens from export
<!--
Note: All PRs with code changes should be targeted to the `dev` branch, pure documentation changes can target `main`
-->

## Proposed change

~~For some reason `serialize` doesn't work as expected for auth tokens. Options here are this PR or I can change it to just use "pk" as the crypt field key because that's what it serializes to (weird). Or we can just remove auth tokens from the export.~~

~~LMK~~

~~Currently `auth_tokens` looks like `{'model': 'authtoken.token', 'pk': '4310a8ef9e6bbce7146ef995b7dec33e0be7da02', 'fields': {'user': 3, 'created': '2023-03-10T06:22:38.138Z'}}`~~

Edit: lets just remove them

Closes #8098

## Type of change

<!--
What type of change does your PR introduce to Paperless-ngx?
NOTE: Please check only one box!
-->

- [x] Bug fix: non-breaking change which fixes an issue.
- [ ] New feature / Enhancement: non-breaking change which adds functionality. _Please read the important note above._
- [ ] Breaking change: fix or feature that would cause existing functionality to not work as expected.
- [ ] Documentation only.
- [ ] Other. Please explain:

## Checklist:

<!--
NOTE: PRs that do not address the following will not be merged, please do not skip any relevant items.
-->

- [ ] I have read & agree with the [contributing guidelines](https://github.com/paperless-ngx/paperless-ngx/blob/main/CONTRIBUTING.md).
- [ ] If applicable, I have included testing coverage for new code in this PR, for [backend](https://docs.paperless-ngx.com/development/#testing) and / or [front-end](https://docs.paperless-ngx.com/development/#testing-and-code-style) changes.
- [ ] If applicable, I have tested my code for new features & regressions on both mobile & desktop devices, using the latest version of major browsers.
- [ ] If applicable, I have checked that all tests pass, see [documentation](https://docs.paperless-ngx.com/development/#back-end-development).
- [ ] I have run all `pre-commit` hooks, see [documentation](https://docs.paperless-ngx.com/development/#code-formatting-with-pre-commit-hooks).
- [ ] I have made corresponding changes to the documentation as needed.
- [ ] I have checked my modifications for any breaking changes.


Problem statement:
[BUG] export does not work anymore after the update from 2.13.0 to 2.13.1
### Description

Paperless export does not work anymore after the update from 2.13.0 to 2.13.1

### Steps to reproduce

docker compose -f /home/paperless/docker-compose.yml exec -T webserver document_exporter ../export --delete --passphrase XYZ

### Webserver logs

```bash
Traceback (most recent call last):
  File "/usr/src/paperless/src/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/paperless/src/documents/management/commands/document_exporter.py", line 218, in handle
    self.dump()
  File "/usr/src/paperless/src/documents/management/commands/document_exporter.py", line 289, in dump
    self.encrypt_secret_fields(manifest_dict)
  File "/usr/src/paperless/src/documents/management/commands/document_exporter.py", line 568, in encrypt_secret_fields
    if manifest_record["fields"][field]:
       ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
KeyError: 'key'
```


### Browser logs

_No response_

### Paperless-ngx version

2.13.1

### Host OS

Debian 12 x86_64

### Installation method

Docker - official image

### System status

_No response_

### Browser

_No response_

### Configuration changes

_No response_

### Please confirm the following

- [X] I believe this issue is a bug that affects all users of Paperless-ngx, not something specific to my installation.
- [X] This issue is not about the OCR or archive creation of a specific file(s). Otherwise, please see above regarding OCR tools.
- [X] I have already searched for relevant existing issues and discussions before opening this report.
- [X] I have updated the title field above with a concise description.

Hints from issue discussion:
This one is my bad, I can fix unless you’re already working on it stumpy?

Commit message: Might as well test social token too

Patch to review:
diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.py
index 6a23a701a5f..2095be02424 100644
--- a/src/documents/management/commands/document_exporter.py
+++ b/src/documents/management/commands/document_exporter.py
@@ -271,7 +271,6 @@ def dump(self):
             "social_accounts": SocialAccount.objects.all(),
             "social_apps": SocialApp.objects.all(),
             "social_tokens": SocialToken.objects.all(),
-            "auth_tokens": Token.objects.all(),
         }
 
         if settings.AUDIT_LOG_ENABLED:
@@ -286,6 +285,20 @@ def dump(self):
                     serializers.serialize("json", manifest_key_to_object_query[key]),
                 )
 
+            # Add the auth tokens to the manifest, serialized manually
+            manifest_dict["auth_tokens"] = [
+                {
+                    "model": "authtoken.token",
+                    "pk": t.pk,
+                    "fields": {
+                        "key": t.key,
+                        "user": t.user_id,
+                        "created": t.created.isoformat(),
+                    },
+                }
+                for t in Token.objects.all()
+            ]
+
             self.encrypt_secret_fields(manifest_dict)
 
             # These are treated specially and included in the per-document manifest
diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.py
index ff514a7d67d..b95b901e064 100644
--- a/src/documents/tests/test_management_exporter.py
+++ b/src/documents/tests/test_management_exporter.py
@@ -8,6 +8,9 @@
 from unittest import mock
 from zipfile import ZipFile
 
+from allauth.socialaccount.models import SocialAccount
+from allauth.socialaccount.models import SocialApp
+from allauth.socialaccount.models import SocialToken
 from django.contrib.auth.models import Group
 from django.contrib.auth.models import Permission
 from django.contrib.contenttypes.models import ContentType
@@ -20,6 +23,7 @@
 from guardian.models import GroupObjectPermission
 from guardian.models import UserObjectPermission
 from guardian.shortcuts import assign_perm
+from rest_framework.authtoken.models import Token
 
 from documents.management.commands import document_exporter
 from documents.models import Correspondent
@@ -874,6 +878,25 @@ def test_export_passphrase(self):
             password="mypassword",
         )
 
+        Token.objects.create(user=User.objects.first())
+
+        app = SocialApp.objects.create(
+            provider="test",
+            name="test",
+            client_id="test",
+        )
+        account = SocialAccount.objects.create(
+            user=User.objects.first(),
+            provider="test",
+            uid="test",
+        )
+        SocialToken.objects.create(
+            app=app,
+            account=account,
+            token="test",
+            token_secret="test",
+        )
+
         call_command(
             "document_exporter",
             "--no-progress-bar",
@@ -912,6 +935,12 @@ def test_export_passphrase(self):
         self.assertIsNotNone(account)
         self.assertEqual(account.password, "mypassword")
 
+        token = Token.objects.first()
+        self.assertIsNotNone(token)
+
+        social_token = SocialToken.objects.first()
+        self.assertIsNotNone(social_token)
+
     def test_import_crypt_no_passphrase(self):
         """
         GIVEN:


Code review comments:
diff_hunk: @@ -286,6 +285,20 @@ def dump(self):
                     serializers.serialize("json", manifest_key_to_object_query[key]),
                 )
 
+            # Add the auth tokens to the manifest, serialized manually
+            manifest_dict["auth_tokens"] = [
+                {
+                    "model": "authtoken.token",
+                    "pk": t.pk,
path: src/documents/management/commands/document_exporter.py
text: @user1:
Does this then put the key out in plaintext?  I think it would

@author:
Oh yea, very good point. Maybe we should just drop them. I dont think its a big deal that users will have to re-generate them.