Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Limit validity time for registration tokens

Marek Brzóska6a2884238a54
diff --git a/registration/migrations/0002_auto__add_field_emailconfirmation_valid_until.py b/registration/migrations/0002_auto__add_field_emailconfirmation_valid_until.py
new file mode 100644
index 0000000000..9b1fcfa0ae
--- /dev/null
+++ b/registration/migrations/0002_auto__add_field_emailconfirmation_valid_until.py
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Adding field 'EmailConfirmation.valid_until'
+ db.add_column(u'registration_emailconfirmation', 'valid_until',
+ self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2013, 3, 21, 0, 0)),
+ keep_default=False)
+
+ def backwards(self, orm):
+ # Deleting field 'EmailConfirmation.valid_until'
+ db.delete_column(u'registration_emailconfirmation', 'valid_until')
+
+ models = {
+ u'registration.emailconfirmation': {
+ 'Meta': {'object_name': 'EmailConfirmation'},
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+ 'external_user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['registration.ExternalUserData']", 'null': 'True', 'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'token': ('django.db.models.fields.CharField', [], {'default': "'d637eaa015c5ec36ccc7f7f27fc7721e'", 'max_length': '32'}),
+ 'valid_until': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2013, 3, 21, 0, 0)'})
+ },
+ u'registration.externaluserdata': {
+ 'Meta': {'unique_together': "(('provider', 'username'),)", 'object_name': 'ExternalUserData'},
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'provider': ('django.db.models.fields.TextField', [], {'db_index': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'external_ids'", 'null': 'True', 'to': u"orm['userprofile.User']"}),
+ 'username': ('django.db.models.fields.TextField', [], {'db_index': 'True'})
+ },
+ u'userprofile.address': {
+ 'Meta': {'unique_together': "(('user', 'alias'),)", 'object_name': 'Address'},
+ 'alias': ('django.db.models.fields.CharField', [], {'default': "u'Home'", 'max_length': '30'}),
+ 'city': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
+ 'company_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'country': ('django.db.models.fields.CharField', [], {'max_length': '2'}),
+ 'country_area': ('django.db.models.fields.CharField', [], {'max_length': '128', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'phone': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'postal_code': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
+ 'street_address_1': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
+ 'street_address_2': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'addressbook'", 'to': u"orm['userprofile.User']"})
+ },
+ u'userprofile.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'default_billing_address': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'+'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': u"orm['userprofile.Address']"}),
+ 'default_shipping_address': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'+'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': u"orm['userprofile.Address']"}),
+ 'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '75'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'password': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '128'})
+ }
+ }
+
+ complete_apps = ['registration']
diff --git a/registration/models.py b/registration/models.py
index 23536e1a7d..399f27702a 100644
--- a/registration/models.py
+++ b/registration/models.py
@@ -1,9 +1,12 @@
import os
+from datetime import datetime, timedelta
from django.db import models
from django.contrib.auth import get_user_model
+from django.conf import settings
User = get_user_model()
+now = datetime.now
class ExternalUserData(models.Model):
@@ -21,6 +24,8 @@ class EmailConfirmation(models.Model):
external_user = models.ForeignKey(ExternalUserData, null=True, blank=True)
token = models.CharField(
max_length=32, default=lambda: os.urandom(16).encode('hex'))
+ valid_until = models.DateTimeField(
+ default=lambda: now() + timedelta(settings.ACCOUNT_ACTIVATION_DAYS))
def get_confirmed_user(self):
"""Confirm that user owns this email address and return User insatnce.
diff --git a/registration/views.py b/registration/views.py
index 777c48b467..4a7fe10bfa 100644
--- a/registration/views.py
+++ b/registration/views.py
@@ -1,3 +1,5 @@
+from datetime import datetime, timedelta
+
from django.contrib.auth.views import (
login as django_login_view, logout as django_logout,
)
@@ -22,6 +24,7 @@ from .utils import (
)
User = get_user_model()
+now = datetime.now
def login(request):
@@ -141,9 +144,9 @@ def select_email(request):
def confirm_email(request, pk, token):
try:
- # TODO: validity period
email_confirmation = EmailConfirmation.objects.get(
- pk=pk, token=token)
+ pk=pk, token=token, valid_until__gte=now())
+ # TODO: cronjob (celery task) to delete stale tokens
except EmailConfirmation.DoesNotExist:
return TemplateResponse(request, 'registration/invalid_token.html')