feat(reset): added a better validator to the form
This commit is contained in:
33
app/reset.py
33
app/reset.py
@@ -7,29 +7,48 @@ from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, PasswordField, BooleanField, \
|
||||
SubmitField
|
||||
from wtforms.validators import ValidationError, DataRequired, \
|
||||
Email, EqualTo, Length
|
||||
Email, EqualTo, Length, Regexp
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
import re
|
||||
|
||||
bp = Blueprint('reset', __name__, url_prefix='/reset')
|
||||
|
||||
class ResetPasswordForm(FlaskForm):
|
||||
username = StringField(label=('Username'),
|
||||
# Minimal password length
|
||||
minlength = 9
|
||||
|
||||
# Form
|
||||
username = StringField(label=('Login'),
|
||||
validators=[DataRequired(),
|
||||
Length(max=64)])
|
||||
currentpassword = PasswordField(label=('Current password'),
|
||||
validators=[DataRequired()])
|
||||
newpassword = PasswordField(label=('New password'),
|
||||
validators=[DataRequired(),
|
||||
Length(min=8, message='Password should be at least %(min)d characters long')],
|
||||
render_kw={"onkeyup": "validate_form()"})
|
||||
Length(min=minlength, message='Password should be at least %(min)d characters long'),
|
||||
Regexp("^(?=.*[a-z])", message="Password must have a lowercase character"),
|
||||
Regexp("^(?=.*[A-Z])", message="Password must have an uppercase character"),
|
||||
Regexp("^(?=.*\\d)", message="Password must contain a number"),
|
||||
#Regexp(
|
||||
# "(?=.*[@$!%*#?&])", message="Password must contain a special character"
|
||||
#),],
|
||||
],
|
||||
render_kw={"onkeyup": f"validate_form({minlength})"})
|
||||
confirm_password = PasswordField(
|
||||
label=('Confirm Password'),
|
||||
validators=[DataRequired(message='* Required'),
|
||||
EqualTo('newpassword', message='Both password fields must be equal!')],
|
||||
render_kw={"onkeyup": "validate_confirm()"})
|
||||
render_kw={"onkeyup": f"validate_confirm({minlength})"})
|
||||
|
||||
submit = SubmitField(label=('Change my password'), render_kw={"onclick": "validate_form()"})
|
||||
submit = SubmitField(label=('Change my password'), render_kw={"onclick": f"validate_form({minlength})"})
|
||||
|
||||
# Validators
|
||||
def validate_username(self, username):
|
||||
excluded_chars = " *?!'^+%&/()=}][{$#;\\\""
|
||||
for char in self.username.data:
|
||||
if char in excluded_chars:
|
||||
raise ValidationError(
|
||||
f"Character {char} is not allowed in a login.")
|
||||
|
||||
@bp.route('/', methods=('GET', 'POST'))
|
||||
def reset():
|
||||
|
||||
Reference in New Issue
Block a user