async def test_jupyterhub_auth(tmpdir, monkeypatch): from jupyterhub.tests.utils import add_user gateway_address = "http://127.0.0.1:%d" % random_port() jhub_api_token = uuid.uuid4().hex jhub_bind_url = "http://127.0.0.1:%i/@/space%%20word/" % random_port() hub_config = Config() hub_config.JupyterHub.services = [{ "name": "dask-gateway", "url": gateway_address, "api_token": jhub_api_token }] hub_config.JupyterHub.bind_url = jhub_bind_url class MockHub(hub_mocking.MockHub): def init_logging(self): pass hub = MockHub(config=hub_config) # Configure gateway config = Config() config.DaskGateway.public_url = gateway_address + "/services/dask-gateway/" config.DaskGateway.temp_dir = str(tmpdir) config.DaskGateway.authenticator_class = ( "dask_gateway_server.auth.JupyterHubAuthenticator") config.JupyterHubAuthenticator.jupyterhub_api_token = jhub_api_token config.JupyterHubAuthenticator.jupyterhub_api_url = jhub_bind_url + "api/" async with temp_gateway(config=config) as gateway_proc: async with temp_hub(hub): # Create a new jupyterhub user alice, and get the api token u = add_user(hub.db, name="alice") api_token = u.new_api_token() hub.db.commit() # Configure auth with incorrect api token auth = JupyterHubAuth(api_token=uuid.uuid4().hex) async with Gateway( address=gateway_proc.public_urls.connect_url, proxy_address=gateway_proc.gateway_urls.connect_url, asynchronous=True, auth=auth, ) as gateway: # Auth fails with bad token with pytest.raises(Exception): await gateway.list_clusters() # Auth works with correct token auth.api_token = api_token await gateway.list_clusters()
async def test_jupyterhub_auth_user(monkeypatch): from jupyterhub.tests.utils import add_user jhub_api_token = uuid.uuid4().hex jhub_bind_url = "http://127.0.0.1:%i/@/space%%20word/" % random_port() hub_config = Config() hub_config.JupyterHub.services = [{ "name": "dask-gateway", "api_token": jhub_api_token }] hub_config.JupyterHub.bind_url = jhub_bind_url class MockHub(hub_mocking.MockHub): def init_logging(self): pass hub = MockHub(config=hub_config) # Configure gateway config = configure_dask_gateway(jhub_api_token, jhub_bind_url) async with temp_gateway(config=config) as g: async with temp_hub(hub): # Create a new jupyterhub user alice, and get the api token u = add_user(hub.db, name="alice") api_token = u.new_api_token() hub.db.commit() # Configure auth with incorrect api token auth = JupyterHubAuth(api_token=uuid.uuid4().hex) async with g.gateway_client(auth=auth) as gateway: # Auth fails with bad token with pytest.raises(Exception): await gateway.list_clusters() # Auth works with correct token auth.api_token = api_token await gateway.list_clusters()