58 lines
2.3 KiB
Python
58 lines
2.3 KiB
Python
import functools
|
|
|
|
from flask import (
|
|
Blueprint, flash, g, redirect, render_template, request, session, url_for
|
|
)
|
|
from flask_wtf import FlaskForm
|
|
from wtforms import StringField, PasswordField, BooleanField, \
|
|
SubmitField
|
|
from wtforms.validators import ValidationError, DataRequired, \
|
|
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):
|
|
# 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=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": f"validate_confirm({minlength})"})
|
|
|
|
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():
|
|
form = ResetPasswordForm()
|
|
if form.validate_on_submit():
|
|
return f'''<h1> Welcome {form.username.data} </h1>'''
|
|
return render_template('reset.html', form=form) |