def test_it_should_not_validate_if_token_has_wrong_type(self):
        token = SlidingToken()
        token[api_settings.TOKEN_TYPE_CLAIM] = 'wrong_type'

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})
        self.assertFalse(s.is_valid())
        self.assertIn('non_field_errors', s.errors)
        self.assertIn("wrong type", s.errors['non_field_errors'][0])
Example #2
0
    def test_it_should_raise_token_error_if_token_has_wrong_type(self):
        token = SlidingToken()
        token[api_settings.TOKEN_TYPE_CLAIM] = 'wrong_type'

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})

        with self.assertRaises(TokenError) as e:
            s.is_valid()

        self.assertIn("wrong type", e.exception.args[0])
Example #3
0
    def test_it_should_raise_token_error_if_token_has_refresh_period_expired(self):
        token = SlidingToken()
        token.set_exp(api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM, lifetime=-timedelta(days=1))

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})

        with self.assertRaises(TokenError) as e:
            s.is_valid()

        self.assertIn("'{}' claim has expired".format(api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM), e.exception.args[0])
Example #4
0
    def test_it_should_raise_token_error_if_token_has_no_refresh_exp_claim(self):
        token = SlidingToken()
        del token[api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM]

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})

        with self.assertRaises(TokenError) as e:
            s.is_valid()

        self.assertIn("has no '{}' claim".format(api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM), e.exception.args[0])
    def test_it_should_not_validate_if_token_has_no_refresh_exp_claim(self):
        token = SlidingToken()
        del token[api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM]

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})
        self.assertFalse(s.is_valid())
        self.assertIn('non_field_errors', s.errors)
        self.assertIn(
            "has no '{}' claim".format(
                api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM),
            s.errors['non_field_errors'][0])
Example #6
0
def token_refresh_sliding_handler(token):
    """
    采用滑动式JSON网络TOKEN,并在TOKEN的刷新期限尚未到期时返回新的刷新版本。
    """
    ser = TokenRefreshSlidingSerializer(data={'token': token})
    try:
        ser.is_valid(raise_exception=True)
    except AssertionError as e:
        log.info('token校验出错')
        raise InvalidJwtToken(detail='token校验出错')
    res = dict(token=ser.validated_data.get('token'))
    return res
    def test_it_should_not_validate_if_token_has_refresh_period_expired(self):
        token = SlidingToken()
        token.set_exp(api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM,
                      lifetime=-timedelta(days=1))

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})
        self.assertFalse(s.is_valid())
        self.assertIn('non_field_errors', s.errors)
        self.assertIn(
            "'{}' claim has expired".format(
                api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM),
            s.errors['non_field_errors'][0])
Example #8
0
    def test_it_should_update_token_exp_claim_if_everything_ok(self):
        old_token = SlidingToken()

        lifetime = api_settings.SLIDING_TOKEN_LIFETIME - timedelta(seconds=1)
        old_exp = old_token.current_time + lifetime

        old_token.set_exp(lifetime=lifetime)

        # Serializer validates
        s = TokenRefreshSlidingSerializer(data={'token': str(old_token)})
        self.assertTrue(s.is_valid())

        # Expiration claim has moved into future
        new_token = SlidingToken(s.validated_data['token'])
        new_exp = datetime_from_epoch(new_token['exp'])

        self.assertTrue(old_exp < new_exp)
Example #9
0
    def test_it_should_not_validate_if_token_invalid(self):
        token = SlidingToken()
        del token['exp']

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})

        with self.assertRaises(TokenError) as e:
            s.is_valid()

        self.assertIn("has no 'exp' claim", e.exception.args[0])

        token.set_exp(lifetime=-timedelta(days=1))

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})

        with self.assertRaises(TokenError) as e:
            s.is_valid()

        self.assertIn('invalid or expired', e.exception.args[0])
    def test_it_should_not_validate_if_token_invalid(self):
        token = SlidingToken()
        del token['exp']

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})
        self.assertFalse(s.is_valid())
        self.assertIn('non_field_errors', s.errors)
        self.assertIn("has no 'exp' claim", s.errors['non_field_errors'][0])

        token.set_exp(lifetime=-timedelta(days=1))

        s = TokenRefreshSlidingSerializer(data={'token': str(token)})
        self.assertFalse(s.is_valid())
        self.assertIn('non_field_errors', s.errors)
        self.assertIn('invalid or expired', s.errors['non_field_errors'][0])