π The fast, Pythonic way to build MCP servers and clients.
Strip a URL scheme from a Clerk domain (#5921)
Asem ahmeda9a41eb7b69d
diff --git a/fastmcp_slim/fastmcp/server/auth/providers/clerk.py b/fastmcp_slim/fastmcp/server/auth/providers/clerk.pyindex cb7a20660..1002646ef 100644--- a/fastmcp_slim/fastmcp/server/auth/providers/clerk.py+++ b/fastmcp_slim/fastmcp/server/auth/providers/clerk.py@@ -44,6 +44,15 @@ from fastmcp.utilities.logging import get_loggerlogger = get_logger(__name__)+def _normalize_domain(domain: str) -> str:+ """Strip any URL scheme and trailing slashes from a Clerk instance domain.++ The Clerk dashboard shows the Frontend API URL with its scheme, so users+ often pass it in that form.+ """+ return domain.removeprefix("https://").removeprefix("http://").rstrip("/")++class ClerkTokenVerifier(TokenVerifier):"""Token verifier for Clerk OAuth tokens.@@ -82,7 +91,7 @@ class ClerkTokenVerifier(TokenVerifier):lifecycle. When None (default), a fresh client is created per call."""super().__init__(required_scopes=required_scopes)- self.domain = domain.rstrip("/")+ self.domain = _normalize_domain(domain)self._client_id = client_idself._client_secret = client_secretself.timeout_seconds = timeout_seconds@@ -351,7 +360,7 @@ class ClerkProvider(OAuthProxy):token_expiry_threshold_seconds: Number of seconds before actual expiry totreat a token as expired, refreshing early to avoid races. Defaults to 0."""- domain = domain.rstrip("/")+ domain = _normalize_domain(domain)required_scopes_final = (parse_scopes(required_scopes)diff --git a/tests/server/auth/providers/test_clerk.py b/tests/server/auth/providers/test_clerk.pyindex 0e9d56cb5..a8dc15246 100644--- a/tests/server/auth/providers/test_clerk.py+++ b/tests/server/auth/providers/test_clerk.py@@ -14,6 +14,16 @@ CLERK_DOMAIN = "test-instance.clerk.accounts.dev"_USERINFO_RE = re.compile(rf"https://{re.escape(CLERK_DOMAIN)}/oauth/userinfo")_INTROSPECTION_RE = re.compile(rf"https://{re.escape(CLERK_DOMAIN)}/oauth/token_info")+# Forms of the same instance domain a user may copy from the Clerk dashboard,+# which shows the Frontend API URL with its scheme.+DOMAIN_FORMS = [+ CLERK_DOMAIN,+ f"{CLERK_DOMAIN}/",+ f"https://{CLERK_DOMAIN}",+ f"https://{CLERK_DOMAIN}/",+ f"http://{CLERK_DOMAIN}",+]+@pytest.fixturedef memory_storage() -> MemoryStore:@@ -76,10 +86,13 @@ class TestClerkProvider:)assert provider._upstream_revocation_endpoint is None- def test_domain_trailing_slash_stripped(self, memory_storage: MemoryStore):- """Test that trailing slashes are stripped from the domain."""+ @pytest.mark.parametrize("domain", DOMAIN_FORMS)+ def test_domain_scheme_and_trailing_slash_stripped(+ self, domain: str, memory_storage: MemoryStore+ ):+ """Test that a scheme and trailing slashes on the domain are ignored."""provider = ClerkProvider(- domain=f"{CLERK_DOMAIN}/",+ domain=domain,client_id="clerk-client-id",client_secret="clerk-client-secret",base_url="https://myserver.com",@@ -91,6 +104,12 @@ class TestClerkProvider:provider._upstream_authorization_endpoint== f"https://{CLERK_DOMAIN}/oauth/authorize")+ assert (+ provider._upstream_token_endpoint == f"https://{CLERK_DOMAIN}/oauth/token"+ )+ verifier = provider._token_validator+ assert isinstance(verifier, ClerkTokenVerifier)+ assert verifier.domain == CLERK_DOMAINdef test_default_scopes(self, memory_storage: MemoryStore):"""Test that default required scopes are openid, email, profile."""@@ -246,6 +265,27 @@ class TestClerkTokenVerifier:assert result.claims["preferred_username"] == "testuser"assert result.claims["aud"] == "clerk-client-id"+ @pytest.mark.parametrize("domain", DOMAIN_FORMS)+ async def test_domain_scheme_and_trailing_slash_stripped(+ self, domain: str, httpx_mock: HTTPXMock+ ):+ """A scheme and trailing slashes on the domain do not change request URLs."""+ httpx_mock.add_response(+ url=_INTROSPECTION_RE,+ json={"active": True, "scope": "openid", "sub": "user_abc123"},+ )+ httpx_mock.add_response(url=_USERINFO_RE, json={"sub": "user_abc123"})++ verifier = ClerkTokenVerifier(domain=domain)+ result = await verifier.verify_token("valid-token")++ assert [str(request.url) for request in httpx_mock.get_requests()] == [+ f"https://{CLERK_DOMAIN}/oauth/token_info",+ f"https://{CLERK_DOMAIN}/oauth/userinfo",+ ]+ assert result is not None+ assert verifier.domain == CLERK_DOMAIN+async def test_invalid_token_returns_none(self, httpx_mock: HTTPXMock):"""Token marked inactive by introspection is rejected."""httpx_mock.add_response(