Beispiel #1
0
def test_should_return_raise_value_error():
    # Given
    token = None
    expires_at = datetime.now(tz=timezone.utc) + timedelta(seconds=60)

    # When
    with pytest.raises(ValueError):
        use_jti_claim(token, expires_at)
Beispiel #2
0
    def test_should_return_raise_value_error(self):
        # Given
        token = None
        expires = datetime.now(tz=tzutc()) + timedelta(seconds=60)

        # When
        with self.assertRaises(ValueError):
            use_jti_claim(token, expires)
Beispiel #3
0
def test_should_use_token(app, mock_redis_put):
    with app.app_context():
        # Given
        jti_token = str(uuid4())
        expires_at = datetime.now(tz=timezone.utc) + timedelta(seconds=60)

        # When
        use_jti_claim(jti_token, expires_at)

        # Then
        assert mock_redis_put.call_count == 1
Beispiel #4
0
    def test_should_use_token(self):
        # Given
        jti_token = str(uuid4())
        expires_at = datetime.now(tz=tzutc()) + timedelta(seconds=60)

        # When
        with patch("app.storage.redis.Redis.put") as add:
            use_jti_claim(jti_token, expires_at)

            # Then
            self.assertEqual(add.call_count, 1)
Beispiel #5
0
def validate_jti(decrypted_token):
    expires = datetime.utcfromtimestamp(decrypted_token['exp']).replace(tzinfo=tzutc())
    if expires < datetime.now(tz=tzutc()):
        raise Unauthorized

    jti_claim = decrypted_token.get('jti')
    try:
        use_jti_claim(jti_claim, expires)
    except JtiTokenUsed as e:
        raise Unauthorized from e
    except (TypeError, ValueError) as e:
        raise InvalidTokenException from e
Beispiel #6
0
    def test_should_raise_jti_token_used_when_token_already_exists(self):
        # Given
        jti_token = str(uuid4())
        expires = datetime.now(tz=tzutc()) + timedelta(seconds=60)

        # When
        with self.assertRaises(JtiTokenUsed) as err:
            with patch('app.storage.data_access.put', side_effect=[ItemAlreadyExistsError()]):
                use_jti_claim(jti_token, expires)

        # Then
        self.assertEqual(err.exception.jti_claim, jti_token)
        self.assertEqual(str(err.exception), "jti claim '{}' has already been used".format(jti_token))
Beispiel #7
0
def validate_jti(decrypted_token):
    expires_at = datetime.fromtimestamp(decrypted_token["exp"], tz=timezone.utc)
    jwt_expired = expires_at < datetime.now(tz=timezone.utc)
    if jwt_expired:
        raise Unauthorized

    jti_claim = decrypted_token.get("jti")
    try:
        use_jti_claim(jti_claim, expires_at)
    except JtiTokenUsed as e:
        raise Unauthorized from e
    except (TypeError, ValueError) as e:
        raise InvalidTokenException from e
Beispiel #8
0
def test_should_raise_jti_token_used_when_token_already_exists(
        app, mock_redis_put):
    with app.app_context():
        # Given
        jti_token = str(uuid4())
        expires_at = datetime.now(tz=timezone.utc) + timedelta(seconds=60)

        # When
        with pytest.raises(JtiTokenUsed) as excinfo:
            mock_redis_put.side_effect = [ItemAlreadyExistsError()]
            use_jti_claim(jti_token, expires_at)

        # Then
        assert str(
            excinfo.value) == f"jti claim '{jti_token}' has already been used"
Beispiel #9
0
    def test_should_raise_type_error_invalid_uuid(self):
        jti_token = 'jti_token'
        expires = datetime.now(tz=tzutc()) + timedelta(seconds=60)

        with self.assertRaises(TypeError):
            use_jti_claim(jti_token, expires)
Beispiel #10
0
def test_should_raise_type_error_invalid_uuid():
    jti_token = "jti_token"
    expires_at = datetime.now(tz=timezone.utc) + timedelta(seconds=60)

    with pytest.raises(TypeError):
        use_jti_claim(jti_token, expires_at)