如何解决Django Graphql 的自动刷新令牌中间件
我正在尝试使用 React 和前端的 Apollo Client 和后端的 Django GraphQl 构建一个站点,我正在使用带有 jwt 令牌的 django-graphql-auth 来处理身份验证。
现在我正在尝试编写一个中间件,通过 http 标头获取访问和刷新令牌,如果访问令牌已过期,我想使用刷新令牌刷新令牌。
我能够执行基本步骤,例如从请求中获取访问和刷新令牌,但无法弄清楚如何刷新访问令牌。
def refreshTokenMiddleWare(get_response):
# this middleware functionality is check the validity of the
# token and if the access token is expired then the access token
# is refreshed and set the appropriate headers
def middleware(request):
# implement the logic
# get token and refresh token
AUTH_HEADER = request.headers.get('authorization')
refreshToken = request.headers.get('X-Refresh-Token')
token = get_http_authorization(request)
# check if the tokens are valid
try:
payload = jwt_settings.JWT_DECODE_HANDLER(token)
except jwt.ExpiredSignature:
pass
except jwt.DecodeError:
raise exceptions.JSONWebTokenError(_('Error decoding signature'))
except jwt.InvalidTokenError:
raise exceptions.JSONWebTokenError(_('Invalid token'))
# check if token is expired
# refresh token
# add appropriate headers
response = get_response(request)
return response
return middleware
如果发生 jwt.ExpiredSignature 错误,我无法弄清楚要写什么。任何帮助将不胜感激。提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。