|
6 | 6 | from mcpauth.config import MCPAuthConfig
|
7 | 7 | from mcpauth.models.auth_server import AuthServerConfig, AuthServerType
|
8 | 8 | from mcpauth.models.oauth import AuthorizationServerMetadata
|
| 9 | +from mcpauth.middleware.create_bearer_auth import BaseBearerAuthConfig |
| 10 | +from mcpauth.middleware.create_bearer_auth import BaseBearerAuthConfig |
| 11 | +from mcpauth.middleware.create_bearer_auth import BaseBearerAuthConfig |
| 12 | +from mcpauth.middleware.create_bearer_auth import BaseBearerAuthConfig |
9 | 13 |
|
10 | 14 |
|
11 | 15 | class TestMCPAuth:
|
@@ -72,6 +76,8 @@ def test_init_with_warnings(self, mock_warning: MagicMock):
|
72 | 76 | # Verify
|
73 | 77 | assert mock_warning.called
|
74 | 78 |
|
| 79 | + |
| 80 | +class TestDelegatedMiddleware: |
75 | 81 | @pytest.mark.asyncio
|
76 | 82 | async def test_delegated_middleware_oauth_endpoint(self):
|
77 | 83 | # Setup
|
@@ -135,3 +141,122 @@ async def test_delegated_middleware_other_endpoint(self):
|
135 | 141 | # Verify
|
136 | 142 | assert mock_call_next.called
|
137 | 143 | assert response == mock_response
|
| 144 | + |
| 145 | + |
| 146 | +class TestBearerAuthMiddleware: |
| 147 | + def test_bearer_auth_middleware_jwt_mode(self): |
| 148 | + # Setup |
| 149 | + server_config = AuthServerConfig( |
| 150 | + type=AuthServerType.OAUTH, |
| 151 | + metadata=AuthorizationServerMetadata( |
| 152 | + issuer="https://example.com", |
| 153 | + authorization_endpoint="https://example.com/oauth/authorize", |
| 154 | + token_endpoint="https://example.com/oauth/token", |
| 155 | + jwks_uri="https://example.com/.well-known/jwks.json", |
| 156 | + response_types_supported=["code"], |
| 157 | + grant_types_supported=["authorization_code"], |
| 158 | + code_challenge_methods_supported=["S256"], |
| 159 | + ), |
| 160 | + ) |
| 161 | + config = MCPAuthConfig(server=server_config) |
| 162 | + auth = MCPAuth(config) |
| 163 | + |
| 164 | + # Exercise |
| 165 | + with patch( |
| 166 | + "mcpauth.utils.create_verify_jwt.create_verify_jwt" |
| 167 | + ) as mock_create_verify_jwt: |
| 168 | + mock_create_verify_jwt.return_value = MagicMock() |
| 169 | + middleware_class = auth.bearer_auth_middleware( |
| 170 | + "jwt", BaseBearerAuthConfig(required_scopes=["profile"]) |
| 171 | + ) |
| 172 | + |
| 173 | + # Verify |
| 174 | + assert middleware_class is not None |
| 175 | + mock_create_verify_jwt.assert_called_once_with( |
| 176 | + "https://example.com/.well-known/jwks.json", options={} |
| 177 | + ) |
| 178 | + |
| 179 | + def test_bearer_auth_middleware_custom_verify(self): |
| 180 | + # Setup |
| 181 | + server_config = AuthServerConfig( |
| 182 | + type=AuthServerType.OAUTH, |
| 183 | + metadata=AuthorizationServerMetadata( |
| 184 | + issuer="https://example.com", |
| 185 | + authorization_endpoint="https://example.com/oauth/authorize", |
| 186 | + token_endpoint="https://example.com/oauth/token", |
| 187 | + response_types_supported=["code"], |
| 188 | + grant_types_supported=["authorization_code"], |
| 189 | + code_challenge_methods_supported=["S256"], |
| 190 | + ), |
| 191 | + ) |
| 192 | + config = MCPAuthConfig(server=server_config) |
| 193 | + auth = MCPAuth(config) |
| 194 | + |
| 195 | + custom_verify = MagicMock() |
| 196 | + |
| 197 | + # Exercise |
| 198 | + with patch( |
| 199 | + "mcpauth.middleware.create_bearer_auth.create_bearer_auth" |
| 200 | + ) as mock_create_bearer_auth: |
| 201 | + middleware_class = auth.bearer_auth_middleware( |
| 202 | + custom_verify, BaseBearerAuthConfig(required_scopes=["profile"]) |
| 203 | + ) |
| 204 | + |
| 205 | + # Verify |
| 206 | + assert middleware_class is not None |
| 207 | + mock_create_bearer_auth.assert_called_once() |
| 208 | + args, kwargs = mock_create_bearer_auth.call_args |
| 209 | + assert args[0] == custom_verify |
| 210 | + assert kwargs == {} |
| 211 | + |
| 212 | + def test_bearer_auth_middleware_jwt_without_jwks_uri(self): |
| 213 | + # Setup |
| 214 | + server_config = AuthServerConfig( |
| 215 | + type=AuthServerType.OAUTH, |
| 216 | + metadata=AuthorizationServerMetadata( |
| 217 | + issuer="https://example.com", |
| 218 | + authorization_endpoint="https://example.com/oauth/authorize", |
| 219 | + token_endpoint="https://example.com/oauth/token", |
| 220 | + # No jwks_uri |
| 221 | + response_types_supported=["code"], |
| 222 | + grant_types_supported=["authorization_code"], |
| 223 | + code_challenge_methods_supported=["S256"], |
| 224 | + ), |
| 225 | + ) |
| 226 | + config = MCPAuthConfig(server=server_config) |
| 227 | + auth = MCPAuth(config) |
| 228 | + |
| 229 | + # Exercise & Verify |
| 230 | + with pytest.raises(MCPAuthAuthServerException) as exc_info: |
| 231 | + auth.bearer_auth_middleware( |
| 232 | + "jwt", BaseBearerAuthConfig(required_scopes=["profile"]) |
| 233 | + ) |
| 234 | + |
| 235 | + assert exc_info.value.code == AuthServerExceptionCode.MISSING_JWKS_URI |
| 236 | + |
| 237 | + def test_bearer_auth_middleware_invalid_mode(self): |
| 238 | + # Setup |
| 239 | + server_config = AuthServerConfig( |
| 240 | + type=AuthServerType.OAUTH, |
| 241 | + metadata=AuthorizationServerMetadata( |
| 242 | + issuer="https://example.com", |
| 243 | + authorization_endpoint="https://example.com/oauth/authorize", |
| 244 | + token_endpoint="https://example.com/oauth/token", |
| 245 | + response_types_supported=["code"], |
| 246 | + grant_types_supported=["authorization_code"], |
| 247 | + code_challenge_methods_supported=["S256"], |
| 248 | + ), |
| 249 | + ) |
| 250 | + config = MCPAuthConfig(server=server_config) |
| 251 | + auth = MCPAuth(config) |
| 252 | + |
| 253 | + # Exercise & Verify |
| 254 | + with pytest.raises(ValueError) as exc_info: |
| 255 | + auth.bearer_auth_middleware( |
| 256 | + "invalid_mode", # type: ignore |
| 257 | + BaseBearerAuthConfig(required_scopes=["profile"]), |
| 258 | + ) |
| 259 | + |
| 260 | + assert "mode_or_verify must be 'jwt' or a callable function" in str( |
| 261 | + exc_info.value |
| 262 | + ) |
0 commit comments