reset_pwd.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import bcrypt
  2. import pymysql
  3. import sys
  4. import myloginpath
  5. def get_db_connection():
  6. conf = myloginpath.parse('app_auth')
  7. return pymysql.connect(
  8. host=conf.get('host', '127.0.0.1'),
  9. user=conf.get('user', 'db_app_auth'),
  10. password=conf.get('password'),
  11. database='food_db',
  12. cursorclass=pymysql.cursors.DictCursor,
  13. autocommit=True
  14. )
  15. def reset_pwd(username, plain_password):
  16. conn = get_db_connection()
  17. if not conn:
  18. print("Failed DB connection!")
  19. sys.exit(1)
  20. hashed = bcrypt.hashpw(plain_password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
  21. with conn.cursor() as cursor:
  22. rows = cursor.execute("UPDATE users SET password_hash = %s WHERE username = %s", (hashed, username))
  23. if rows > 0:
  24. print(f"✅ Successfully updated password for {username}!")
  25. else:
  26. print(f"❌ User '{username}' not found in database!")
  27. conn.close()
  28. if __name__ == "__main__":
  29. if len(sys.argv) < 3:
  30. username = input("Enter Username: ")
  31. plain_password = input("Enter New Password: ")
  32. else:
  33. username = sys.argv[1]
  34. plain_password = sys.argv[2]
  35. reset_pwd(username, plain_password)