Skip to content

Commit

Permalink
Refactor password write function
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Putt authored and Robert Putt committed Jan 4, 2023
1 parent 803f23b commit 05bb898
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
38 changes: 38 additions & 0 deletions hw_diag/utilities/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from functools import wraps
from flask import redirect
from flask import session
from password_strength import PasswordPolicy

from hw_diag.utilities.diagnostics import read_diagnostics_file

Expand Down Expand Up @@ -54,3 +55,40 @@ def check_password(password):
return True
else:
return False


def write_new_password(current_password, new_password, confirm_password):
error = False
msg = ''

if not check_password(current_password):
error = True
msg = 'Current password is not valid.'

if new_password != confirm_password:
error = True
msg = 'New password and password confirmation do not match.'

policy = PasswordPolicy.from_names(
length=8, # min length: 8
uppercase=1, # need min. 2 uppercase letters
numbers=1, # need min. 2 digits
special=1, # need min. 2 special characters
nonletters=0, # need min. 2 non-letter characters (digits, specials, anything)
)

if len(policy.test(new_password)) > 0:
error = True
msg = (
'Password is not complex enough, please ensure password is greater than 8 '
'characters, has atleast 1 number, 1 uppercase character and 1 special character.'
)

if not error:
write_password_file(new_password)
msg = 'Password updated successfully.'

return {
'error': error,
'msg': msg
}
35 changes: 6 additions & 29 deletions hw_diag/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
from flask import request
from flask import redirect
from flask import session
from password_strength import PasswordPolicy
from hm_pyhelper.logger import get_logger

from hw_diag.utilities.diagnostics import read_diagnostics_file
from hw_diag.utilities.hardware import should_display_lte
from hw_diag.utilities.auth import check_password
from hw_diag.utilities.auth import authenticate
from hw_diag.utilities.auth import write_password_file
from hw_diag.utilities.auth import write_new_password


logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG"))
Expand Down Expand Up @@ -56,39 +55,17 @@ def get_password_change_form():
@AUTH.route('/change_password', methods=['POST'])
@authenticate
def handle_password_change():
error = False
msg = ''
current_password = request.form.get('txtOriginalPassword')
new_password = request.form.get('txtNewPassword')
confirm_password = request.form.get('txtConfirmPassword')

if not check_password(current_password):
error = True
msg = 'Current password is not valid.'

if new_password != confirm_password:
error = True
msg = 'New password and password confirmation do not match.'

policy = PasswordPolicy.from_names(
length=8, # min length: 8
uppercase=1, # need min. 2 uppercase letters
numbers=1, # need min. 2 digits
special=1, # need min. 2 special characters
nonletters=0, # need min. 2 non-letter characters (digits, specials, anything)
result = write_new_password(
current_password,
new_password,
confirm_password
)

if len(policy.test(new_password)) > 0:
error = True
msg = (
'Password is not complex enough, please ensure password is greater than 8 '
'characters, has atleast 1 number, 1 uppercase character and 1 special character.'
)

if not error:
write_password_file(new_password)
msg = 'Password updated successfully.'

msg = result.get('msg')
diagnostics = read_diagnostics_file()
now = datetime.datetime.utcnow()
template_filename = 'password_change_form.html'
Expand Down

0 comments on commit 05bb898

Please sign in to comment.