add FC email verification token model (#73148)

This commit is contained in:
Paul Marillonnet 2023-01-17 17:04:52 +01:00
parent 8702e2a6da
commit 1c22e90637
2 changed files with 72 additions and 0 deletions

View File

@ -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',
),
),
],
),
]

View File

@ -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)