Repository URL to install this package:
|
Version:
0.8.0 ▾
|
from rest_framework_jwt.authentication import JSONWebTokenAuthentication, jwt_get_user_id_from_payload
from rest_framework import exceptions
from django.contrib.auth import get_user_model
from dockerhub.constants import UserTypes
User = get_user_model()
class HubJSONWebTokenAuthentication(JSONWebTokenAuthentication):
def authenticate_credentials(self, payload):
"""
Returns an active user that matches the payload's user id and email.
"""
try:
user_id = jwt_get_user_id_from_payload(payload)
if user_id is not None:
user = User.objects.get(
username=user_id,
is_active=True,
type=UserTypes.USER,
)
else:
msg = 'Invalid payload'
raise exceptions.AuthenticationFailed(msg)
except User.DoesNotExist:
msg = 'Invalid signature'
raise exceptions.AuthenticationFailed(msg)
return user