class CustomAuth(JSONWebTokenAuthentication):
def authenticate_credentials(self, payload):
"""
Returns an active user that matches the payload's user id and email.
"""
User = get_user_model()
# Thingsboard payload
if payload.get('sub') is not None:
username = payload.get('sub')
else:
# Our payload
username = payload.get('username')
if not username:
msg = _('Invalid payload.')
raise exceptions.AuthenticationFailed(msg)
try:
user = User.objects.get_by_natural_key(username)
except User.DoesNotExist:
msg = _('Invalid signature.')
raise exceptions.AuthenticationFailed(msg)
if not user.is_active:
msg = _('User account is disabled.')
raise exceptions.AuthenticationFailed(msg)
return user
flask jwt extended的版本3.21.0添加了一个
JWT_DECODE_ISSUER
选项,该选项可以为您解决这个问题:https://github.com/vimalloc/flask-jwt-extended/releases/tag/3.21.0确保
JWT_AUTH_USERNAME_KEY
与库用于获取用户的用户名匹配。 例如,当您获得Thingsboard JWT时,用户名在sub
键中,许多库默认使用username
。你知道吗也许这段代码可以帮助您(implementation with JWT Rest Framework:
相关问题 更多 >
编程相关推荐