From 548469e511ea522b3766ff53e918cd58aa489fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Tue, 29 Oct 2024 14:30:30 +0100 Subject: [PATCH] add min length validator on api key --- .../migrations/0040_alter_user_api_key.py | 25 +++++++++++++++++++ lemarche/users/models.py | 5 +++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 lemarche/users/migrations/0040_alter_user_api_key.py diff --git a/lemarche/users/migrations/0040_alter_user_api_key.py b/lemarche/users/migrations/0040_alter_user_api_key.py new file mode 100644 index 000000000..e851928f8 --- /dev/null +++ b/lemarche/users/migrations/0040_alter_user_api_key.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.15 on 2024-10-29 13:28 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("users", "0039_remove_user_user_email_ci_uniqueness_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="user", + name="api_key", + field=models.CharField( + blank=True, + max_length=128, + null=True, + unique=True, + validators=[django.core.validators.MinLengthValidator(64)], + verbose_name="Clé API", + ), + ), + ] diff --git a/lemarche/users/models.py b/lemarche/users/models.py index 6cbdb843b..fac63747b 100644 --- a/lemarche/users/models.py +++ b/lemarche/users/models.py @@ -2,6 +2,7 @@ from django.contrib.auth.base_user import BaseUserManager from django.contrib.auth.models import AbstractUser from django.contrib.contenttypes.fields import GenericRelation +from django.core.validators import MinLengthValidator from django.db import models from django.db.models import Count from django.db.models.functions import Greatest, Lower @@ -230,7 +231,9 @@ class User(AbstractUser): max_length=20, choices=user_constants.SOURCE_CHOICES, default=user_constants.SOURCE_SIGNUP_FORM ) - api_key = models.CharField(verbose_name="Clé API", max_length=128, unique=True, blank=True, null=True) + api_key = models.CharField( + verbose_name="Clé API", max_length=128, unique=True, blank=True, null=True, validators=[MinLengthValidator(64)] + ) api_key_last_updated = models.DateTimeField( verbose_name="Date de dernière mise à jour de la clé API", blank=True, null=True )