From adf4b99a9b98fccc7cca0a42b3d76a129c46fdfc Mon Sep 17 00:00:00 2001 From: Darks Date: Fri, 7 Jun 2019 14:00:26 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20r=C3=A8gles=20pour=20la=20validati?= =?UTF-8?q?on=20des=20mots=20de=20passe.=20#13=20Je=20vous=20conseille=20d?= =?UTF-8?q?'avoir=20un=20bon=20gestionnaire=20de=20mots=20de=20passe=20du?= =?UTF-8?q?=20coup=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/validators.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/app/utils/validators.py b/app/utils/validators.py index 5705f1c..04efbf6 100644 --- a/app/utils/validators.py +++ b/app/utils/validators.py @@ -50,9 +50,40 @@ def email(form, email): def password(form, password): - if len(password.data) != 0 and len(password.data) < 10: - raise ValidationError('Mot de passe est trop court (10 caractères minimum).') - # TODO: add more rules >:] + MIN_CHARS = 10 + # To avoid errors in forms where password is optionnal + if len(password.data) != 0: + errors = [] + if len(password.data) < MIN_CHARS: + errors.append(f'Le mot de passe doit faire au moins {MIN_CHARS} caractères.') + + checks = { + 'lower': False, + 'upper': False, + 'numeric': False, + 'other': False + } + for c in password.data: + if c in "abcdefghijklmnopqrstuvwxyz": + checks['lower'] = True + elif c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": + checks['upper'] = True + elif c in "0123456789": + checks['numeric'] = True + else: + checks['other'] = True + + if not checks['lower']: + errors.append('Le mot de passe doit contenir au moins une minuscule.') + if not checks['upper']: + errors.append('Le mot de passe doit contenir au moins une majuscule.') + if not checks['numeric']: + errors.append('Le mot de passe doit contenir au moins une chiffre.') + if not checks['other']: + errors.append('Le mot de passe doit contenir au moins un caractère spécial.') + + if errors != []: + raise ValidationError(' '.join(errors)) def avatar(form, avatar):