decorators.py 文件源码

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

项目:is-service-up 作者: marcopaz 项目源码 文件源码
def authenticated(blocking=True):

    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            user = None
            auth = request.headers.get('Authorization')
            if auth and len(auth) >= 7:
                sid = auth[7:]  # 'Bearer ' prefix
                session = sessions.get(sid)
                if session and session.get('user_id'):
                    user = get_user(session['user_id'])
                    if user:
                        user.sid = sid
            if blocking and not user:
                raise ApiException('unauthorized', status_code=401)
            res = f(user=user, *args, **kwargs)
            return res
        return wrapper

    return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号