diff --git a/src/late/mcp/auth.py b/src/late/mcp/auth.py index 0741acad..46205afc 100644 --- a/src/late/mcp/auth.py +++ b/src/late/mcp/auth.py @@ -204,6 +204,33 @@ def _identity_in(response: httpx.Response) -> dict[str, str]: } +# A client that registered before a scope existed holds the full list of its +# day, which meant "everything". New scopes are only ever appended to +# OAUTH_SCOPES, so every earlier full list is a prefix of it, starting from the +# original 8. Port of expandLegacyFullScope (Schedule-Posts-API +# libs/oauth/scopes.ts): keep the two in step. +_ORIGINAL_SCOPE_COUNT = 8 + + +def expand_legacy_full_scope(scope: str) -> list[str]: + """The scopes an OAuth `scope` string grants, as the Zernio API reads it.""" + requested = list(dict.fromkeys(scope.split())) + granted = set(requested) + size = len(granted) + is_earlier_full_list = _ORIGINAL_SCOPE_COUNT <= size < len(OAUTH_SCOPES) and ( + granted.issuperset(OAUTH_SCOPES[:size]) + ) + return list(OAUTH_SCOPES) if is_earlier_full_list else requested + + +def _scopes_for(claims: dict[str, str | int]) -> list[str]: + """An OAuth token's granted scopes; every scope for an API key.""" + scope = claims.get("scope") + if isinstance(scope, str): + return expand_legacy_full_scope(scope) + return list(OAUTH_SCOPES) + + def forget_rejected_token(token: str) -> None: _forget(token_cache_key(token)) @@ -337,7 +364,7 @@ def _grant( return AccessToken( token=token, client_id="zernio", - scopes=list(OAUTH_SCOPES), + scopes=_scopes_for(claims), claims=claims, ) diff --git a/tests/test_mcp_auth_cache.py b/tests/test_mcp_auth_cache.py index e600ea99..cea164fc 100644 --- a/tests/test_mcp_auth_cache.py +++ b/tests/test_mcp_auth_cache.py @@ -298,3 +298,85 @@ async def test_an_oauth_token_past_the_outage_grace_is_not_granted(): with pytest.raises(AuthenticationError): await verifier.verify_token("late_at_live") + + +def _verifier_granting_scope(scope: str | None) -> ZernioTokenVerifier: + return _verifier_answering( + httpx.Response(200, json={"userId": "u" * 24, "scope": scope}) + ) + + +async def test_an_oauth_token_with_a_narrow_scope_carries_exactly_those_scopes(): + verifier = _verifier_granting_scope("posts:read accounts:read") + + granted = await verifier.verify_token("late_at_live") + + assert granted is not None + assert granted.scopes == ["posts:read", "accounts:read"] + + +@pytest.mark.parametrize("count", [8, 13, len(auth.OAUTH_SCOPES)]) +async def test_a_full_scope_list_from_any_earlier_day_carries_every_current_scope( + count, +): + verifier = _verifier_granting_scope(" ".join(auth.OAUTH_SCOPES[:count])) + + granted = await verifier.verify_token("late_at_live") + + assert granted is not None + assert granted.scopes == list(auth.OAUTH_SCOPES) + + +async def test_eight_scopes_that_are_not_the_original_eight_stay_narrow(): + narrow = auth.OAUTH_SCOPES[1:9] + verifier = _verifier_granting_scope(" ".join(narrow)) + + granted = await verifier.verify_token("late_at_live") + + assert granted is not None + assert granted.scopes == narrow + + +async def test_an_api_key_carries_every_scope(): + verifier = _verifier_granting_scope(None) + + granted = await verifier.verify_token("sk_live") + + assert granted is not None + assert granted.scopes == list(auth.OAUTH_SCOPES) + + +async def test_a_grant_served_from_cache_carries_the_cached_scope(): + verifier = _verifier_granting_scope("posts:read") + await verifier.verify_token("late_at_live") + + cached = await verifier.verify_token("late_at_live") + + assert cached is not None + assert cached.claims["resolved_by"] == "cache" + assert cached.scopes == ["posts:read"] + + +async def test_a_grant_during_an_outage_carries_the_cached_scope(): + verifier, _ = _verifier_counting(httpx.Response(503)) + _verified_seconds_ago("late_at_live", 1800) + auth._remember_identity( + auth.token_cache_key("late_at_live"), {"scope": "messaging:read"} + ) + + granted = await verifier.verify_token("late_at_live") + + assert granted is not None + assert granted.claims["resolved_by"] == "grace" + assert granted.scopes == ["messaging:read"] + + +async def test_an_api_key_during_an_outage_carries_every_scope(): + verifier, _ = _verifier_counting(httpx.Response(503)) + _verified_seconds_ago("sk_live", 1800) + + granted = await verifier.verify_token("sk_live") + + assert granted is not None + assert granted.claims["resolved_by"] == "grace" + assert granted.scopes == list(auth.OAUTH_SCOPES)