Skip to content

Commit

Permalink
Don't propagate empty tokens on the direct-access API. (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
airwoodix authored Jun 21, 2024
1 parent 7ccb55f commit d429015
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Support direct-access mode on AQT devices (#164)
* Support up to 2000 shots per circuit on cloud resources (#165)
* Fix result timeout propagation in direct-access mode (#171)
* Don't propagate empty access tokens in direct-access mode (#172)

## qiskit-aqt-provider v1.5.0

Expand Down
5 changes: 4 additions & 1 deletion qiskit_aqt_provider/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ def http_client(*, base_url: str, token: str) -> httpx.Client:
base_url: base URL of the server
token: access token for the remote service.
"""
headers = {"Authorization": f"Bearer {token}", "User-Agent": USER_AGENT}
headers = {"User-Agent": USER_AGENT}
if token:
headers["Authorization"] = f"Bearer {token}"

return httpx.Client(headers=headers, base_url=base_url, timeout=10.0, follow_redirects=True)


Expand Down
12 changes: 10 additions & 2 deletions test/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,9 @@ def handle_result(request: httpx.Request) -> httpx.Response:
assert job.status() is JobStatus.ERROR


def test_direct_access_mocked_successful_transaction(httpx_mock: HTTPXMock) -> None:
@pytest.mark.parametrize("token", [str(uuid.uuid4()), ""])
def test_direct_access_mocked_successful_transaction(token: str, httpx_mock: HTTPXMock) -> None:
"""Mock a successful single-circuit transaction on a direct-access resource."""
token = str(uuid.uuid4())
backend = DummyDirectAccessResource(token)
backend.options.with_progress_bar = False

Expand All @@ -539,8 +539,15 @@ def test_direct_access_mocked_successful_transaction(httpx_mock: HTTPXMock) -> N

expected_job_id = str(uuid.uuid4())

def assert_valid_token(headers: httpx.Headers) -> None:
if token:
assert headers["authorization"] == f"Bearer {token}"
else:
assert "authorization" not in headers

def handle_submit(request: httpx.Request) -> httpx.Response:
assert request.headers["user-agent"] == USER_AGENT
assert_valid_token(request.headers)

data = api_models.QuantumCircuit.model_validate_json(request.content.decode("utf-8"))
assert data.repetitions == shots
Expand All @@ -552,6 +559,7 @@ def handle_submit(request: httpx.Request) -> httpx.Response:

def handle_result(request: httpx.Request) -> httpx.Response:
assert request.headers["user-agent"] == USER_AGENT
assert_valid_token(request.headers)

_, job_id = request.url.path.rsplit("/", maxsplit=1)
assert job_id == expected_job_id
Expand Down

0 comments on commit d429015

Please sign in to comment.