Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
hub-client / dockerhub / authentication.py
Size: Mime:
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