def authenticate(self, request):
"""
Returns a `Person` if a correct access token has been supplied. Otherwise returns `None`.
"""
auth = get_authorization_header(request).split()
if not auth or auth[0].lower() != b'bearer':
return None
if len(auth) == 1:
msg = _('Invalid basic header. No credentials provided.')
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = _('Invalid basic header. Credentials string should not contain spaces.')
raise exceptions.AuthenticationFailed(msg)
try:
token = AccessToken.get_token(auth[1].decode())
except (InvalidTokenException, UnicodeDecodeError):
msg = _('Token invalide.')
raise exceptions.AuthenticationFailed(msg)
token.person.role.token = token
return token.person.role, token
评论列表
文章目录