Skip to content

Commit 4becd33

Browse files
committed
Centralize password check as method of 'User' objects
1 parent c2a2583 commit 4becd33

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

KerbalStuff/blueprints/accounts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def login() -> Union[str, werkzeug.wrappers.Response]:
138138
return render_template("login.html", username=username, errors='Your username or password is incorrect.')
139139
if user.confirmation != '' and user.confirmation is not None:
140140
return redirect("/account-pending")
141-
if not bcrypt.hashpw(password.encode('utf-8'), user.password.encode('utf-8')) == user.password.encode('utf-8'):
141+
if not user.check_password(password):
142142
return render_template("login.html", username=username, errors='Your username or password is incorrect.')
143143
login_user(user, remember=remember)
144144
if 'return_to' in request.form and request.form['return_to']:

KerbalStuff/blueprints/api.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ def login() -> Union[Dict[str, Any], Tuple[Dict[str, Any], int]]:
363363
user = User.query.filter(User.username.ilike(username)).first()
364364
if not user:
365365
return {'error': True, 'reason': 'Username or password is incorrect'}, 401
366-
if not bcrypt.hashpw(password.encode('utf-8'),
367-
user.password.encode('utf-8')) == user.password.encode('utf-8'):
366+
if not user.check_password(password):
368367
return {'error': True, 'reason': 'Username or password is incorrect'}, 401
369368
if user.confirmation and user.confirmation is not None:
370369
return {'error': True, 'reason': 'User is not confirmed'}, 403
@@ -439,7 +438,7 @@ def change_password(username: str) -> Union[Dict[str, Any], Tuple[Union[str, Any
439438
new_password = request.form.get('new-password', '')
440439
new_password_confirm = request.form.get('new-password-confirm', '')
441440

442-
if not bcrypt.hashpw(old_password.encode('utf-8'), current_user.password.encode('utf-8')) == current_user.password.encode('utf-8'):
441+
if not current_user.check_password(old_password):
443442
return {'error': True, 'reason': 'The old password you entered doesn\'t match your current account password.'}
444443

445444
pw_valid, pw_message = check_password_criteria(new_password, new_password_confirm)

KerbalStuff/objects.py

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class User(Base): # type: ignore
6767
def set_password(self, password: str) -> None:
6868
self.password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
6969

70+
def check_password(self, password: str) -> bool:
71+
return bcrypt.checkpw(password.encode('utf-8'), self.password.encode('utf-8'))
72+
7073
def create_confirmation(self) -> None:
7174
self.confirmation = binascii.b2a_hex(os.urandom(20)).decode('utf-8')
7275

requirements-backend.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
alembic
2-
bcrypt
2+
bcrypt>=3.1.0
33
bleach
44
bleach-allowlist
55
celery

0 commit comments

Comments
 (0)