From ebfb6772dfa3a4b804a6b242aa0bf646d866bae8 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Tue, 17 Jan 2023 17:04:52 +0100
Subject: [PATCH 2/5] add FC email verification token model (#73148)

---
 .../0009_fcemailverificationtoken.py          | 40 +++++++++++++++++++
 src/authentic2_auth_fc/models.py              | 32 +++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 src/authentic2_auth_fc/migrations/0009_fcemailverificationtoken.py

diff --git a/src/authentic2_auth_fc/migrations/0009_fcemailverificationtoken.py b/src/authentic2_auth_fc/migrations/0009_fcemailverificationtoken.py
new file mode 100644
index 000000000..845a7eff7
--- /dev/null
+++ b/src/authentic2_auth_fc/migrations/0009_fcemailverificationtoken.py
@@ -0,0 +1,40 @@
+# Generated by Django 2.2.26 on 2023-01-18 10:54
+
+import uuid
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('authentic2_auth_fc', '0008_fcauthenticator_link_by_email'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='FcEmailVerificationToken',
+            fields=[
+                (
+                    'id',
+                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
+                ),
+                ('value', models.UUIDField(default=uuid.uuid4, verbose_name='Token value')),
+                ('created', models.DateTimeField(auto_now_add=True, verbose_name='Creation date')),
+                ('expires', models.DateTimeField(verbose_name='Expires')),
+                ('sent', models.BooleanField(default=False, verbose_name='Token sent')),
+                (
+                    'user',
+                    models.ForeignKey(
+                        on_delete=django.db.models.deletion.CASCADE,
+                        related_name='fc_email_verification_tokens',
+                        to=settings.AUTH_USER_MODEL,
+                        verbose_name='user',
+                    ),
+                ),
+            ],
+        ),
+    ]
diff --git a/src/authentic2_auth_fc/models.py b/src/authentic2_auth_fc/models.py
index b1aa13386..f72a3782f 100644
--- a/src/authentic2_auth_fc/models.py
+++ b/src/authentic2_auth_fc/models.py
@@ -14,11 +14,14 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import datetime
 import json
+import uuid
 
 from django.conf import settings
 from django.contrib.postgres.fields import ArrayField
 from django.db import models
+from django.utils import timezone
 from django.utils.functional import cached_property
 from django.utils.translation import gettext_lazy as _
 
@@ -184,3 +187,32 @@ class FcAccount(models.Model):
             ('sub', 'order'),
             ('user', 'order'),
         ]
+
+
+class FcEmailVerificationToken(models.Model):
+    DURATION = 60 * 60 * 24 * 2  # 48 hours
+    value = models.UUIDField(
+        verbose_name=_('Token value'),
+        default=uuid.uuid4,
+    )
+    created = models.DateTimeField(verbose_name=_('Creation date'), auto_now_add=True)
+    expires = models.DateTimeField(verbose_name=_('Expires'))
+    sent = models.BooleanField(default=False, verbose_name=_('Token sent'))
+    user = models.ForeignKey(
+        to=settings.AUTH_USER_MODEL,
+        verbose_name=_('user'),
+        related_name='fc_email_verification_tokens',
+        on_delete=models.CASCADE,
+    )
+
+    @classmethod
+    def cleanup(cls, now=None):
+        now = now or timezone.now()
+        cls.objects.filter(expires__lte=now).delete()
+
+    @classmethod
+    def create(cls, user, expires=None, duration=None):
+        if not duration:
+            duration = cls.DURATION
+        expires = expires or (timezone.now() + datetime.timedelta(seconds=duration))
+        return cls.objects.create(user=user, expires=expires)
-- 
2.39.0

