Skip to content

Commit e20f5dd

Browse files
committed
Added password hashing, strength check
1 parent 4cca9e7 commit e20f5dd

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/server/api/user_api.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ def log_user_action(user, event_class, detail):
3333
except Exception as e:
3434
print(e)
3535

36+
def password_is_strong(password):
37+
""" Check plain-text password against strength rules."""
38+
39+
def has_digit(test_string):
40+
"""Test if any character is a digit."""
41+
for c in test_string:
42+
if c.isdigit():
43+
return True
44+
return False
45+
46+
def has_alpha(test_string):
47+
"""Test if any character is alphabetic."""
48+
for c in test_string:
49+
if c.isalpha():
50+
return True
51+
return False
52+
53+
if (len(password) > 11
54+
and has_alpha(password)
55+
and has_digit(password) ):
56+
return True
57+
58+
else:
59+
return False
60+
3661

3762
def hash_password(password):
3863
""" Generate salt+hash for storing in db"""
@@ -331,8 +356,6 @@ def user_update():
331356
except:
332357
return jsonify("Must specify username"), 400
333358

334-
# We should get 1+ values to update
335-
336359
update_dict = {}
337360

338361
# Need to be a bit defensive here & select what we want instead of taking what we're given
@@ -343,10 +366,17 @@ def user_update():
343366
except:
344367
pass
345368

346-
if not update_dict:
347-
return jsonify("No changed items specified")
348369

349-
# TODO: If updating password, need to hash first
370+
if not update_dict:
371+
return jsonify("No changed items specified") # If nothing to do, declare victory
372+
373+
if "password" in update_dict.keys():
374+
375+
if password_is_strong(update_dict['password']):
376+
update_dict['password'] = hash_password(update_dict['password'])
377+
else:
378+
return jsonify("Password too weak")
379+
350380

351381

352382
# We have a variable number of columns to update.
@@ -364,6 +394,7 @@ def user_update():
364394
PU = Table("pdp_users", metadata, autoload=True, autoload_with=engine)
365395
# pr = Table("pdp_user_roles", metadata, autoload=True, autoload_with=engine)
366396

397+
#TODO: Check tendered role or join roles table for update
367398

368399
stmt = update(PU).where(PU.columns.username == username).values(update_dict).\
369400
execution_options(synchronize_session="fetch")

0 commit comments

Comments
 (0)