Exemple #1
0
    def validate(self, attrs):
        token = attrs['token']

        payload = self._check_payload(token=token)
        user = self._check_user(payload=payload)
        # Get and check 'orig_iat'
        orig_iat = payload.get('orig_iat')

        if orig_iat:
            # Verify expiration
            refresh_limit = api_settings.JWT_REFRESH_EXPIRATION_DELTA

            if isinstance(refresh_limit, timedelta):
                refresh_limit = (refresh_limit.days * 24 * 3600 +
                                 refresh_limit.seconds)

            expiration_timestamp = orig_iat + int(refresh_limit)
            now_timestamp = timegm(datetime.utcnow().utctimetuple())

            if now_timestamp > expiration_timestamp:
                msg = _('Refresh has expired.')
                raise serializers.ValidationError(msg)
        else:
            msg = _('orig_iat field is required.')
            raise serializers.ValidationError(msg)

        new_payload = jwt_payload_handler(user)
        new_payload['orig_iat'] = orig_iat

        return {'token': jwt_encode_handler(new_payload), 'user': user}
Exemple #2
0
    def _check_payload(self, token):
        # Check payload valid (based off of JSONWebTokenAuthentication,
        # may want to refactor)
        try:
            payload = jwt_decode_handler(token)
        except jwt.ExpiredSignature:
            msg = _('Signature has expired.')
            raise serializers.ValidationError(msg)
        except jwt.DecodeError:
            msg = _('Error decoding signature.')
            raise serializers.ValidationError(msg)

        return payload
Exemple #3
0
    def _check_user(self, payload):
        username = jwt_get_username_from_payload(payload)

        if not username:
            msg = _('Invalid payload.')
            raise serializers.ValidationError(msg)

        # Make sure user exists
        try:
            user = User.objects.get_by_natural_key(username)
        except User.DoesNotExist:
            msg = _("User doesn't exist.")
            raise serializers.ValidationError(msg)

        if not user.is_active:
            msg = _('User account is disabled.')
            raise serializers.ValidationError(msg)

        return user
Exemple #4
0
    def validate(self, attrs):
        """
        Updating validation to retrieve username
        via email
        """
        email = attrs.get(self.username_field)

        # get user (this is a little inefficient as User DB is being called twice)
        user = User.objects.filter(email=email).first()
        if not user:
            msg = _('Unable to login with provided credentials.')
            raise serializers.ValidationError(msg)

        credentials = {
            self.username_field: user.username,
            'password': attrs.get('password')
        }

        if all(credentials.values()):
            user = authenticate(**credentials)

            if user:
                if not user.is_active:
                    msg = _('User account is disabled.')
                    raise serializers.ValidationError(msg)

                payload = jwt_payload_handler(user)

                return {'token': jwt_encode_handler(payload), 'user': user}
            else:
                msg = _('Unable to login with provided credentials.')
                raise serializers.ValidationError(msg)
        else:
            msg = _('Must include "{username_field}" and "password".')
            msg = msg.format(username_field=self.username_field)
            raise serializers.ValidationError(msg)
Exemple #5
0
 def validate(self, attrs):
     raise serializers.ValidationError("serializer invalid")
Exemple #6
0
 def validate_foo(self, attrs, source):
     raise serializers.ValidationError("foo invalid")
Exemple #7
0
 def validate_renamed(self, value):
     if len(value) < 3:
         raise serializers.ValidationError('Minimum 3 characters.')
     return value
Exemple #8
0
 def validate(self, attrs):
     raise serializers.ValidationError('Non field error')
 def validate(self, attrs):
     raise serializers.ValidationError({'char': 'Field error'})
Exemple #10
0
 def to_python(self, value):
     raise serializers.ValidationError(self.error_messages['invalid_image'])