def wrapper(*args, **kwargs): res = ResMsg() token = request.headers.get("access_token", default=None) if not token: res.update(code=ResponseCode.PleaseSignIn) return res.data try: payload = jwt.decode(token, current_app.config['SECRET_KEY'], algorithms=['HS256']) if payload["exp"] < int(time.time()): res.update(code=ResponseCode.PleaseReSignIn) return res.data except ExpiredSignatureError: res.update(code=ResponseCode.PleaseSignIn) return res.data if payload: # 获取到用户并写入到g.current_user中,方便后续使用 g.current_user = WXUser.verify_jwt(token) if token else None return f(*args, **kwargs) res.update(code=ResponseCode.PleaseSignIn) return res.data
def wrapper(*args, **kwargs): res = ResMsg() token = request.headers.get("Authorization", default=None) if not token: res.update(code=ResponseCode.PleaseSignIn) return res.data auth = Auth() user_name = auth.identify(token) if not user_name: res.update(code=ResponseCode.PleaseSignIn) return res.data # 获取到用户并写入到session中,方便后续使用 session["user_name"] = user_name return f(*args, **kwargs)
def wrapper(*args, **kwargs): res = ResMsg() # token = request.headers.get("Authorization", default=None) token = request.headers.get("xtoken") if not token: res.update(code=ResponseCode.PleaseSignIn, msg=f"{request.headers}") return res.data auth = Auth() user_name = auth.identify(token) if not user_name: res.update(code=ResponseCode.PleaseSignIn, msg=f"{user_name}") return res.data session["user_name"] = user_name return f(*args, **kwargs)
def token_auth_error(): '''用于在 Token Auth 认证失败的情况下返回错误响应''' res = ResMsg() res.update(code=ResponseCode.PleaseSignIn) return res.data