【发布时间】:2017-01-19 20:12:36
【问题描述】:
我正在使用烧瓶安全性来验证用户身份。我已确保身份验证与 http_auth_required 装饰器一起正常工作 - 正在针对用户存储(在我的情况下为 SQLAlchemyUserDatastore)验证用户,一切都很好。
我现在想使用我自己的身份验证方法(我将使用自定义 LDAP 验证系统),同时仍然利用 Flask-Security 为我提供的功能(例如current_user)。我写了一个自定义装饰器,看起来像这样:
def authenticate_with_ldap(func):
@wraps(func)
def wrapper(*args, **kwargs):
if not request.authorization:
return unauthorized_user_handler()
user = user_datastore.get_user(request.authorization.username)
if not user or not authenticate_with_ldap(user.email, user.password):
return unauthorized_user_handler()
return func(*args, **kwargs)
return wrapper
但是,当我查看 http_auth_required 装饰器时,我发现它使用了一个名为 _check_http_auth 的私有函数,该函数正在做一些我不能在不访问私有成员的情况下自己做的事情,比如将用户设置为请求上下文堆栈的顶部和发送信号。代码如下所示:
def _check_http_auth():
auth = request.authorization or BasicAuth(username=None, password=None)
user = _security.datastore.find_user(email=auth.username)
if user and utils.verify_and_update_password(auth.password, user):
_security.datastore.commit()
app = current_app._get_current_object()
_request_ctx_stack.top.user = user
identity_changed.send(app, identity=Identity(user.id))
return True
return False
所以我的问题是:在充分利用 Flask-Security 的同时,拥有自定义身份验证方法的正确方法是什么?
【问题讨论】:
标签: python flask-login flask-security