utils.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:grest 作者: mostafa 项目源码 文件源码
def authenticate(func):
    @wraps(func)
    def authenticate_requests(*args, **kwargs):
        """
        The authentication_function can be either empty, which
        results in all requests being taken as granted and authenticated.
        Otherwise the authentication_function must return one of these values:
        1- False -> To indicate the user is not authenticated
        2- g.user global user instance ->
            + not None: access is granted.
            + None: access is denied.
        3- jsonified error message:
            + It is directly returned to user, e.g.:
                return jsonify(error="Authentication failed!"), 403
        """
        authenticated = False
        if (global_config.DEBUG):
            app.ext_logger.info(
                request.endpoint.replace(":", "/").replace(".", "/").lower())
        # authenticate users here!
        if hasattr(app, "authentication_function"):
            authenticated = app.authentication_function(
                global_config.X_AUTH_TOKEN)
        else:
            return func(*args, **kwargs)

        if authenticated is False:
            return jsonify(errors=["Authentication failed!"]), 403
        elif g.user is not None:
            return func(*args, **kwargs)
        else:
            return authenticated
    return authenticate_requests
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号