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..c170385 100644 --- a/supertab_connect/customer/token.py +++ b/supertab_connect/customer/token.py @@ -338,6 +338,24 @@ 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: + # 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( content_blocks: list[_ContentBlock], resource_url: str, @@ -347,12 +365,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..a94c711 100644 --- a/tests/customer/test_tokens.py +++ b/tests/customer/test_tokens.py @@ -675,6 +675,64 @@ async def obtain(resource_url: str) -> str | None: assert token_calls == 2 +MULTI_PROVIDER_XML = """ + + + + + + + + +""" + + +@pytest.mark.parametrize( + ("license_xml", "base_url", "expected_url"), + [ + (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", "explicit-default-port", "malformed-unrelated-server"], +) +def test_obtain_license_token_prefers_supertab_hosted_blocks( + 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") + seen: dict[str, str] = {} + + async def handler(request: httpx.Request) -> httpx.Response: + if str(request.url).endswith("/license.xml"): + 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) + + _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: