From 1bb6074fe7d60a64691eab71b713200cc80b71d9 Mon Sep 17 00:00:00 2001 From: Filipa Andrade Date: Fri, 16 Oct 2026 15:22:15 +0100 Subject: [PATCH 1/2] Prefer Supertab-hosted content blocks when minting --- CHANGELOG.md | 4 +++ supertab_connect/customer/token.py | 19 +++++++++--- tests/customer/test_tokens.py | 48 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f134cc0..ca39649 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 block's own `{server}/token` with the `` chunk attached. - **`resource` carries the raw resource URL** on both lanes, no longer the matched block's `url_pattern`. +- **Supertab-hosted blocks win.** When several `` blocks match, one whose `server` + is on the `supertab_base_url` host is chosen over a more specific match from another + provider, so client credentials never go to a third party when a Supertab block also + matches. Matches the TypeScript and PHP SDKs. ## [1.0.0] — 2026-07-15 diff --git a/supertab_connect/customer/token.py b/supertab_connect/customer/token.py index 8e09056..a2ad4b1 100644 --- a/supertab_connect/customer/token.py +++ b/supertab_connect/customer/token.py @@ -338,6 +338,10 @@ class _TokenEndpoint: license_xml: str | None = None +def _is_supertab_server(server: str, supertab_base_url: str) -> bool: + return urllib.parse.urlsplit(server).netloc.lower() == urllib.parse.urlsplit(supertab_base_url).netloc.lower() + + def _select_token_endpoint( content_blocks: list[_ContentBlock], resource_url: str, @@ -347,12 +351,19 @@ def _select_token_endpoint( """Resolve where to mint, decoupled from whether the live license.xml still grants the resource. A path-matching ```` block gives the RSL License lane: mint against that block's - URN-scoped server and send its ```` chunk. Anything else falls through to the - Agreement lane, where the backend resolves the merchant system from the resource URL and the - customer's single Active Agreement, so a diverged license.xml cannot veto the mint. + URN-scoped server and send its ```` chunk. Matches on the Supertab API host win over + other providers, so credentials never go to a third party when a Supertab block also matches. + Anything else falls through to the Agreement lane, where the backend resolves the merchant + system from the resource URL and the customer's single Active Agreement, so a diverged + license.xml cannot veto the mint. """ token_content_blocks = [block for block in content_blocks if block.server] - matched = _find_best_matching_content(token_content_blocks, resource_url, debug) + supertab_blocks = [ + b for b in token_content_blocks if b.server and _is_supertab_server(b.server, supertab_base_url) + ] + matched = _find_best_matching_content(supertab_blocks, resource_url, debug) or _find_best_matching_content( + token_content_blocks, resource_url, debug + ) if matched is not None and matched.server is not None: debug_log(debug, f"Matched content block for resource URL: {resource_url}") return _TokenEndpoint( diff --git a/tests/customer/test_tokens.py b/tests/customer/test_tokens.py index 55c9e35..beb0349 100644 --- a/tests/customer/test_tokens.py +++ b/tests/customer/test_tokens.py @@ -675,6 +675,54 @@ async def obtain(resource_url: str) -> str | None: assert token_calls == 2 +MULTI_PROVIDER_XML = """ + + + + + + + + +""" + + +@pytest.mark.parametrize( + ("base_url", "expected_url"), + [ + ("http://api-connect.test", "http://api-connect.test/merchant/token"), + ("http://other-api.test", "http://third-party.test/issuer/token"), + ], + ids=["supertab-block-wins", "falls-back-to-best-match"], +) +def test_obtain_license_token_prefers_supertab_hosted_blocks( + monkeypatch: pytest.MonkeyPatch, base_url: str, expected_url: str +) -> None: + """A Supertab-hosted match beats a more specific third-party one; without one, the best match wins.""" + access_token = jwt.encode({"exp": int(time.time()) + 3600}, "x" * 32, algorithm="HS256") + seen: dict[str, str] = {} + + async def handler(request: httpx.Request) -> httpx.Response: + if str(request.url).endswith("/license.xml"): + return httpx.Response(200, text=MULTI_PROVIDER_XML, request=request) + + seen["url"] = str(request.url) + return httpx.Response(200, json={"access_token": access_token}, request=request) + + _install_mock_transport(monkeypatch, handler) + + asyncio.run( + obtain_license_token( + client_id="client", + client_secret="secret", + resource_url="http://127.0.0.1:7676/news/premium/story", + supertab_base_url=base_url, + ) + ) + + assert seen["url"] == expected_url + + def test_obtain_license_token_raises_on_token_endpoint_failure( monkeypatch: pytest.MonkeyPatch, ) -> None: From 4e90ec277a7d20d0b8983064db9fa05235e0ddcb Mon Sep 17 00:00:00 2001 From: Filipa Andrade Date: Fri, 16 Oct 2026 15:52:15 +0100 Subject: [PATCH 2/2] Normalize default ports and skip malformed servers in the Supertab host check --- supertab_connect/customer/token.py | 16 +++++++++++++++- tests/customer/test_tokens.py | 22 ++++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/supertab_connect/customer/token.py b/supertab_connect/customer/token.py index a2ad4b1..c170385 100644 --- a/supertab_connect/customer/token.py +++ b/supertab_connect/customer/token.py @@ -338,8 +338,22 @@ class _TokenEndpoint: license_xml: str | None = None +_DEFAULT_PORTS = {"http": 80, "https": 443} + + +def _host_and_port(url: str) -> tuple[str, int | None] | None: + try: + parts = urllib.parse.urlsplit(url) + port = parts.port or _DEFAULT_PORTS.get(parts.scheme.lower()) + except ValueError: + return None + return (parts.hostname, port) if parts.hostname else None + + def _is_supertab_server(server: str, supertab_base_url: str) -> bool: - return urllib.parse.urlsplit(server).netloc.lower() == urllib.parse.urlsplit(supertab_base_url).netloc.lower() + # Default ports normalized like the TS SDK's URL.host; an unparseable server is never Supertab. + host = _host_and_port(server) + return host is not None and host == _host_and_port(supertab_base_url) def _select_token_endpoint( diff --git a/tests/customer/test_tokens.py b/tests/customer/test_tokens.py index beb0349..a94c711 100644 --- a/tests/customer/test_tokens.py +++ b/tests/customer/test_tokens.py @@ -688,15 +688,25 @@ async def obtain(resource_url: str) -> str | None: @pytest.mark.parametrize( - ("base_url", "expected_url"), + ("license_xml", "base_url", "expected_url"), [ - ("http://api-connect.test", "http://api-connect.test/merchant/token"), - ("http://other-api.test", "http://third-party.test/issuer/token"), + (MULTI_PROVIDER_XML, "http://api-connect.test", "http://api-connect.test/merchant/token"), + (MULTI_PROVIDER_XML, "http://other-api.test", "http://third-party.test/issuer/token"), + ( + MULTI_PROVIDER_XML.replace("http://api-connect.test/merchant", "http://API-Connect.test:80/merchant"), + "http://api-connect.test", + "http://api-connect.test/merchant/token", + ), + ( + MULTI_PROVIDER_XML.replace("http://third-party.test/issuer", "http://[broken"), + "http://api-connect.test", + "http://api-connect.test/merchant/token", + ), ], - ids=["supertab-block-wins", "falls-back-to-best-match"], + ids=["supertab-block-wins", "falls-back-to-best-match", "explicit-default-port", "malformed-unrelated-server"], ) def test_obtain_license_token_prefers_supertab_hosted_blocks( - monkeypatch: pytest.MonkeyPatch, base_url: str, expected_url: str + monkeypatch: pytest.MonkeyPatch, license_xml: str, base_url: str, expected_url: str ) -> None: """A Supertab-hosted match beats a more specific third-party one; without one, the best match wins.""" access_token = jwt.encode({"exp": int(time.time()) + 3600}, "x" * 32, algorithm="HS256") @@ -704,7 +714,7 @@ def test_obtain_license_token_prefers_supertab_hosted_blocks( async def handler(request: httpx.Request) -> httpx.Response: if str(request.url).endswith("/license.xml"): - return httpx.Response(200, text=MULTI_PROVIDER_XML, request=request) + return httpx.Response(200, text=license_xml, request=request) seen["url"] = str(request.url) return httpx.Response(200, json={"access_token": access_token}, request=request)