@@ -33,6 +33,31 @@ def log_user_action(user, event_class, detail):
33
33
except Exception as e :
34
34
print (e )
35
35
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
+
36
61
37
62
def hash_password (password ):
38
63
""" Generate salt+hash for storing in db"""
@@ -331,8 +356,6 @@ def user_update():
331
356
except :
332
357
return jsonify ("Must specify username" ), 400
333
358
334
- # We should get 1+ values to update
335
-
336
359
update_dict = {}
337
360
338
361
# 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():
343
366
except :
344
367
pass
345
368
346
- if not update_dict :
347
- return jsonify ("No changed items specified" )
348
369
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
+
350
380
351
381
352
382
# We have a variable number of columns to update.
@@ -364,6 +394,7 @@ def user_update():
364
394
PU = Table ("pdp_users" , metadata , autoload = True , autoload_with = engine )
365
395
# pr = Table("pdp_user_roles", metadata, autoload=True, autoload_with=engine)
366
396
397
+ #TODO: Check tendered role or join roles table for update
367
398
368
399
stmt = update (PU ).where (PU .columns .username == username ).values (update_dict ).\
369
400
execution_options (synchronize_session = "fetch" )
0 commit comments