Files
smeta-web/tests/test_external_mcp_oauth.py
T

92 lines
4.3 KiB
Python

"""OAuth grant and revocation contract for the external MCP server."""
import asyncio
from urllib.parse import parse_qs, urlparse
from pydantic import AnyUrl
from mcp.server.auth.provider import AuthorizationParams
from mcp.shared.auth import OAuthClientInformationFull
def test_approved_client_receives_user_bound_token_and_revocation(tmp_path, monkeypatch):
import db
monkeypatch.setattr(db, "DB_PATH", tmp_path / "mcp.db")
db.init_db()
db.create_user("alice@example.test", "hash")
db.admin_verify_user("alice@example.test")
from mcp_identity import InfosmetaOAuthProvider
provider = InfosmetaOAuthProvider("https://infosmeta.test", "https://infosmeta.test/mcp")
asyncio.run(provider.register_client(OAuthClientInformationFull(
client_id="client-a", client_name="Тестовый ИИ",
redirect_uris=[AnyUrl("http://localhost:9876/callback")],
token_endpoint_auth_method="none", scope="norms:read files:read")))
client = asyncio.run(provider.get_client("client-a"))
params = AuthorizationParams(state="state-a", scopes=["norms:read"],
code_challenge="a" * 43, redirect_uri=AnyUrl("http://localhost:9876/callback"),
redirect_uri_provided_explicitly=True, resource="https://infosmeta.test/mcp")
consent_url = asyncio.run(provider.authorize(client, params))
ticket = parse_qs(urlparse(consent_url).query)["ticket"][0]
callback_url = asyncio.run(provider.confirm(ticket, "alice@example.test", approved=True))
code = parse_qs(urlparse(callback_url).query)["code"][0]
grant = asyncio.run(provider.load_authorization_code(client, code))
token = asyncio.run(provider.exchange_authorization_code(client, grant))
access = asyncio.run(provider.load_access_token(token.access_token))
assert access.subject == "alice@example.test"
assert access.client_id == "client-a"
assert access.resource == "https://infosmeta.test/mcp"
assert access.scopes == ["norms:read"]
assert asyncio.run(provider.load_authorization_code(client, code)) is None
asyncio.run(provider.revoke_token(access))
assert asyncio.run(provider.load_access_token(token.access_token)) is None
def test_wrong_resource_is_rejected(tmp_path, monkeypatch):
import db
monkeypatch.setattr(db, "DB_PATH", tmp_path / "mcp.db")
db.init_db()
from mcp_identity import InfosmetaOAuthProvider
from mcp.server.auth.provider import AuthorizeError
provider = InfosmetaOAuthProvider("https://infosmeta.test", "https://infosmeta.test/mcp")
client = OAuthClientInformationFull(client_id="x", redirect_uris=[AnyUrl("http://localhost/cb")])
params = AuthorizationParams(state=None, scopes=["norms:read"],
code_challenge="a" * 43, redirect_uri=AnyUrl("http://localhost/cb"),
redirect_uri_provided_explicitly=True, resource="https://other.test/mcp")
try:
asyncio.run(provider.authorize(client, params))
except AuthorizeError as error:
assert error.error == "invalid_target"
else:
raise AssertionError("чужой resource должен быть отклонён")
def test_destructive_action_requires_browser_confirmation_once(tmp_path, monkeypatch):
import db
monkeypatch.setattr(db, "DB_PATH", tmp_path / "mcp.db")
db.init_db()
from mcp_identity import InfosmetaOAuthProvider
provider = InfosmetaOAuthProvider("https://infosmeta.test", "https://infosmeta.test/mcp")
url = asyncio.run(provider.prepare_action("alice@example.test", "client-a", "tab-a", "clear_files"))
ticket = parse_qs(urlparse(url).query)["ticket"][0]
try:
asyncio.run(provider.consume_action(ticket, "alice@example.test", "client-a", "tab-a", "clear_files"))
except PermissionError:
pass
else:
raise AssertionError("browser confirmation required")
try:
asyncio.run(provider.approve_action(ticket, "bob@example.test"))
except ValueError:
pass
else:
raise AssertionError("another user cannot confirm")
asyncio.run(provider.approve_action(ticket, "alice@example.test"))
asyncio.run(provider.consume_action(ticket, "alice@example.test", "client-a", "tab-a", "clear_files"))
try:
asyncio.run(provider.consume_action(ticket, "alice@example.test", "client-a", "tab-a", "clear_files"))
except PermissionError:
pass
else:
raise AssertionError("confirmation must be single-use")