【发布时间】:2022-12-05 22:17:25
【问题描述】:
这是一个通过Flask-HTTPAuth 验证的小型 Flask 应用程序。
如何将下面的authorized_users_dict传递给用@auth.verify_password装饰的authenticate函数(不引发错误)?
理由:我想通过将带有用户凭据的字典更明确地传递给修饰的自动函数来提高代码的可读性和可测试性(例如,使用函数调用,而不是通过全局变量隐式调用)。
当前代码:
auth = HTTPBasicAuth()
authorized_users_dict = [..]
# [..]
@auth.verify_password
def authenticate(username, password):
#######################################################
# caution: authorized_users_dict passed via global env.
#######################################################
if username in authorized_users_dict:
if check_password_hash(pwhash=authorized_users_dict[username], password=password):
return True
# [..]
# [..]
@auth.login_required()
# [..]
【问题讨论】:
标签: python authentication flask basic-authentication