mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-30 11:12:08 +02:00
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Jeremiah Lowin <jlowin@users.noreply.github.com> Co-authored-by: Marvin Context Protocol <41898282+Marvin Context Protocol@users.noreply.github.com> Co-authored-by: voidborne-d <voidborne-d@users.noreply.github.com> Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: d 🔹 <258577966+voidborne-d@users.noreply.github.com> Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Co-authored-by: nightcityblade <nightcityblade@gmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Bill Easton <strawgate@users.noreply.github.com> Co-authored-by: Sumanshu Nankana <sumanshunankana@gmail.com> Co-authored-by: Eric Robinson <ericrobinson@indeed.com> Co-authored-by: Martim Santos <martimfasantos@gmail.com> Co-authored-by: d 🔹 <liusway405@gmail.com> Co-authored-by: Matthieu B <66959271+mtthidoteu@users.noreply.github.com> Co-authored-by: Sascha Buehrle <47737812+saschabuehrle@users.noreply.github.com> Co-authored-by: Hakancan <142545736+hkc5@users.noreply.github.com> Co-authored-by: nightcityblade <jackchen@haloailabs.com> Co-authored-by: Matt Hallowell <17804673+mhallo@users.noreply.github.com> Co-authored-by: nate nowack <thrast36@gmail.com> Co-authored-by: Bill Easton <williamseaston@gmail.com> Co-authored-by: Marcus Shu <46469249+shulkx@users.noreply.github.com> Co-authored-by: Rushabh Doshi <radoshi@gmail.com> Co-authored-by: AIKAWA Shigechika <shige@aikawa.jp> Co-authored-by: Jeremy Simon <simonjer805@gmail.com> Co-authored-by: Miguel Miranda Dias <7780875+pandego@users.noreply.github.com> Co-authored-by: Anthony James Padavano <padavano.anthony@gmail.com> Co-authored-by: Mostafa Kamal <hiremostafa@gmail.com> Fix auto-close MRE script posting comment without closing (#3386) Fix WorkOS token scope verification bypass 🤖 Generated with Codex (#3407) Fix initialize McpError fallthrough 🤖 Generated with Codex (#3413) Fix transform arg collisions with passthrough params (#3431) Fix get_* returning None when latest version is disabled (#3439) Fix get_* returning None when latest version is disabled (#3421) Fix server lifespan overlap teardown (#3415) Fix $ref output schema object detection regression (#3420) resolved annotations (#3429) Fix async partial callables rejected by iscoroutinefunction (#3438) Fix async partial callables rejected by iscoroutinefunction (#3423) fix: add version to components (#3458) fix: use intent-based flag for OIDC scope patch in load_access_token (#3465) Fixes #3461 fix: normalize Google scope shorthands and surface valid_scopes (#3477) fix: resolve ty 0.0.23 type-checking errors and bump pin (#3481) fix: shield lifespan teardown from cancellation (#3480) fix: forward custom_route endpoints from mounted servers (#3462) fix updates _get_additional_http_routes() to traverse providers, Fixes #3457 fix: remove hardcoded version from CLI help text (#3456) fix: monty 0.0.8 compatibility, drop external_functions from constructor (#3468) fix: task test teardown hanging 5s per test (#3499) Closes #3498 fix: validate workspace path is a directory before cursor install (#3440) Fixes #3426 fix: handle re.error from malformed URI templates in build_regex (#3501) fix: reject empty/OIDC-only required_scopes in AzureProvider (#3503) fix: restrict $ref resolution to local refs only (SSRF/LFI) (#3502) fix warnings and timeouts (#3504) close upgrade check issue when build passes (#3505) Closes #3484 fix: URL-encode path params to prevent SSRF/path traversal (GHSA-vv7q-7jx5-f767) (#3507) fix: prevent path traversal in skill download (#3493) fix: prefer IdP-granted scopes over client-requested scopes in OAuthProxy (#3492) fix: remove unrelated transform and http.py changes from PR scope fix: remove forced follow_redirects from httpx_client_factory calls (#3496) fix: stop passing follow_redirects to httpx_client_factory fix: restore follow_redirects=True for custom httpx client factories Closes #3509 fix: CSRF double-submit cookie check in consent flow (#3519) fix: validate server names in install commands (#3522) fix: use raw strings for regex in pytest.raises match (#3523) fix: reject refresh tokens used as Bearer access tokens (#3524) fix: route ResourcesAsTools/PromptsAsTools through server middleware (#3495) fix: resolve Pyright "Module is not callable" on @tool, @resource, @prompt decorators (#3540) fix: filter warnings by message in KEY_PREFIX test (#3549) fix: suppress output schema for ToolResult subclass annotations (#3548) fix: increase sleep duration in proxy cache tests (#3567) fix: store absolute token expiry to prevent stale expires_in on reload (#3572) fix: preserve tool properties named 'title' during schema compression (#3582) Fix loopback redirect URI port matching per RFC 8252 §7.3 (#3589) Fix app tool routing: visibility check and middleware propagation (#3591) Fix query parameter serialization to respect OpenAPI explode/style settings (#3595) Fix dev apps form: union types, textarea support, JSON parsing (#3597) fix(google): replace deprecated /oauth2/v1/tokeninfo with /oauth2/v3/userinfo (#3603) fix: resolve EntraOBOToken dependency injection through MultiAuth (#3609) fix(docs): correct misleading stateless_http header (#3622) fix: filesystem provider import machinery (#3626) Closes #3625 (issues 2, 3, 6) fix: recover StdioTransport after subprocess exits (#3630) fix(server): preserve mounted tool task metadata (#3632) fix: scope deprecation warning filter to FastMCPDeprecationWarning (#3649) fix imports, add PrefabAppConfig (#3650) fix: resolve CurrentFastMCP/ctx.fastmcp to child server in mounted background tasks (#3651) Fix blocking docs issues: chart imports, Select API, Rx consistency (#3652) closed by default (#3657) Fix prompt caching middleware missing wrap/unwrap round-trip (#3666) fix: serialize object query params per OpenAPI style/explode rules (#3662) Fixes #2857 fix: HTTP request headers not accessible in background task workers (#3631) fix: restore HTTP headers in worker execution path for background tasks (#3681) fix: strip discriminator after dereferencing schemas (#3682) fix: remove stale ty:ignore directives for ty 0.0.26 (#3684) Fix docs gaps in app provider pages (#3690) fix: dev apps log panel UX improvements (#3698) fix dev server empty string args (#3700)
768 lines
30 KiB
Python
768 lines
30 KiB
Python
"""Tests for Azure provider scope handling, JWT verifier, and OBO integration."""
|
|
|
|
import pytest
|
|
from key_value.aio.stores.memory import MemoryStore
|
|
|
|
from fastmcp.server.auth.auth import MultiAuth
|
|
from fastmcp.server.auth.providers.azure import (
|
|
OIDC_SCOPES,
|
|
AzureJWTVerifier,
|
|
AzureProvider,
|
|
_find_azure_provider,
|
|
)
|
|
from fastmcp.server.auth.providers.jwt import RSAKeyPair, StaticTokenVerifier
|
|
|
|
|
|
@pytest.fixture
|
|
def memory_storage() -> MemoryStore:
|
|
"""Provide a MemoryStore for tests to avoid SQLite initialization on Windows."""
|
|
return MemoryStore()
|
|
|
|
|
|
class TestOIDCScopeHandling:
|
|
"""Tests for OIDC scope handling in Azure provider.
|
|
|
|
Azure access tokens do NOT include OIDC scopes (openid, profile, email,
|
|
offline_access) in the `scp` claim - they're only used during authorization.
|
|
These tests verify that:
|
|
1. OIDC scopes are never prefixed with identifier_uri
|
|
2. OIDC scopes are filtered from token validation
|
|
3. OIDC scopes are still advertised to clients via valid_scopes
|
|
"""
|
|
|
|
def test_oidc_scopes_constant(self, memory_storage: MemoryStore):
|
|
"""Verify OIDC_SCOPES contains the standard OIDC scopes."""
|
|
assert OIDC_SCOPES == {"openid", "profile", "email", "offline_access"}
|
|
|
|
def test_prefix_scopes_does_not_prefix_oidc_scopes(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that _prefix_scopes_for_azure never prefixes OIDC scopes."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
# All OIDC scopes should pass through unchanged
|
|
result = provider._prefix_scopes_for_azure(
|
|
["openid", "profile", "email", "offline_access"]
|
|
)
|
|
|
|
assert result == ["openid", "profile", "email", "offline_access"]
|
|
|
|
def test_prefix_scopes_mixed_oidc_and_custom(self, memory_storage: MemoryStore):
|
|
"""Test prefixing with a mix of OIDC and custom scopes."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
result = provider._prefix_scopes_for_azure(
|
|
["read", "openid", "write", "profile"]
|
|
)
|
|
|
|
# Custom scopes should be prefixed, OIDC scopes should not
|
|
assert "api://my-api/read" in result
|
|
assert "api://my-api/write" in result
|
|
assert "openid" in result
|
|
assert "profile" in result
|
|
# Verify OIDC scopes are NOT prefixed
|
|
assert "api://my-api/openid" not in result
|
|
assert "api://my-api/profile" not in result
|
|
|
|
def test_prefix_scopes_dot_notation_gets_prefixed(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that dot-notation scopes get prefixed (use additional_authorize_scopes for Graph)."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
# Dot-notation scopes ARE prefixed - use additional_authorize_scopes for Graph
|
|
# or fully-qualified format like https://graph.microsoft.com/User.Read
|
|
result = provider._prefix_scopes_for_azure(["my.scope", "admin.read"])
|
|
|
|
assert result == ["api://my-api/my.scope", "api://my-api/admin.read"]
|
|
|
|
def test_prefix_scopes_fully_qualified_graph_not_prefixed(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that fully-qualified Graph scopes are not prefixed."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
result = provider._prefix_scopes_for_azure(
|
|
[
|
|
"https://graph.microsoft.com/User.Read",
|
|
"https://graph.microsoft.com/Mail.Send",
|
|
]
|
|
)
|
|
|
|
# Fully-qualified URIs pass through unchanged
|
|
assert result == [
|
|
"https://graph.microsoft.com/User.Read",
|
|
"https://graph.microsoft.com/Mail.Send",
|
|
]
|
|
|
|
def test_required_scopes_with_oidc_filters_validation(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that OIDC scopes in required_scopes are filtered from token validation."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read", "openid", "profile"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
# Token validator should only require non-OIDC scopes
|
|
assert provider._token_validator.required_scopes == ["read"]
|
|
|
|
def test_required_scopes_all_oidc_raises_value_error(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that providing only OIDC scopes raises ValueError."""
|
|
with pytest.raises(ValueError, match="at least one non-OIDC scope"):
|
|
AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["openid", "profile"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
def test_empty_required_scopes_raises_value_error(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that providing empty required_scopes raises ValueError."""
|
|
with pytest.raises(ValueError, match="at least one non-OIDC scope"):
|
|
AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=[],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
"scopes",
|
|
[
|
|
["offline_access"],
|
|
["openid", "email", "profile", "offline_access"],
|
|
["email"],
|
|
],
|
|
)
|
|
def test_only_oidc_scopes_raises_value_error(
|
|
self, memory_storage: MemoryStore, scopes: list[str]
|
|
):
|
|
"""Test that various OIDC-only scope combinations raise ValueError."""
|
|
with pytest.raises(ValueError, match="at least one non-OIDC scope"):
|
|
AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
required_scopes=scopes,
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
def test_valid_scopes_includes_oidc_scopes(self, memory_storage: MemoryStore):
|
|
"""Test that valid_scopes advertises OIDC scopes to clients."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read", "openid", "profile"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
# required_scopes (used for validation) excludes OIDC scopes
|
|
assert provider.required_scopes == ["read"]
|
|
# But valid_scopes (advertised to clients) includes all scopes
|
|
assert provider.client_registration_options is not None
|
|
assert provider.client_registration_options.valid_scopes == [
|
|
"read",
|
|
"openid",
|
|
"profile",
|
|
]
|
|
|
|
def test_prepare_scopes_for_refresh_handles_oidc_scopes(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that token refresh correctly handles OIDC scopes."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
# Simulate stored scopes that include OIDC scopes
|
|
result = provider._prepare_scopes_for_upstream_refresh(
|
|
["read", "openid", "profile"]
|
|
)
|
|
|
|
# Custom scope should be prefixed, OIDC scopes should not
|
|
assert "api://my-api/read" in result
|
|
assert "openid" in result
|
|
assert "profile" in result
|
|
assert "api://my-api/openid" not in result
|
|
assert "api://my-api/profile" not in result
|
|
|
|
|
|
class TestAzureTokenExchangeScopes:
|
|
"""Tests for Azure provider's token exchange scope handling.
|
|
|
|
Azure requires scopes to be sent during the authorization code exchange.
|
|
The provider overrides _prepare_scopes_for_token_exchange to return
|
|
properly prefixed scopes.
|
|
"""
|
|
|
|
def test_prepare_scopes_returns_prefixed_scopes(self, memory_storage: MemoryStore):
|
|
"""Test that _prepare_scopes_for_token_exchange returns prefixed scopes."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read", "write"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
scopes = provider._prepare_scopes_for_token_exchange(["read", "write"])
|
|
assert len(scopes) > 0
|
|
assert "api://my-api/read" in scopes
|
|
assert "api://my-api/write" in scopes
|
|
|
|
def test_prepare_scopes_includes_additional_oidc_scopes(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that _prepare_scopes_for_token_exchange includes OIDC scopes."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read"],
|
|
additional_authorize_scopes=["openid", "profile", "offline_access"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
scopes = provider._prepare_scopes_for_token_exchange(["read"])
|
|
assert len(scopes) > 0
|
|
assert "api://my-api/read" in scopes
|
|
assert "openid" in scopes
|
|
assert "profile" in scopes
|
|
assert "offline_access" in scopes
|
|
|
|
def test_prepare_scopes_excludes_other_api_scopes(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test token exchange excludes other API scopes (Azure AADSTS28000).
|
|
|
|
Azure only allows ONE resource per token exchange. Other API scopes
|
|
are requested during authorization but excluded from token exchange.
|
|
"""
|
|
provider = AzureProvider(
|
|
client_id="00000000-1111-2222-3333-444444444444",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["user_impersonation"],
|
|
additional_authorize_scopes=[
|
|
"openid",
|
|
"profile",
|
|
"offline_access",
|
|
"api://aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/user_impersonation",
|
|
"api://11111111-2222-3333-4444-555555555555/user_impersonation",
|
|
],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
scopes = provider._prepare_scopes_for_token_exchange(["user_impersonation"])
|
|
assert len(scopes) > 0
|
|
# Primary API scope should be prefixed with the provider's identifier_uri
|
|
assert "api://00000000-1111-2222-3333-444444444444/user_impersonation" in scopes
|
|
# OIDC scopes should be included
|
|
assert "openid" in scopes
|
|
assert "profile" in scopes
|
|
assert "offline_access" in scopes
|
|
# Other API scopes should NOT be included (Azure multi-resource limitation)
|
|
assert not any("api://aaaaaaaa" in s for s in scopes)
|
|
assert not any("api://11111111" in s for s in scopes)
|
|
|
|
def test_prepare_scopes_deduplicates_scopes(self, memory_storage: MemoryStore):
|
|
"""Test that duplicate scopes are deduplicated."""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read"],
|
|
additional_authorize_scopes=["api://my-api/read", "openid"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
# Pass a scope that will be prefixed to match one in additional_authorize_scopes
|
|
scopes = provider._prepare_scopes_for_token_exchange(["read"])
|
|
assert len(scopes) > 0
|
|
# Should be deduplicated - api://my-api/read appears only once
|
|
assert scopes.count("api://my-api/read") == 1
|
|
assert "openid" in scopes
|
|
|
|
def test_extra_token_params_does_not_contain_scope(
|
|
self, memory_storage: MemoryStore
|
|
):
|
|
"""Test that extra_token_params doesn't contain scope to avoid TypeError.
|
|
|
|
Previously, Azure provider set extra_token_params={"scope": ...} during init.
|
|
This caused a TypeError in exchange_refresh_token because it passes both
|
|
scope=... AND **self._extra_token_params, resulting in:
|
|
"got multiple values for keyword argument 'scope'"
|
|
|
|
The fix uses the _prepare_scopes_for_token_exchange hook instead.
|
|
"""
|
|
provider = AzureProvider(
|
|
client_id="test_client",
|
|
client_secret="test_secret",
|
|
tenant_id="test-tenant",
|
|
base_url="https://myserver.com",
|
|
identifier_uri="api://my-api",
|
|
required_scopes=["read", "write"],
|
|
additional_authorize_scopes=["openid", "profile", "offline_access"],
|
|
jwt_signing_key="test-secret",
|
|
client_storage=memory_storage,
|
|
)
|
|
|
|
# extra_token_params should NOT contain "scope" to avoid TypeError during refresh
|
|
assert "scope" not in provider._extra_token_params
|
|
|
|
# Instead, scopes should be provided via the hook methods
|
|
exchange_scopes = provider._prepare_scopes_for_token_exchange(["read", "write"])
|
|
assert len(exchange_scopes) > 0
|
|
|
|
refresh_scopes = provider._prepare_scopes_for_upstream_refresh(
|
|
["read", "write"]
|
|
)
|
|
assert len(refresh_scopes) > 0
|
|
|
|
|
|
class TestAzureJWTVerifier:
|
|
"""Tests for AzureJWTVerifier pre-configured JWT verifier."""
|
|
|
|
def test_auto_configures_from_client_and_tenant(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["access_as_user"],
|
|
)
|
|
assert (
|
|
verifier.jwks_uri
|
|
== "https://login.microsoftonline.com/my-tenant-id/discovery/v2.0/keys"
|
|
)
|
|
assert verifier.issuer == "https://login.microsoftonline.com/my-tenant-id/v2.0"
|
|
assert verifier.audience == "my-client-id"
|
|
assert verifier.algorithm == "RS256"
|
|
assert verifier.required_scopes == ["access_as_user"]
|
|
|
|
async def test_validates_short_form_scopes(self):
|
|
key_pair = RSAKeyPair.generate()
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["access_as_user"],
|
|
)
|
|
# Override to use our test key instead of JWKS
|
|
verifier.public_key = key_pair.public_key
|
|
verifier.jwks_uri = None
|
|
|
|
token = key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://login.microsoftonline.com/my-tenant-id/v2.0",
|
|
audience="my-client-id",
|
|
additional_claims={"scp": "access_as_user"},
|
|
)
|
|
result = await verifier.load_access_token(token)
|
|
assert result is not None
|
|
assert "access_as_user" in result.scopes
|
|
|
|
def test_scopes_supported_returns_prefixed_form(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["read", "write"],
|
|
)
|
|
assert verifier.scopes_supported == [
|
|
"api://my-client-id/read",
|
|
"api://my-client-id/write",
|
|
]
|
|
|
|
def test_already_prefixed_scopes_pass_through(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["api://my-client-id/read"],
|
|
)
|
|
assert verifier.scopes_supported == ["api://my-client-id/read"]
|
|
|
|
def test_oidc_scopes_not_prefixed(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["openid", "read"],
|
|
)
|
|
assert verifier.scopes_supported == ["openid", "api://my-client-id/read"]
|
|
|
|
def test_custom_identifier_uri(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["read"],
|
|
identifier_uri="api://custom-uri",
|
|
)
|
|
assert verifier.scopes_supported == ["api://custom-uri/read"]
|
|
|
|
def test_custom_base_authority_for_gov_cloud(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["read"],
|
|
base_authority="login.microsoftonline.us",
|
|
)
|
|
assert (
|
|
verifier.jwks_uri
|
|
== "https://login.microsoftonline.us/my-tenant-id/discovery/v2.0/keys"
|
|
)
|
|
assert verifier.issuer == "https://login.microsoftonline.us/my-tenant-id/v2.0"
|
|
|
|
def test_scopes_supported_empty_when_no_required_scopes(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="my-tenant-id",
|
|
)
|
|
assert verifier.scopes_supported == []
|
|
|
|
def test_default_identifier_uri_uses_client_id(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="abc-123",
|
|
tenant_id="my-tenant-id",
|
|
required_scopes=["read"],
|
|
)
|
|
assert verifier.scopes_supported == ["api://abc-123/read"]
|
|
|
|
def test_multi_tenant_organizations_skips_issuer(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="organizations",
|
|
)
|
|
assert verifier.issuer is None
|
|
|
|
def test_multi_tenant_consumers_skips_issuer(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="consumers",
|
|
)
|
|
assert verifier.issuer is None
|
|
|
|
def test_multi_tenant_common_skips_issuer(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="common",
|
|
)
|
|
assert verifier.issuer is None
|
|
|
|
def test_specific_tenant_sets_issuer(self):
|
|
verifier = AzureJWTVerifier(
|
|
client_id="my-client-id",
|
|
tenant_id="12345678-1234-1234-1234-123456789012",
|
|
)
|
|
assert (
|
|
verifier.issuer
|
|
== "https://login.microsoftonline.com/12345678-1234-1234-1234-123456789012/v2.0"
|
|
)
|
|
|
|
|
|
class TestAzureOBOIntegration:
|
|
"""Tests for azure.identity OBO integration (get_obo_credential, EntraOBOToken)."""
|
|
|
|
async def test_get_obo_credential_returns_configured_credential(self):
|
|
"""Test that get_obo_credential returns a properly configured credential."""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
provider = AzureProvider(
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
tenant_id="test-tenant-id",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
)
|
|
|
|
mock_credential = MagicMock()
|
|
with patch(
|
|
"azure.identity.aio.OnBehalfOfCredential", return_value=mock_credential
|
|
) as mock_class:
|
|
credential = await provider.get_obo_credential(
|
|
user_assertion="user-token-123"
|
|
)
|
|
|
|
mock_class.assert_called_once_with(
|
|
tenant_id="test-tenant-id",
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
user_assertion="user-token-123",
|
|
authority="https://login.microsoftonline.com",
|
|
)
|
|
assert credential is mock_credential
|
|
|
|
async def test_get_obo_credential_caches_by_assertion(self):
|
|
"""Test that the same assertion returns the cached credential."""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
provider = AzureProvider(
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
tenant_id="test-tenant-id",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
)
|
|
|
|
mock_credential = MagicMock()
|
|
with patch(
|
|
"azure.identity.aio.OnBehalfOfCredential", return_value=mock_credential
|
|
) as mock_class:
|
|
first = await provider.get_obo_credential(user_assertion="same-token")
|
|
second = await provider.get_obo_credential(user_assertion="same-token")
|
|
|
|
assert first is second
|
|
mock_class.assert_called_once()
|
|
|
|
async def test_get_obo_credential_different_assertions_get_different_credentials(
|
|
self,
|
|
):
|
|
"""Test that different assertions produce different credentials."""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
provider = AzureProvider(
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
tenant_id="test-tenant-id",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
)
|
|
|
|
creds = [MagicMock(), MagicMock()]
|
|
with patch("azure.identity.aio.OnBehalfOfCredential", side_effect=creds):
|
|
first = await provider.get_obo_credential(user_assertion="token-a")
|
|
second = await provider.get_obo_credential(user_assertion="token-b")
|
|
|
|
assert first is not second
|
|
assert first is creds[0]
|
|
assert second is creds[1]
|
|
|
|
async def test_get_obo_credential_evicts_oldest_when_over_capacity(self):
|
|
"""Test that credentials are evicted LRU-style when cache is full."""
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
provider = AzureProvider(
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
tenant_id="test-tenant-id",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
)
|
|
provider._obo_max_credentials = 2
|
|
|
|
creds = [MagicMock(close=AsyncMock()) for _ in range(3)]
|
|
with patch("azure.identity.aio.OnBehalfOfCredential", side_effect=creds):
|
|
await provider.get_obo_credential(user_assertion="token-1")
|
|
await provider.get_obo_credential(user_assertion="token-2")
|
|
await provider.get_obo_credential(user_assertion="token-3")
|
|
|
|
assert len(provider._obo_credentials) == 2
|
|
creds[0].close.assert_awaited_once()
|
|
# token-1's credential was evicted
|
|
assert (
|
|
await provider.get_obo_credential(user_assertion="token-2") is creds[1]
|
|
)
|
|
assert (
|
|
await provider.get_obo_credential(user_assertion="token-3") is creds[2]
|
|
)
|
|
|
|
async def test_close_obo_credentials(self):
|
|
"""Test that close_obo_credentials closes all cached credentials."""
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
provider = AzureProvider(
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
tenant_id="test-tenant-id",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["read"],
|
|
jwt_signing_key="test-secret",
|
|
)
|
|
|
|
creds = [MagicMock(close=AsyncMock()) for _ in range(2)]
|
|
with patch("azure.identity.aio.OnBehalfOfCredential", side_effect=creds):
|
|
await provider.get_obo_credential(user_assertion="token-a")
|
|
await provider.get_obo_credential(user_assertion="token-b")
|
|
|
|
await provider.close_obo_credentials()
|
|
|
|
assert len(provider._obo_credentials) == 0
|
|
for cred in creds:
|
|
cred.close.assert_awaited_once()
|
|
|
|
async def test_get_obo_credential_with_custom_authority(self):
|
|
"""Test that get_obo_credential uses custom base_authority."""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
provider = AzureProvider(
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
tenant_id="gov-tenant-id",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["read"],
|
|
base_authority="login.microsoftonline.us",
|
|
jwt_signing_key="test-secret",
|
|
)
|
|
|
|
mock_credential = MagicMock()
|
|
with patch(
|
|
"azure.identity.aio.OnBehalfOfCredential", return_value=mock_credential
|
|
) as mock_class:
|
|
await provider.get_obo_credential(user_assertion="user-token")
|
|
|
|
call_kwargs = mock_class.call_args[1]
|
|
assert call_kwargs["authority"] == "https://login.microsoftonline.us"
|
|
|
|
def test_tenant_and_authority_stored_as_attributes(self):
|
|
"""Test that tenant_id and base_authority are stored for OBO credential creation."""
|
|
provider = AzureProvider(
|
|
client_id="test-client-id",
|
|
client_secret="test-client-secret",
|
|
tenant_id="my-tenant",
|
|
base_url="https://myserver.com",
|
|
required_scopes=["read"],
|
|
base_authority="login.microsoftonline.us",
|
|
jwt_signing_key="test-secret",
|
|
)
|
|
|
|
assert provider._tenant_id == "my-tenant"
|
|
assert provider._base_authority == "login.microsoftonline.us"
|
|
|
|
def test_entra_obo_token_is_importable(self):
|
|
"""Test that EntraOBOToken can be imported."""
|
|
from fastmcp.server.auth.providers.azure import EntraOBOToken
|
|
|
|
assert EntraOBOToken is not None
|
|
|
|
def test_entra_obo_token_creates_dependency(self):
|
|
"""Test that EntraOBOToken creates a dependency with scopes."""
|
|
from fastmcp.server.auth.providers.azure import EntraOBOToken, _EntraOBOToken
|
|
|
|
dep = EntraOBOToken(["https://graph.microsoft.com/User.Read"])
|
|
assert isinstance(dep, _EntraOBOToken)
|
|
assert dep.scopes == ["https://graph.microsoft.com/User.Read"]
|
|
|
|
def test_entra_obo_token_is_dependency_instance(self):
|
|
"""Test that EntraOBOToken is a Dependency instance."""
|
|
from fastmcp.dependencies import Dependency
|
|
from fastmcp.server.auth.providers.azure import _EntraOBOToken
|
|
|
|
dep = _EntraOBOToken(["scope"])
|
|
assert isinstance(dep, Dependency)
|
|
|
|
|
|
class TestFindAzureProvider:
|
|
"""Tests for _find_azure_provider helper used by EntraOBOToken."""
|
|
|
|
def test_returns_azure_provider_directly(self, memory_storage):
|
|
"""When auth is an AzureProvider, return it directly."""
|
|
provider = AzureProvider(
|
|
tenant_id="test-tenant",
|
|
client_id="test-client",
|
|
client_secret="test-secret",
|
|
client_storage=memory_storage,
|
|
base_url="https://example.com",
|
|
required_scopes=["read"],
|
|
)
|
|
assert _find_azure_provider(provider) is provider
|
|
|
|
def test_unwraps_multiauth_with_azure_server(self, memory_storage):
|
|
"""When auth is a MultiAuth wrapping an AzureProvider, return the inner provider."""
|
|
provider = AzureProvider(
|
|
tenant_id="test-tenant",
|
|
client_id="test-client",
|
|
client_secret="test-secret",
|
|
client_storage=memory_storage,
|
|
base_url="https://example.com",
|
|
required_scopes=["read"],
|
|
)
|
|
multi = MultiAuth(server=provider)
|
|
assert _find_azure_provider(multi) is provider
|
|
|
|
def test_returns_none_for_no_auth(self):
|
|
"""When auth is None, return None."""
|
|
assert _find_azure_provider(None) is None
|
|
|
|
def test_returns_none_for_multiauth_without_azure_server(self):
|
|
"""When MultiAuth has no server or a non-Azure server, return None."""
|
|
verifier = StaticTokenVerifier(tokens={"t": {"client_id": "c", "scopes": []}})
|
|
multi = MultiAuth(verifiers=[verifier])
|
|
assert _find_azure_provider(multi) is None
|