mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-21 13:04:18 +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)
611 lines
21 KiB
Python
611 lines
21 KiB
Python
from collections.abc import AsyncGenerator
|
|
from typing import Any
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from pytest_httpx import HTTPXMock
|
|
|
|
from fastmcp import FastMCP
|
|
from fastmcp.server.auth.providers.jwt import JWKData, JWKSData, JWTVerifier, RSAKeyPair
|
|
from fastmcp.utilities.tests import run_server_async
|
|
|
|
# Standard public IP used for DNS mocking in tests
|
|
TEST_PUBLIC_IP = "93.184.216.34"
|
|
|
|
|
|
class SymmetricKeyHelper:
|
|
"""Helper class for generating symmetric key JWT tokens for testing."""
|
|
|
|
def __init__(self, secret: str):
|
|
"""Initialize with a secret key."""
|
|
self.secret = secret
|
|
|
|
def create_token(
|
|
self,
|
|
subject: str = "fastmcp-user",
|
|
issuer: str = "https://fastmcp.example.com",
|
|
audience: str | list[str] | None = None,
|
|
scopes: list[str] | None = None,
|
|
expires_in_seconds: int = 3600,
|
|
additional_claims: dict[str, Any] | None = None,
|
|
algorithm: str = "HS256",
|
|
) -> str:
|
|
"""
|
|
Generate a test JWT token using symmetric key for testing purposes.
|
|
|
|
Args:
|
|
subject: Subject claim (usually user ID)
|
|
issuer: Issuer claim
|
|
audience: Audience claim - can be a string or list of strings (optional)
|
|
scopes: List of scopes to include
|
|
expires_in_seconds: Token expiration time in seconds
|
|
additional_claims: Any additional claims to include
|
|
algorithm: JWT signing algorithm (HS256, HS384, or HS512)
|
|
"""
|
|
import time
|
|
|
|
from authlib.jose import JsonWebToken
|
|
|
|
# Create header
|
|
header = {"alg": algorithm}
|
|
|
|
# Create payload
|
|
payload: dict[str, str | int | list[str]] = {
|
|
"sub": subject,
|
|
"iss": issuer,
|
|
"iat": int(time.time()),
|
|
"exp": int(time.time()) + expires_in_seconds,
|
|
}
|
|
|
|
if audience:
|
|
payload["aud"] = audience
|
|
|
|
if scopes:
|
|
payload["scope"] = " ".join(scopes)
|
|
|
|
if additional_claims:
|
|
payload.update(additional_claims)
|
|
|
|
# Create JWT
|
|
jwt_lib = JsonWebToken([algorithm])
|
|
token_bytes = jwt_lib.encode(header, payload, self.secret)
|
|
|
|
return token_bytes.decode("utf-8")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def rsa_key_pair() -> RSAKeyPair:
|
|
return RSAKeyPair.generate()
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def symmetric_key_helper() -> SymmetricKeyHelper:
|
|
"""Generate a symmetric key helper for testing."""
|
|
return SymmetricKeyHelper("test-secret-key-for-hmac-signing")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def bearer_token(rsa_key_pair: RSAKeyPair) -> str:
|
|
return rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def bearer_provider(rsa_key_pair: RSAKeyPair) -> JWTVerifier:
|
|
return JWTVerifier(
|
|
public_key=rsa_key_pair.public_key,
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def symmetric_provider(symmetric_key_helper: SymmetricKeyHelper) -> JWTVerifier:
|
|
"""Create JWTVerifier configured for symmetric key verification."""
|
|
return JWTVerifier(
|
|
public_key=symmetric_key_helper.secret,
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
algorithm="HS256",
|
|
)
|
|
|
|
|
|
def create_mcp_server(
|
|
public_key: str,
|
|
auth_kwargs: dict[str, Any] | None = None,
|
|
) -> FastMCP:
|
|
mcp = FastMCP(
|
|
auth=JWTVerifier(
|
|
public_key=public_key,
|
|
**auth_kwargs or {},
|
|
)
|
|
)
|
|
|
|
@mcp.tool
|
|
def add(a: int, b: int) -> int:
|
|
return a + b
|
|
|
|
return mcp
|
|
|
|
|
|
@pytest.fixture
|
|
async def mcp_server_url(rsa_key_pair: RSAKeyPair) -> AsyncGenerator[str, None]:
|
|
server = create_mcp_server(
|
|
public_key=rsa_key_pair.public_key,
|
|
auth_kwargs=dict(
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
),
|
|
)
|
|
async with run_server_async(server, transport="http") as url:
|
|
yield url
|
|
|
|
|
|
class TestRSAKeyPair:
|
|
def test_generate_key_pair(self):
|
|
"""Test RSA key pair generation."""
|
|
key_pair = RSAKeyPair.generate()
|
|
|
|
assert key_pair.private_key is not None
|
|
assert key_pair.public_key is not None
|
|
|
|
# Check that keys are in PEM format
|
|
private_pem = key_pair.private_key.get_secret_value()
|
|
public_pem = key_pair.public_key
|
|
|
|
assert "-----BEGIN PRIVATE KEY-----" in private_pem
|
|
assert "-----END PRIVATE KEY-----" in private_pem
|
|
assert "-----BEGIN PUBLIC KEY-----" in public_pem
|
|
assert "-----END PUBLIC KEY-----" in public_pem
|
|
|
|
def test_create_basic_token(self, rsa_key_pair: RSAKeyPair):
|
|
"""Test basic token creation."""
|
|
token = rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
)
|
|
|
|
assert isinstance(token, str)
|
|
assert len(token.split(".")) == 3 # JWT has 3 parts
|
|
|
|
def test_create_token_with_scopes(self, rsa_key_pair: RSAKeyPair):
|
|
"""Test token creation with scopes."""
|
|
token = rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
scopes=["read", "write"],
|
|
)
|
|
|
|
assert isinstance(token, str)
|
|
# We'll validate the scopes in the BearerToken tests
|
|
|
|
|
|
class TestSymmetricKeyJWT:
|
|
"""Tests for JWT verification using symmetric keys (HMAC algorithms)."""
|
|
|
|
def test_initialization_with_symmetric_key(
|
|
self, symmetric_key_helper: SymmetricKeyHelper
|
|
):
|
|
"""Test JWTVerifier initialization with symmetric key."""
|
|
provider = JWTVerifier(
|
|
public_key=symmetric_key_helper.secret,
|
|
issuer="https://test.example.com",
|
|
algorithm="HS256",
|
|
)
|
|
|
|
assert provider.issuer == "https://test.example.com"
|
|
assert provider.public_key == symmetric_key_helper.secret
|
|
assert provider.algorithm == "HS256"
|
|
assert provider.jwks_uri is None
|
|
|
|
def test_initialization_rejects_hs_algorithm_with_jwks_uri(self):
|
|
"""Test that HMAC algorithms cannot be used with JWKS URI."""
|
|
with pytest.raises(ValueError, match="cannot be used with jwks_uri"):
|
|
JWTVerifier(
|
|
jwks_uri="https://test.example.com/.well-known/jwks.json",
|
|
issuer="https://test.example.com",
|
|
algorithm="HS256",
|
|
)
|
|
|
|
def test_initialization_with_different_symmetric_algorithms(
|
|
self, symmetric_key_helper: SymmetricKeyHelper
|
|
):
|
|
"""Test JWTVerifier initialization with different HMAC algorithms."""
|
|
algorithms = ["HS256", "HS384", "HS512"]
|
|
|
|
for algorithm in algorithms:
|
|
provider = JWTVerifier(
|
|
public_key=symmetric_key_helper.secret,
|
|
issuer="https://test.example.com",
|
|
algorithm=algorithm,
|
|
)
|
|
assert provider.algorithm == algorithm
|
|
|
|
def test_symmetric_algorithm_rejects_jwks_uri(self):
|
|
"""HS* algorithms must not be configured with JWKS/public key endpoints."""
|
|
with pytest.raises(ValueError, match="cannot be used with jwks_uri"):
|
|
JWTVerifier(
|
|
jwks_uri="https://test.example.com/.well-known/jwks.json",
|
|
issuer="https://test.example.com",
|
|
algorithm="HS256",
|
|
)
|
|
|
|
def test_symmetric_algorithm_rejects_pem_public_key(self, rsa_key_pair: RSAKeyPair):
|
|
"""HS* algorithms must use a shared secret, not PEM public key material."""
|
|
with pytest.raises(ValueError, match="require a shared secret"):
|
|
JWTVerifier(
|
|
public_key=rsa_key_pair.public_key,
|
|
issuer="https://test.example.com",
|
|
algorithm="HS256",
|
|
)
|
|
|
|
def test_symmetric_algorithm_accepts_bytes_secret(self):
|
|
"""HS* algorithms accept bytes secrets without TypeError."""
|
|
verifier = JWTVerifier(
|
|
public_key=b"secret",
|
|
algorithm="HS256",
|
|
)
|
|
assert verifier.algorithm == "HS256"
|
|
|
|
async def test_valid_symmetric_token_validation(
|
|
self, symmetric_key_helper: SymmetricKeyHelper, symmetric_provider: JWTVerifier
|
|
):
|
|
"""Test validation of a valid token signed with symmetric key."""
|
|
token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
scopes=["read", "write"],
|
|
algorithm="HS256",
|
|
)
|
|
|
|
access_token = await symmetric_provider.load_access_token(token)
|
|
|
|
assert access_token is not None
|
|
assert access_token.client_id == "test-user"
|
|
assert "read" in access_token.scopes
|
|
assert "write" in access_token.scopes
|
|
assert access_token.expires_at is not None
|
|
|
|
async def test_symmetric_token_with_different_algorithms(
|
|
self, symmetric_key_helper: SymmetricKeyHelper
|
|
):
|
|
"""Test that different HMAC algorithms work correctly."""
|
|
algorithms = ["HS256", "HS384", "HS512"]
|
|
|
|
for algorithm in algorithms:
|
|
provider = JWTVerifier(
|
|
public_key=symmetric_key_helper.secret,
|
|
issuer="https://test.example.com",
|
|
algorithm=algorithm,
|
|
)
|
|
|
|
token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
algorithm=algorithm,
|
|
)
|
|
|
|
access_token = await provider.load_access_token(token)
|
|
assert access_token is not None
|
|
assert access_token.client_id == "test-user"
|
|
|
|
async def test_symmetric_token_issuer_validation(
|
|
self, symmetric_key_helper: SymmetricKeyHelper, symmetric_provider: JWTVerifier
|
|
):
|
|
"""Test issuer validation with symmetric key tokens."""
|
|
# Valid issuer
|
|
valid_token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
access_token = await symmetric_provider.load_access_token(valid_token)
|
|
assert access_token is not None
|
|
|
|
# Invalid issuer
|
|
invalid_token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://evil.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
access_token = await symmetric_provider.load_access_token(invalid_token)
|
|
assert access_token is None
|
|
|
|
async def test_symmetric_token_audience_validation(
|
|
self, symmetric_key_helper: SymmetricKeyHelper, symmetric_provider: JWTVerifier
|
|
):
|
|
"""Test audience validation with symmetric key tokens."""
|
|
# Valid audience
|
|
valid_token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
access_token = await symmetric_provider.load_access_token(valid_token)
|
|
assert access_token is not None
|
|
|
|
# Invalid audience
|
|
invalid_token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://wrong-api.example.com",
|
|
)
|
|
access_token = await symmetric_provider.load_access_token(invalid_token)
|
|
assert access_token is None
|
|
|
|
async def test_symmetric_token_scope_extraction(
|
|
self, symmetric_key_helper: SymmetricKeyHelper, symmetric_provider: JWTVerifier
|
|
):
|
|
"""Test scope extraction from symmetric key tokens."""
|
|
token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
scopes=["read", "write", "admin"],
|
|
)
|
|
|
|
access_token = await symmetric_provider.load_access_token(token)
|
|
assert access_token is not None
|
|
assert set(access_token.scopes) == {"read", "write", "admin"}
|
|
|
|
async def test_symmetric_token_expiration(
|
|
self, symmetric_key_helper: SymmetricKeyHelper, symmetric_provider: JWTVerifier
|
|
):
|
|
"""Test expiration validation with symmetric key tokens."""
|
|
# Valid token
|
|
valid_token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
expires_in_seconds=3600, # 1 hour from now
|
|
)
|
|
access_token = await symmetric_provider.load_access_token(valid_token)
|
|
assert access_token is not None
|
|
|
|
# Expired token
|
|
expired_token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
expires_in_seconds=-3600, # 1 hour ago
|
|
)
|
|
access_token = await symmetric_provider.load_access_token(expired_token)
|
|
assert access_token is None
|
|
|
|
async def test_symmetric_token_invalid_signature(
|
|
self, symmetric_key_helper: SymmetricKeyHelper, symmetric_provider: JWTVerifier
|
|
):
|
|
"""Test rejection of tokens with invalid signatures."""
|
|
# Create a token with a different secret
|
|
other_helper = SymmetricKeyHelper("different-secret-key")
|
|
token = other_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
access_token = await symmetric_provider.load_access_token(token)
|
|
assert access_token is None
|
|
|
|
async def test_symmetric_token_algorithm_mismatch(
|
|
self, symmetric_key_helper: SymmetricKeyHelper
|
|
):
|
|
"""Test that tokens with mismatched algorithms are rejected."""
|
|
# Create provider expecting HS256
|
|
provider = JWTVerifier(
|
|
public_key=symmetric_key_helper.secret,
|
|
issuer="https://test.example.com",
|
|
algorithm="HS256",
|
|
)
|
|
|
|
# Create token with HS512
|
|
token = symmetric_key_helper.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
algorithm="HS512",
|
|
)
|
|
|
|
# Should fail because provider expects HS256
|
|
access_token = await provider.load_access_token(token)
|
|
assert access_token is None
|
|
|
|
|
|
class TestBearerTokenJWKS:
|
|
"""Tests for JWKS URI functionality.
|
|
|
|
Note: With SSRF protection, JWKS fetches validate DNS and connect to the
|
|
resolved IP. Tests mock DNS resolution to return a public IP.
|
|
"""
|
|
|
|
@pytest.fixture
|
|
def jwks_provider(self, rsa_key_pair: RSAKeyPair) -> JWTVerifier:
|
|
"""Provider configured with JWKS URI."""
|
|
return JWTVerifier(
|
|
jwks_uri="https://test.example.com/.well-known/jwks.json",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
@pytest.fixture
|
|
def mock_jwks_data(self, rsa_key_pair: RSAKeyPair) -> JWKSData:
|
|
"""Create mock JWKS data from RSA key pair."""
|
|
from authlib.jose import JsonWebKey
|
|
|
|
# Create JWK from the RSA public key
|
|
jwk = JsonWebKey.import_key(rsa_key_pair.public_key)
|
|
jwk_data: JWKData = jwk.as_dict()
|
|
jwk_data["kid"] = "test-key-1"
|
|
jwk_data["alg"] = "RS256"
|
|
|
|
return {"keys": [jwk_data]}
|
|
|
|
@pytest.fixture
|
|
def mock_dns(self):
|
|
"""Mock DNS resolution to return test public IP."""
|
|
with patch(
|
|
"fastmcp.server.auth.ssrf.resolve_hostname",
|
|
return_value=[TEST_PUBLIC_IP],
|
|
):
|
|
yield
|
|
|
|
async def test_jwks_token_validation(
|
|
self,
|
|
rsa_key_pair: RSAKeyPair,
|
|
jwks_provider: JWTVerifier,
|
|
mock_jwks_data: JWKSData,
|
|
httpx_mock: HTTPXMock,
|
|
mock_dns,
|
|
):
|
|
"""Test token validation using JWKS URI."""
|
|
httpx_mock.add_response(json=mock_jwks_data)
|
|
|
|
username = "test-user"
|
|
issuer = "https://test.example.com"
|
|
audience = "https://api.example.com"
|
|
|
|
token = rsa_key_pair.create_token(
|
|
subject=username,
|
|
issuer=issuer,
|
|
audience=audience,
|
|
)
|
|
|
|
access_token = await jwks_provider.load_access_token(token)
|
|
assert access_token is not None
|
|
assert access_token.client_id == username
|
|
|
|
# ensure the raw claims are present - #1398
|
|
assert access_token.claims.get("sub") == username
|
|
assert access_token.claims.get("iss") == issuer
|
|
assert access_token.claims.get("aud") == audience
|
|
|
|
async def test_jwks_token_validation_with_invalid_key(
|
|
self,
|
|
rsa_key_pair: RSAKeyPair,
|
|
jwks_provider: JWTVerifier,
|
|
mock_jwks_data: JWKSData,
|
|
httpx_mock: HTTPXMock,
|
|
mock_dns,
|
|
):
|
|
httpx_mock.add_response(json=mock_jwks_data)
|
|
token = RSAKeyPair.generate().create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
access_token = await jwks_provider.load_access_token(token)
|
|
assert access_token is None
|
|
|
|
async def test_jwks_token_validation_with_kid(
|
|
self,
|
|
rsa_key_pair: RSAKeyPair,
|
|
jwks_provider: JWTVerifier,
|
|
mock_jwks_data: JWKSData,
|
|
httpx_mock: HTTPXMock,
|
|
mock_dns,
|
|
):
|
|
mock_jwks_data["keys"][0]["kid"] = "test-key-1"
|
|
httpx_mock.add_response(json=mock_jwks_data)
|
|
token = rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
kid="test-key-1",
|
|
)
|
|
|
|
access_token = await jwks_provider.load_access_token(token)
|
|
assert access_token is not None
|
|
assert access_token.client_id == "test-user"
|
|
|
|
async def test_jwks_token_validation_with_kid_and_no_kid_in_token(
|
|
self,
|
|
rsa_key_pair: RSAKeyPair,
|
|
jwks_provider: JWTVerifier,
|
|
mock_jwks_data: JWKSData,
|
|
httpx_mock: HTTPXMock,
|
|
mock_dns,
|
|
):
|
|
mock_jwks_data["keys"][0]["kid"] = "test-key-1"
|
|
httpx_mock.add_response(json=mock_jwks_data)
|
|
token = rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
access_token = await jwks_provider.load_access_token(token)
|
|
assert access_token is not None
|
|
assert access_token.client_id == "test-user"
|
|
|
|
async def test_jwks_token_validation_with_no_kid_and_kid_in_jwks(
|
|
self,
|
|
rsa_key_pair: RSAKeyPair,
|
|
jwks_provider: JWTVerifier,
|
|
mock_jwks_data: JWKSData,
|
|
httpx_mock: HTTPXMock,
|
|
mock_dns,
|
|
):
|
|
mock_jwks_data["keys"][0]["kid"] = "test-key-1"
|
|
httpx_mock.add_response(json=mock_jwks_data)
|
|
token = rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
access_token = await jwks_provider.load_access_token(token)
|
|
assert access_token is not None
|
|
assert access_token.client_id == "test-user"
|
|
|
|
async def test_jwks_token_validation_with_kid_mismatch(
|
|
self,
|
|
rsa_key_pair: RSAKeyPair,
|
|
jwks_provider: JWTVerifier,
|
|
mock_jwks_data: JWKSData,
|
|
httpx_mock: HTTPXMock,
|
|
mock_dns,
|
|
):
|
|
mock_jwks_data["keys"][0]["kid"] = "test-key-1"
|
|
httpx_mock.add_response(json=mock_jwks_data)
|
|
token = rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
kid="test-key-2",
|
|
)
|
|
|
|
access_token = await jwks_provider.load_access_token(token)
|
|
assert access_token is None
|
|
|
|
async def test_jwks_token_validation_with_multiple_keys_and_no_kid_in_token(
|
|
self,
|
|
rsa_key_pair: RSAKeyPair,
|
|
jwks_provider: JWTVerifier,
|
|
mock_jwks_data: JWKSData,
|
|
httpx_mock: HTTPXMock,
|
|
mock_dns,
|
|
):
|
|
mock_jwks_data["keys"] = [
|
|
{
|
|
"kid": "test-key-1",
|
|
"alg": "RS256",
|
|
},
|
|
{
|
|
"kid": "test-key-2",
|
|
"alg": "RS256",
|
|
},
|
|
]
|
|
|
|
httpx_mock.add_response(json=mock_jwks_data)
|
|
token = rsa_key_pair.create_token(
|
|
subject="test-user",
|
|
issuer="https://test.example.com",
|
|
audience="https://api.example.com",
|
|
)
|
|
|
|
access_token = await jwks_provider.load_access_token(token)
|
|
assert access_token is None
|