Commit f98eac2e authored by David Seaward's avatar David Seaward

* add wordcount validator to passphrase

* force lowercase usernames
* FIXME: can still start username with a numeral and include non-alphanumerics
parent 8fd0235d
......@@ -8,8 +8,10 @@ import pyasn1.type.univ
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import UserManager as BaseUserManager
from django.core.exceptions import ValidationError
from django.db import connections, router
from django.utils.crypto import salted_hmac
from django.utils.translation import ugettext as _
from ldapdb.models.fields import CharField, ListField
from limitmonitor import models as limitmonitor_models
......@@ -17,6 +19,19 @@ from limitmonitor import models as limitmonitor_models
log = logging.getLogger(__name__)
class WordCountValidator(object):
def __init__(self, min_words=3):
self.min_words = min_words
def validate(self, password, user=None):
is_valid_count = password.count(' ') > (self.min_words - 1)
if not is_valid_count:
raise ValidationError(self.get_help_text())
def get_help_text(self):
return _('Passphrase must contain at least three words. Use spaces between words.')
class UserManager(BaseUserManager):
def create_user(self, username, email=None, password=None, **extra_fields):
"""Create regular users in LDAP, and with no Django password."""
......@@ -38,6 +53,11 @@ class User(AbstractUser):
user = super(User, self).__init__(*args, **kwargs)
return user
def normalize_username(cls, username):
username = super(User, cls).normalize_username(username)
return username.lower()
def validate_unique(self, exclude=None):
return super(User, self).validate_unique(exclude)
......@@ -56,6 +56,12 @@ AUTH_PASSWORD_VALIDATORS = [
'min_length': 15,
'NAME': 'ldapregister.models.WordCountValidator',
'min_words': 3,
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment