Fix: get_resource_url nested URL handling (#1914)

This commit is contained in:
raphael-linx 2025-09-25 17:35:37 +03:00 committed by GitHub
commit eb0db4fb77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 2 deletions

View file

@ -146,8 +146,7 @@ class AuthProvider(TokenVerifierProtocol):
return None
if path:
return AnyHttpUrl(urljoin(str(self.base_url), path))
return AnyHttpUrl(f"{str(self.base_url).rstrip('/')}/{path.lstrip('/')}")
return self.base_url

View file

@ -105,6 +105,28 @@ class TestRemoteAuthProvider:
"https://api.example.com/.well-known/oauth-protected-resource"
)
def test_get_resource_url_with_nested_base_url(self):
"""Test _get_resource_url returns correct URL for .well-known path with nested base_url."""
tokens = {
"test_token": {
"client_id": "test-client",
"scopes": ["read"],
}
}
token_verifier = StaticTokenVerifier(tokens=tokens)
provider = RemoteAuthProvider(
token_verifier=token_verifier,
authorization_servers=[AnyHttpUrl("https://auth.example.com")],
base_url="https://api.example.com/v1/",
)
metadata_url = provider._get_resource_url(
"/.well-known/oauth-protected-resource"
)
assert metadata_url == AnyHttpUrl(
"https://api.example.com/v1/.well-known/oauth-protected-resource"
)
def test_get_resource_url_handles_trailing_slash(self):
"""Test _get_resource_url handles trailing slash correctly."""
tokens = {
@ -216,6 +238,8 @@ class TestRemoteAuthProviderIntegration:
[
("https://api.example.com", "https://api.example.com/mcp"),
("https://api.example.com/", "https://api.example.com/mcp"),
("https://api.example.com/v1/", "https://api.example.com/v1/mcp"),
],
)
async def test_base_url_configurations(self, base_url: str, expected_resource: str):