Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<license>` 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 `<content>` 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

Expand Down
33 changes: 29 additions & 4 deletions supertab_connect/customer/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ``<content>`` block gives the RSL License lane: mint against that block's
URN-scoped server and send its ``<license>`` 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 ``<license>`` 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)
Comment thread
filias marked this conversation as resolved.
Resolved
]
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(
Expand Down
58 changes: 58 additions & 0 deletions tests/customer/test_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,64 @@ async def obtain(resource_url: str) -> str | None:
assert token_calls == 2


MULTI_PROVIDER_XML = """
<rsl xmlns="https://rslstandard.org/rsl">
<content url="/news/premium/*" server="http://third-party.test/issuer">
<license type="test"><link rel="self" /></license>
</content>
<content url="/news/*" server="http://api-connect.test/merchant">
<license type="test"><link rel="self" /></license>
</content>
</rsl>
"""


@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:
Expand Down
Loading