我使用flask安全来验证用户身份。我已经确保了身份验证在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的同时,使用自定义身份验证方法的正确方法是什么?
发布于 2017-03-22 15:51:02
你可以用一个快速的猴子补丁来完成这件事。不是很理想,但我不确定你还能做什么,直到Flask-Security团队用一种更优雅的方式来处理这个问题。
import flask_security
def verify_and_update_password_custom(password, user):
return user.verify_password(password)
flask_security.forms.verify_and_update_password = verify_and_update_password_custom我不确定它是否在其他地方使用。上面的工作是为了我自己的目的。如果它确实在其他地方被调用,你只需要把它放在任何地方。
https://stackoverflow.com/questions/39449480
复制相似问题