From 0ef78647fb7efb9ede08cc6fb1d1faa258b76f70 Mon Sep 17 00:00:00 2001 From: Alexandre CHAZAL Date: Mon, 6 Dec 2021 18:29:43 +0100 Subject: [PATCH] fix(password): fixed pep8 lint errors --- app/password.py | 112 +++++++++++++++++++++++++++++------------------ requirements.txt | 4 +- 2 files changed, 72 insertions(+), 44 deletions(-) diff --git a/app/password.py b/app/password.py index 5153c8f..88f7efb 100644 --- a/app/password.py +++ b/app/password.py @@ -1,51 +1,64 @@ -from . import ldap_client - +from . import ( + ldap_client +) from flask import ( - Blueprint, render_template, flash, + Blueprint, + render_template, + flash, current_app ) -from flask_wtf import FlaskForm +from flask_wtf import ( + FlaskForm +) from wtforms import ( - StringField, PasswordField, - SubmitField, EmailField + StringField, + PasswordField, + SubmitField, + EmailField ) from wtforms.validators import ( - ValidationError, DataRequired, - EqualTo, Length, Regexp, Email + ValidationError, + DataRequired, + EqualTo, + Length, + Regexp, + Email ) bp = Blueprint('password', __name__, url_prefix='/password') + class ChangePasswordForm(FlaskForm): # Minimal password length minlength = 9 # Form - username = StringField(label=('Login'), - validators=[DataRequired(), - Length(max=64)], - render_kw={"onkeyup": "validate_username()"}) - currentpassword = PasswordField(label=('Current password'), - validators=[DataRequired()]) - newpassword = PasswordField(label=('New password'), - validators=[DataRequired(), - Length(min=minlength), - Regexp("^(?=.*[a-z])"), - Regexp("^(?=.*[A-Z])"), - Regexp("^(?=.*\\d)"), - #Regexp( - # "(?=.*[@$!%*#?&])", message="Password must contain a special character" - #),], - ], - render_kw={"onkeyup": f"validate_username_form({minlength})"}) + username = StringField(label=('Login'), + validators=[DataRequired(), + Length(max=64)], + render_kw={"onkeyup": "validate_username()"}) + currentpassword = PasswordField(label=('Current password'), + validators=[DataRequired()]) + newpassword = PasswordField(label=('New password'), + validators=[DataRequired(), + Length(min=minlength), + Regexp("^(?=.*[a-z])"), + Regexp("^(?=.*[A-Z])"), + Regexp("^(?=.*\\d)"), + ], + render_kw={"onkeyup": "validate_username_form" + f"({minlength})"}) confirm_password = PasswordField( - label=('Confirm Password'), + label=('Confirm Password'), validators=[DataRequired(message='* Required'), - EqualTo('newpassword')], + EqualTo('newpassword')], render_kw={"onkeyup": f"validate_username_form({minlength})"}) - submit = SubmitField(label=('Change my password'), render_kw={"disabled": "true", - "onclick": f"validate_username_form({minlength})"}) + submit = SubmitField( + label=('Change my password'), + render_kw={ + "disabled": "true", + "onclick": f"validate_username_form({minlength})"}) # Validators def validate_username(self, username): @@ -55,32 +68,45 @@ class ChangePasswordForm(FlaskForm): raise ValidationError( f"Character {char} is not allowed in an username.") -class ResetPasswordForm(FlaskForm): - email = EmailField(label=('Email address'), - validators=[DataRequired(), Email()], - render_kw={"onkeyup": f"validate_email()"}) - submit = SubmitField(label=('Change my password'), render_kw={"disabled": "true", - "onclick": f"validate_email()"}) +class ResetPasswordForm(FlaskForm): + email = EmailField(label=('Email address'), + validators=[DataRequired(), Email()], + render_kw={"onkeyup": "validate_email()"}) + + submit = SubmitField( + label=('Change my password'), + render_kw={ + "disabled": "true", + "onclick": "validate_email()"}) + @bp.route('/change', methods=["GET", "POST"]) def change(): form = ChangePasswordForm() if form.validate_on_submit(): - client = ldap_client.Client(address=current_app.config["LDAP_ADDR"], port=current_app.config["LDAP_PORT"], base_dn=current_app.config["BASE_DN"], tls=current_app.config["LDAP_TLS"]) + client = ldap_client.Client(address=current_app.config["LDAP_ADDR"], + port=current_app.config["LDAP_PORT"], + base_dn=current_app.config["BASE_DN"], + tls=current_app.config["LDAP_TLS"]) - bind_status = client.bind(form.username._value(), form.currentpassword._value()) - if bind_status[0] == False: - flash(f"Connection failed, are you sure that your login and password are correct ? ({client.link.last_error})") - elif client.change_pwd(bind_status[1], form.newpassword._value()) == False: - flash(f"An error occured and your password was not changed, sorry. ({client.link.last_error})") + bind_status = client.bind( + form.username._value(), form.currentpassword._value()) + if bind_status[0] is False: + flash("Connection failed, are you sure that your login and" + f" password are correct ? ({client.link.last_error})") + elif client.change_pwd(bind_status[1], + form.newpassword._value()) is False: + flash("An error occured and your password was not changed, sorry." + f"({client.link.last_error})") client.unbind() else: flash('Your password has been changed !') client.unbind() - + return render_template('change.html', form=form) + @bp.route('/reset', methods=["GET"]) def reset(): - return render_template('reset.html') \ No newline at end of file + return render_template('reset.html') diff --git a/requirements.txt b/requirements.txt index 071afd4..8f5ecee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,6 @@ Werkzeug==2.0.2 zipp==3.6.0 ldap3 Flask-WTF==1.0.0 -email-validator \ No newline at end of file +email-validator +flake8 +bandit \ No newline at end of file