コード例 #1
0
    def authenticate(self, request):
        """
        Returns a `User` if a correct username and password have been supplied
        using HTTP Basic authentication.  Otherwise returns `None`.
        """
        auth = request.META.get('HTTP_AUTHORIZATION', b'')
        if type(auth) == type(''):
            # Work around django test client oddness
            auth = auth.encode(HTTP_HEADER_ENCODING)
        auth = auth.split()

        if not auth or auth[0].lower() != b'basic':
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed(user_messages.get("invalid_basic_header"))

        try:
            auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':')
        except (TypeError, UnicodeDecodeError):
            raise exceptions.AuthenticationFailed(user_messages.get("invalid_basic_header"))

        try:
            userid, password = auth_parts[0], auth_parts[2]
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed(user_messages.get("invalid_basic_header"))

        return self.authenticate_credentials(userid, password)
コード例 #2
0
    def authenticate(self, request):
        """
        Returns a `User` if a correct username and password have been supplied
        using HTTP Basic authentication.  Otherwise returns `None`.
        """
        auth = request.META.get('HTTP_AUTHORIZATION', b'')
        if type(auth) == type(''):
            # Work around django test client oddness
            auth = auth.encode(HTTP_HEADER_ENCODING)
        auth = auth.split()

        if not auth or auth[0].lower() != b'basic':
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed(
                user_messages.get("invalid_basic_header"))

        try:
            auth_parts = base64.b64decode(
                auth[1]).decode(HTTP_HEADER_ENCODING).partition(':')
        except (TypeError, UnicodeDecodeError):
            raise exceptions.AuthenticationFailed(
                user_messages.get("invalid_basic_header"))

        try:
            userid, password = auth_parts[0], auth_parts[2]
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed(
                user_messages.get("invalid_basic_header"))

        return self.authenticate_credentials(userid, password)
コード例 #3
0
    def authenticate_credentials(self, key):
        """
        Authenticate the token.
        """
        try:
            user = User.objects.get(token=key)
        except Exception:
            raise exceptions.AuthenticationFailed(user_messages.get("invalid token"))

        if user.is_valid_token():
            return (user, None)

        raise exceptions.AuthenticationFailed(user_messages.get("invalid_token"))
コード例 #4
0
    def authenticate_credentials(self, key):
        """
        Authenticate the token.
        """
        try:
            user = User.objects.get(token=key)
        except Exception:
            raise exceptions.AuthenticationFailed(
                user_messages.get("invalid token"))

        if user.is_valid_token():
            return (user, None)

        raise exceptions.AuthenticationFailed(
            user_messages.get("invalid_token"))
コード例 #5
0
ファイル: serializers.py プロジェクト: brunozrk/contcomb_api
 def validate_confirm_password(self, attrs, source):
     if not self.object:
         password = attrs['password']
         confirm_password = attrs[source]
 
         if password != confirm_password:
             raise serializers.ValidationError(user_messages.get("invalid_confirm_password"))
     return attrs
コード例 #6
0
    def validate_confirm_password(self, attrs, source):
        if not self.object:
            password = attrs['password']
            confirm_password = attrs[source]

            if password != confirm_password:
                raise serializers.ValidationError(
                    user_messages.get("invalid_confirm_password"))
        return attrs
コード例 #7
0
ファイル: serializers.py プロジェクト: brunozrk/contcomb_api
    def validate_username(self, attrs, source):
        username = attrs[source]
        try:
            User.objects.get_by_username(username)
            raise serializers.ValidationError(user_messages.get("duplicated_username") % username)
        except ObjectDoesNotExist:
            pass

        return attrs
コード例 #8
0
    def authenticate(self, request):
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "token":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed(user_messages.get("invalid_token_header"))

        return self.authenticate_credentials(auth[1])
コード例 #9
0
    def validate_username(self, attrs, source):
        username = attrs[source]
        try:
            User.objects.get_by_username(username)
            raise serializers.ValidationError(
                user_messages.get("duplicated_username") % username)
        except ObjectDoesNotExist:
            pass

        return attrs
コード例 #10
0
    def authenticate(self, request):
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "token":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed(
                user_messages.get("invalid_token_header"))

        return self.authenticate_credentials(auth[1])