Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Supporting Multiple Issuers For JWTVerifier Oauth Workflow (#2233)

mhassaninmsftb24d77145717
diff --git a/src/fastmcp/server/auth/providers/jwt.py b/src/fastmcp/server/auth/providers/jwt.py
index 74ca55bd2..b03463ee8 100644
--- a/src/fastmcp/server/auth/providers/jwt.py
+++ b/src/fastmcp/server/auth/providers/jwt.py
@@ -150,7 +150,7 @@ class JWTVerifierSettings(BaseSettings):
public_key: str | None = None
jwks_uri: str | None = None
- issuer: str | None = None
+ issuer: str | list[str] | None = None
algorithm: str | None = None
audience: str | list[str] | None = None
required_scopes: list[str] | None = None
@@ -186,7 +186,7 @@ class JWTVerifier(TokenVerifier):
*,
public_key: str | NotSetT | None = NotSet,
jwks_uri: str | NotSetT | None = NotSet,
- issuer: str | NotSetT | None = NotSet,
+ issuer: str | list[str] | NotSetT | None = NotSet,
audience: str | list[str] | NotSetT | None = NotSet,
algorithm: str | NotSetT | None = NotSet,
required_scopes: list[str] | NotSetT | None = NotSet,
@@ -400,13 +400,25 @@ class JWTVerifier(TokenVerifier):
# Validate issuer - note we use issuer instead of issuer_url here because
# issuer is optional, allowing users to make this check optional
- if self.issuer and claims.get("iss") != self.issuer:
- self.logger.debug(
- "Token validation failed: issuer mismatch for client %s",
- client_id,
- )
- self.logger.info("Bearer token rejected for client %s", client_id)
- return None
+ if self.issuer:
+ iss = claims.get("iss")
+
+ # Handle different combinations of issuer types
+ issuer_valid = False
+ if isinstance(self.issuer, list):
+ # self.issuer is a list - check if token issuer matches any expected issuer
+ issuer_valid = iss in self.issuer
+ else:
+ # self.issuer is a string - check for equality
+ issuer_valid = iss == self.issuer
+
+ if not issuer_valid:
+ self.logger.debug(
+ "Token validation failed: issuer mismatch for client %s",
+ client_id,
+ )
+ self.logger.info("Bearer token rejected for client %s", client_id)
+ return None
# Validate audience if configured
if self.audience:
diff --git a/tests/server/auth/test_jwt_provider.py b/tests/server/auth/test_jwt_provider.py
index 773d365e9..6f79bacb4 100644
--- a/tests/server/auth/test_jwt_provider.py
+++ b/tests/server/auth/test_jwt_provider.py
@@ -763,6 +763,29 @@ class TestBearerToken:
access_token3 = await provider.load_access_token(token3)
assert access_token3 is None
+ @pytest.mark.parametrize(
+ ("iss", "expected"),
+ [
+ ("https://test.example.com", True),
+ ("https://other-issuer.example.com", True),
+ ("https://wrong-issuer.example.com", False),
+ ],
+ )
+ async def test_provider_with_multiple_expected_issuers(
+ self, rsa_key_pair: RSAKeyPair, iss: str, expected: bool
+ ):
+ """Provider accepts any issuer from the configured list."""
+ provider = JWTVerifier(
+ public_key=rsa_key_pair.public_key,
+ issuer=["https://test.example.com", "https://other-issuer.example.com"],
+ audience="https://api.example.com",
+ )
+ token = rsa_key_pair.create_token(
+ subject="test-user", issuer=iss, audience="https://api.example.com"
+ )
+ access_token = await provider.load_access_token(token)
+ assert (access_token is not None) is expected
+
async def test_scope_extraction_string(
self, rsa_key_pair: RSAKeyPair, bearer_provider: JWTVerifier
):