From fce844bfc3c7078167b2e34edf1b0b9696197807 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 26 May 2020 16:26:57 +0300 Subject: [PATCH] Uid: Change how validation is done. --- .../migrations/0007_auto_20200526_1336.py | 39 +++++++++++++++++++ django_etesync/models.py | 12 +++--- 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 django_etesync/migrations/0007_auto_20200526_1336.py diff --git a/django_etesync/migrations/0007_auto_20200526_1336.py b/django_etesync/migrations/0007_auto_20200526_1336.py new file mode 100644 index 0000000..37e31ac --- /dev/null +++ b/django_etesync/migrations/0007_auto_20200526_1336.py @@ -0,0 +1,39 @@ +# Generated by Django 3.0.3 on 2020-05-26 13:36 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etesync', '0006_auto_20200526_1040'), + ] + + operations = [ + migrations.AlterField( + model_name='collection', + name='uid', + field=models.CharField(db_index=True, max_length=43, validators=[django.core.validators.RegexValidator(message='Not a valid UID', regex='^[a-zA-Z0-9]*$')]), + ), + migrations.AlterField( + model_name='collectioninvitation', + name='uid', + field=models.CharField(db_index=True, max_length=43, validators=[django.core.validators.RegexValidator(message='Expected a base64url.', regex='^[a-zA-Z0-9\\-_]{42,43}$')]), + ), + migrations.AlterField( + model_name='collectionitem', + name='uid', + field=models.CharField(db_index=True, max_length=43, null=True, validators=[django.core.validators.RegexValidator(message='Not a valid UID', regex='^[a-zA-Z0-9]*$')]), + ), + migrations.AlterField( + model_name='collectionitemchunk', + name='uid', + field=models.CharField(db_index=True, max_length=43, validators=[django.core.validators.RegexValidator(message='Expected a base64url.', regex='^[a-zA-Z0-9\\-_]{42,43}$')]), + ), + migrations.AlterField( + model_name='collectionitemrevision', + name='uid', + field=models.CharField(db_index=True, max_length=43, unique=True, validators=[django.core.validators.RegexValidator(message='Expected a base64url.', regex='^[a-zA-Z0-9\\-_]{42,43}$')]), + ), + ] diff --git a/django_etesync/models.py b/django_etesync/models.py index 98f57f2..3a286ce 100644 --- a/django_etesync/models.py +++ b/django_etesync/models.py @@ -20,13 +20,13 @@ from django.core.validators import RegexValidator from django.utils.functional import cached_property -Base64Url256BitValidator = RegexValidator(regex=r'^[a-zA-Z0-9\-_]{43}$', message='Expected a 256bit base64url.') +Base64Url256BitlValidator = RegexValidator(regex=r'^[a-zA-Z0-9\-_]{42,43}$', message='Expected a base64url.') UidValidator = RegexValidator(regex=r'^[a-zA-Z0-9]*$', message='Not a valid UID') class Collection(models.Model): uid = models.CharField(db_index=True, blank=False, null=False, - max_length=44, validators=[UidValidator]) + max_length=43, validators=[UidValidator]) version = models.PositiveSmallIntegerField() owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) @@ -60,7 +60,7 @@ class Collection(models.Model): class CollectionItem(models.Model): uid = models.CharField(db_index=True, blank=False, null=True, - max_length=44, validators=[UidValidator]) + max_length=43, validators=[UidValidator]) collection = models.ForeignKey(Collection, related_name='items', on_delete=models.CASCADE) version = models.PositiveSmallIntegerField() encryptionKey = models.BinaryField(editable=True, blank=False, null=True) @@ -90,7 +90,7 @@ def chunk_directory_path(instance, filename): class CollectionItemChunk(models.Model): uid = models.CharField(db_index=True, blank=False, null=False, - max_length=44, validators=[Base64Url256BitValidator]) + max_length=43, validators=[Base64Url256BitlValidator]) item = models.ForeignKey(CollectionItem, related_name='chunks', on_delete=models.CASCADE) chunkFile = models.FileField(upload_to=chunk_directory_path, max_length=150, unique=True) @@ -100,7 +100,7 @@ class CollectionItemChunk(models.Model): class CollectionItemRevision(models.Model): uid = models.CharField(db_index=True, unique=True, blank=False, null=False, - max_length=44, validators=[Base64Url256BitValidator]) + max_length=43, validators=[Base64Url256BitlValidator]) item = models.ForeignKey(CollectionItem, related_name='revisions', on_delete=models.CASCADE) meta = models.BinaryField(editable=True, blank=False, null=False) current = models.BooleanField(db_index=True, default=True, null=True) @@ -146,7 +146,7 @@ class CollectionMember(models.Model): class CollectionInvitation(models.Model): uid = models.CharField(db_index=True, blank=False, null=False, - max_length=44, validators=[Base64Url256BitValidator]) + max_length=43, validators=[Base64Url256BitlValidator]) version = models.PositiveSmallIntegerField(default=1) fromMember = models.ForeignKey(CollectionMember, on_delete=models.CASCADE) # FIXME: make sure to delete all invitations for the same collection once one is accepted