【问题标题】:send customized response from connexion flask server when token authorization fails令牌授权失败时从连接烧瓶服务器发送自定义响应
【发布时间】:2020-06-27 22:14:52
【问题描述】:

在每个端点被命中之前,令牌被授权。我创建了与下面相同的应用程序

os.environ[TOKENINFO_FUNC] =  some_function
add.api(openapi.yaml)
app.add_error_handler(OAuthResponseProblem, render_unauthorized)

def render_unauthorized:
  return customized response 
def some_function:
  raise OAuthResponseProblem

这是挂钩异常的正确方法吗?我收到上述代码的错误。这里 render_unauthorized 没有被调用。我想验证令牌并发送端点用户自定义响应。提前致谢。

【问题讨论】:

    标签: python flask oauth-2.0 authorization connexion


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      抱歉,如果为时已晚。我遇到了同样的问题。您需要做的就是使用您的自定义字符串引发未经授权的异常。

      import jwt    
      from werkzeug.exceptions import Unauthorized    
      
      # This is your TOKENINFO_FUNC
      def decode_token(jwt_token):
        try:
          payload = jwt.decode(jwt_token, 'SECRET_KEY', algorithms=['HS256'])
          return payload
        except jwt.ExpiredSignatureError:
          raise Unauthorized('Token expired. Please log in again.')
        except jwt.InvalidTokenError:
          raise Unauthorized('Invalid token. Please log in again.')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        • 2020-05-09
        • 1970-01-01
        • 2021-01-28
        相关资源
        最近更新 更多