Skip to content

Commit a45fc39

Browse files
authored
refactor: fall back to default grant types for config validation (#22)
* refactor: fall back to default grant types for config validation * refactor: use correct fall back method
1 parent 205158c commit a45fc39

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

mcpauth/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ class AuthServerType(str, Enum):
117117
OIDC = "oidc"
118118

119119

120+
class AuthorizationServerMetadataDefaults(Enum):
121+
grant_types_supported = ["authorization_code", "implicit"]
122+
response_modes_supported = ["query", "fragment"]
123+
124+
120125
class AuthServerConfig(BaseModel):
121126
"""
122127
Configuration for the remote authorization server integrated with the MCP server.

mcpauth/utils/_validate_server_config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from enum import Enum
22
from typing import Any, Dict, List, Optional
33
from pydantic import BaseModel
4-
from ..config import AuthServerConfig
4+
from ..config import AuthServerConfig, AuthorizationServerMetadataDefaults
55

66

77
class AuthServerConfigErrorCode(str, Enum):
@@ -97,6 +97,7 @@ def validate_server_config(
9797
invalid (`{ is_valid: False }`), along with any errors or warnings encountered during validation.
9898
"""
9999

100+
MetadataDefaults = AuthorizationServerMetadataDefaults
100101
errors: List[AuthServerConfigError] = []
101102
warnings: List[AuthServerConfigWarning] = []
102103
metadata = config.metadata
@@ -112,9 +113,10 @@ def validate_server_config(
112113
)
113114

114115
# Check if 'authorization_code' grant type is supported
115-
if (
116-
not metadata.grant_types_supported
117-
or "authorization_code" not in metadata.grant_types_supported
116+
if "authorization_code" not in (
117+
metadata.grant_types_supported
118+
if metadata.grant_types_supported is not None
119+
else MetadataDefaults.grant_types_supported.value
118120
):
119121
errors.append(
120122
_create_error(

tests/utils/validate_server_config_test.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ def test_valid_server_config(self):
2626
assert not hasattr(result, "errors") or len(result.errors) == 0
2727
assert result.warnings == []
2828

29+
def test_valid_server_config_no_grant_types(self):
30+
config = AuthServerConfig(
31+
type=AuthServerType.OAUTH,
32+
metadata=AuthorizationServerMetadata(
33+
issuer="https://example.com",
34+
authorization_endpoint="https://example.com/oauth/authorize",
35+
token_endpoint="https://example.com/oauth/token",
36+
response_types_supported=["code"],
37+
code_challenge_methods_supported=["S256"],
38+
registration_endpoint="https://example.com/register",
39+
),
40+
)
41+
42+
result = validate_server_config(config)
43+
assert result.is_valid is True
44+
assert not hasattr(result, "errors") or len(result.errors) == 0
45+
assert result.warnings == []
46+
2947
def test_invalid_server_config(self):
3048
config = AuthServerConfig(
3149
type=AuthServerType.OAUTH,
@@ -42,10 +60,6 @@ def test_invalid_server_config(self):
4260

4361
error_codes = [error.code for error in result.errors]
4462
assert AuthServerConfigErrorCode.CODE_RESPONSE_TYPE_NOT_SUPPORTED in error_codes
45-
assert (
46-
AuthServerConfigErrorCode.AUTHORIZATION_CODE_GRANT_NOT_SUPPORTED
47-
in error_codes
48-
)
4963
assert AuthServerConfigErrorCode.PKCE_NOT_SUPPORTED in error_codes
5064

5165
warning_codes = [warning.code for warning in result.warnings]
@@ -78,7 +92,7 @@ def test_warning_for_missing_dynamic_registration(self):
7892
)
7993
assert len(result.warnings) == 1
8094

81-
def test_code_challenge_methods(self):
95+
def test_invalid_code_challenge_methods(self):
8296
config = AuthServerConfig(
8397
type=AuthServerType.OAUTH,
8498
metadata=AuthorizationServerMetadata(
@@ -99,3 +113,25 @@ def test_code_challenge_methods(self):
99113
AuthServerConfigErrorCode.S256_CODE_CHALLENGE_METHOD_NOT_SUPPORTED
100114
in error_codes
101115
)
116+
117+
def test_invalid_grant_type(self):
118+
config = AuthServerConfig(
119+
type=AuthServerType.OAUTH,
120+
metadata=AuthorizationServerMetadata(
121+
issuer="https://example.com",
122+
authorization_endpoint="https://example.com/oauth/authorize",
123+
token_endpoint="https://example.com/oauth/token",
124+
response_types_supported=["code"],
125+
grant_types_supported=[], # Use empty list on purpose to ensure it should be treated correctly
126+
code_challenge_methods_supported=["S256"],
127+
),
128+
)
129+
130+
result = validate_server_config(config)
131+
assert result.is_valid is False
132+
133+
error_codes = [error.code for error in result.errors]
134+
assert (
135+
AuthServerConfigErrorCode.AUTHORIZATION_CODE_GRANT_NOT_SUPPORTED
136+
in error_codes
137+
)

0 commit comments

Comments
 (0)