Repository: paperless-ngx/paperless-ngx
Pull request: Fix: v2.13.0 RC1 - increase field max lengths to accommodate larger tokens
<!--
Note: All PRs with code changes should be targeted to the `dev` branch, pure documentation changes can target `main`
-->

## Proposed change

Closes #7915

## 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.
-->

- [x] 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.
- [x] 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).
- [x] 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] Beta 2.13.0 RC1: Microsoft Business OAuth Token too long for database
### Description

When connecting a Microsoft 365 business account with the recently added OAuth feature, the token can not be stored in the db due to length constraints of 2048 characters.

### Steps to reproduce

1. Set up OAuth Config variables for Office365
2. Click the OAuth button in mail settings (paperless)
3. Accept the OAuth Sign-In
4. Get error 500

### Webserver logs

```bash
paperless-db-1         | 2024-10-14 13:54:04.157 UTC [184] ERROR:  value too long for type character varying(2048)
paperless-db-1         | 2024-10-14 13:54:04.157 UTC [184] STATEMENT:  INSERT INTO "paperless_mail_mailaccount" ("owner_id", "name", "imap_server", "imap_port", "imap_security", "username", "password", "is_token", "character_set", "account_type", "refresh_token", "expiration") VALUES (NULL, 'Outlook OAuth 2024-10-14 13:54:03.697806+00:00', 'outlook.office365.com', 993, 2, '', '*deleted token for obvious reasons*', true, 'UTF-8', 3, '*deleted token for obvious reasons*', '2024-10-14 15:10:07.154097+00:00'::timestamptz) RETURNING "paperless_mail_mailaccount"."id"
paperless-webserver-1  | [2024-10-14 15:54:04,158] [ERROR] [django.request] Internal Server Error: /api/oauth/callback/
```


### Browser logs

_No response_

### Paperless-ngx version

2.13.0-beta-rc1

### Host OS

Synology DSM 7.2

### 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:
Sheesh Microsoft. How long is the token?

Also odd it’s different for business accounts
Yes, I think the problem is that a business account has a longer token...
The first token is 2338 characters, the second one 921 chars. (Edit: length (-1 each))

I can offer my help testing it once it is in dev (or any other branch).

Commit message: Update 0027_mailaccount_expiration_mailaccount_account_type_and_more.py

Patch to review:
diff --git a/src/paperless_mail/migrations/0027_mailaccount_expiration_mailaccount_account_type_and_more.py b/src/paperless_mail/migrations/0027_mailaccount_expiration_mailaccount_account_type_and_more.py
index a39455b0f65..f944f7b23e3 100644
--- a/src/paperless_mail/migrations/0027_mailaccount_expiration_mailaccount_account_type_and_more.py
+++ b/src/paperless_mail/migrations/0027_mailaccount_expiration_mailaccount_account_type_and_more.py
@@ -10,6 +10,11 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
+        migrations.AlterField(
+            model_name="mailaccount",
+            name="password",
+            field=models.CharField(max_length=3072, verbose_name="password"),
+        ),
         migrations.AddField(
             model_name="mailaccount",
             name="expiration",
diff --git a/src/paperless_mail/models.py b/src/paperless_mail/models.py
index 15f7bef3560..f5f8109115e 100644
--- a/src/paperless_mail/models.py
+++ b/src/paperless_mail/models.py
@@ -42,7 +42,7 @@ class MailAccountType(models.IntegerChoices):
 
     username = models.CharField(_("username"), max_length=256)
 
-    password = models.CharField(_("password"), max_length=2048)
+    password = models.CharField(_("password"), max_length=3072)
 
     is_token = models.BooleanField(_("Is token authentication"), default=False)
 


Code review comments:
path: src/paperless_mail/models.py
text: @user1:
Should we increase the refresh token too, just in case?

@author:
Good idea