def validate_signed_token(uid, token, require_token=True):
"""
Validates a signed token and uid and returns the user who owns it.
:param uid: The uid of the request
:param token: The signed token of the request if one exists
:param require_token: Whether or not there is a signed token, the token parameter is ignored if False
:return: The user who's token it is, if one exists, None otherwise
"""
user_model = get_user_model()
try:
uid = force_text(urlsafe_base64_decode(uid))
user = user_model.objects.get(pk=uid)
if require_token:
if user is not None and default_token_generator.check_token(user, token):
return user
else:
return user
except (TypeError, ValueError, OverflowError, user_model.DoesNotExist):
pass
return None
评论列表
文章目录