Beispiel #1
0
def test_init():
    config = AuthConfig(
        secrets_location=f"/tmp/{uuid.uuid4()}",
        auth_passphrase='test_passphrase',
        enabled=False
    )
    auth.init("test_app", config)
    assert auth.private_keys == {}
    assert auth.public_keys == {}

    config.enabled = True

    with pytest.raises(FileNotFoundError):
        auth.init("test_app", config)

    config.create_keys = True
    auth.init("test_app", config)
    assert "test_app" in auth.private_keys
    assert "test_app" in auth.public_keys
    assert os.path.exists(pathlib.Path(config.secrets_location) / '.private' / 'test_app.pem')
    assert os.path.exists(pathlib.Path(config.secrets_location) / 'public' / 'test_app_pub.pem')

    config.create_keys = False
    auth.init("test_app", config)
    assert "test_app" in auth.private_keys
    assert "test_app" in auth.public_keys
Beispiel #2
0
def test_init():
    config = AuthConfig(
        secrets_location=f"/tmp/{uuid.uuid4()}",
        auth_passphrase='test_passphrase',
        enabled=False
    )
    auth.init(config)
    assert auth.private_key is None
    assert auth.public_key is None

    config.enabled = True

    with pytest.raises(FileNotFoundError):
        auth.init(config)

    config.create_keys = True
    auth.init(config)
    assert auth.private_key is not None
    assert auth.public_key is not None
    assert os.path.exists(pathlib.Path(config.secrets_location) / 'key.pem')
    assert os.path.exists(pathlib.Path(config.secrets_location) / 'key_pub.pem')

    config.create_keys = False
    auth.init(config)
    assert auth.private_key is not None
    assert auth.public_key is not None
Beispiel #3
0
def _setup_client_context(app_config: AppConfig, server_app_config: AppConfig,
                          register_client_key: bool = True) -> EventContext:
    _init_engine_logger(app_config)
    assert app_config.server
    assert server_app_config.server
    app_config.server.auth = AuthConfig(
        secrets_location=f"/tmp/{uuid.uuid4()}",
        auth_passphrase='test_passphrase',
        enabled=True,
        create_keys=True
    )
    auth.init(app_config.app_key(), app_config.server.auth)
    if register_client_key:
        os.rename(
            pathlib.Path(app_config.server.auth.secrets_location) / 'public' / f'{app_config.app_key()}_pub.pem',
            pathlib.Path(server_app_config.server.auth.secrets_location) / 'public' / f'{app_config.app_key()}_pub.pem'
        )
    return EventContext(
        app_config=app_config,
        plugin_config=app_config,
        event_name='mock_event',
        settings=get_event_settings(app_config.effective_settings, 'mock_event'),  # type: ignore
        track_ids={},
        auth_info={}
    )
Beispiel #4
0
def test_token_lifecycle(mock_app_config):  # noqa: F811
    context = _setup_event_context(mock_app_config)
    payload = {
        'test': 'test_value',
        'iat': datetime.now().astimezone(timezone.utc).timestamp(),
        'exp': datetime.now().astimezone(timezone.utc) + timedelta(seconds=2)
    }
    token = auth.new_token(payload)
    assert token is not None
    decoded = auth.validate_token(token, context)
    assert decoded == payload

    time.sleep(3)
    assert auth.validate_token(token, context) is None
    with pytest.raises(ExpiredSignatureError):
        auth.decode_token(token)
    with pytest.raises(DecodeError):
        auth.decode_token('INVALID_TOKEN!!')

    token = auth.new_token(payload={
        'test': 'test_value',
        'iat': datetime.now().astimezone(timezone.utc),
        'exp': datetime.now().astimezone(timezone.utc) + timedelta(seconds=2)
    })
    auth.init(AuthConfig(
        secrets_location=f"/tmp/{uuid.uuid4()}",
        auth_passphrase='test',
        enabled=True,
        create_keys=True
    ))
    assert auth.validate_token(token, context) is None
    with pytest.raises(InvalidSignatureError):
        auth.decode_token(token)
def mock_api_app_config():
    return AppConfig(
        app=AppDescriptor(
            name='mock-app-api',
            version='test'
        ),
        plugins=[AppDescriptor(
            name='mock-plugin', version='test'
        )],
        engine=AppEngineConfig(
            import_modules=['mock_app'],
            read_stream_timeout=1,
            read_stream_interval=5,
            track_headers=['session_id'],
            cors_origin='http://test'
        ),
        events={
            "mock-app-api-get": EventDescriptor(
                type=EventType.GET,
                auth=[AuthType.BASIC],
                route='mock-app-api/test/mock-app-api'
            ),
            "mock-app-api-post": EventDescriptor(
                type=EventType.POST,
                route='mock-app-api/test/mock-app-api'
            ),
            "mock-app-api-multipart": EventDescriptor(
                type=EventType.MULTIPART,
                route='mock-app-api/test/mock-app-api-multipart'
            ),
            "mock-app-api-get-list": EventDescriptor(
                type=EventType.GET,
                auth=[AuthType.REFRESH]
            ),
            "mock-app-api-query-args": EventDescriptor(
                type=EventType.GET,
                auth=[AuthType.REFRESH]
            ),
            "mock-app-noapi": EventDescriptor(
                type=EventType.GET
            ),
            "mock_file_response_content_type": EventDescriptor(
                type=EventType.GET
            ),
        },
        server=ServerConfig(
            logging=LoggingConfig(
                log_level="DEBUG", log_path="work/logs/test/"),
            auth=AuthConfig(
                secrets_location='/tmp',
                auth_passphrase='test',
                default_auth_methods=[AuthType.BEARER]
            ),
            api=APIConfig(
                docs_path='/api/docs'
            )
        )
    )
def mock_app_config():
    return AppConfig(
        app=AppDescriptor(name='test_app', version='test'),
        engine=AppEngineConfig(),
        env={},
        events={'test': EventDescriptor(type=EventType.GET)},
        server=ServerConfig(auth=AuthConfig(secrets_location='/tmp',
                                            auth_passphrase='test',
                                            create_keys=True)))
Beispiel #7
0
def _setup_event_context(mock_app_config: AppConfig) -> EventContext:  # noqa: F811
    _init_engine_logger(mock_app_config)
    assert mock_app_config.server
    mock_app_config.server.auth = AuthConfig(
        secrets_location=f"/tmp/{uuid.uuid4()}",
        auth_passphrase='test_passphrase',
        enabled=True,
        create_keys=True
    )
    auth.init(mock_app_config.server.auth)
    return EventContext(
        app_config=mock_app_config,
        plugin_config=mock_app_config,
        event_name='mock_event',
        track_ids={},
        auth_info={}
    )
Beispiel #8
0
def _setup_server_context(app_config: AppConfig) -> EventContext:
    _init_engine_logger(app_config)
    assert app_config.server
    app_config.server.auth = AuthConfig(
        secrets_location=f"/tmp/{uuid.uuid4()}",
        auth_passphrase='test_passphrase',
        enabled=True,
        create_keys=True
    )
    auth.init(app_config.app_key(), app_config.server.auth)
    return EventContext(
        app_config=app_config,
        plugin_config=app_config,
        event_name='mock_event',
        settings=get_event_settings(app_config.effective_settings, 'mock_event'),  # type: ignore
        track_ids={},
        auth_info={}
    )