golf-mcp 0.1.20__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of golf-mcp might be problematic. Click here for more details.

Files changed (123) hide show
  1. golf/__init__.py +9 -1
  2. golf/_endpoints.py +6 -0
  3. golf/_endpoints_fallback.py +10 -0
  4. golf/auth/__init__.py +235 -83
  5. golf/auth/api_key.py +6 -14
  6. golf/auth/factory.py +358 -0
  7. golf/auth/helpers.py +12 -42
  8. golf/auth/providers.py +446 -0
  9. golf/auth/registry.py +256 -0
  10. golf/cli/branding.py +192 -0
  11. golf/cli/main.py +28 -69
  12. golf/commands/__init__.py +2 -0
  13. golf/commands/build.py +4 -7
  14. golf/commands/init.py +30 -53
  15. golf/commands/run.py +50 -20
  16. golf/core/builder.py +355 -414
  17. golf/core/builder_auth.py +63 -144
  18. golf/core/builder_telemetry.py +26 -3
  19. golf/core/config.py +38 -59
  20. golf/core/parser.py +132 -139
  21. golf/core/platform.py +12 -10
  22. golf/core/telemetry.py +11 -19
  23. golf/core/transformer.py +38 -15
  24. golf/examples/__pycache__/__init__.cpython-311.pyc +0 -0
  25. golf/examples/basic/.coverage +0 -0
  26. golf/examples/basic/.env.example +8 -4
  27. golf/examples/basic/README.md +117 -45
  28. golf/examples/basic/__pycache__/auth.cpython-311.pyc +0 -0
  29. golf/examples/basic/auth.py +76 -0
  30. golf/examples/basic/golf.json +2 -5
  31. golf/examples/basic/htmlcov/.gitignore +2 -0
  32. golf/examples/basic/htmlcov/class_index.html +547 -0
  33. golf/examples/basic/htmlcov/coverage_html_cb_6fb7b396.js +733 -0
  34. golf/examples/basic/htmlcov/favicon_32_cb_58284776.png +0 -0
  35. golf/examples/basic/htmlcov/function_index.html +2091 -0
  36. golf/examples/basic/htmlcov/index.html +349 -0
  37. golf/examples/basic/htmlcov/keybd_closed_cb_ce680311.png +0 -0
  38. golf/examples/basic/htmlcov/status.json +1 -0
  39. golf/examples/basic/htmlcov/style_cb_8e611ae1.css +337 -0
  40. golf/examples/basic/htmlcov/z_1c9a91c0e91c8496___init___py.html +323 -0
  41. golf/examples/basic/htmlcov/z_1c9a91c0e91c8496_api_key_py.html +170 -0
  42. golf/examples/basic/htmlcov/z_1c9a91c0e91c8496_factory_py.html +430 -0
  43. golf/examples/basic/htmlcov/z_1c9a91c0e91c8496_helpers_py.html +288 -0
  44. golf/examples/basic/htmlcov/z_1c9a91c0e91c8496_providers_py.html +493 -0
  45. golf/examples/basic/htmlcov/z_1c9a91c0e91c8496_registry_py.html +353 -0
  46. golf/examples/basic/htmlcov/z_3ec3b3f490dc0950___init___py.html +120 -0
  47. golf/examples/basic/htmlcov/z_3ec3b3f490dc0950_instrumentation_py.html +1535 -0
  48. golf/examples/basic/htmlcov/z_4b8b9dd4ccccc5db___init___py.html +98 -0
  49. golf/examples/basic/htmlcov/z_4b8b9dd4ccccc5db_branding_py.html +289 -0
  50. golf/examples/basic/htmlcov/z_4b8b9dd4ccccc5db_main_py.html +476 -0
  51. golf/examples/basic/htmlcov/z_5a6c4e6bcc86fb2f___init___py.html +97 -0
  52. golf/examples/basic/htmlcov/z_6cadab9ec0df475d___init___py.html +102 -0
  53. golf/examples/basic/htmlcov/z_6cadab9ec0df475d_build_py.html +178 -0
  54. golf/examples/basic/htmlcov/z_6cadab9ec0df475d_init_py.html +387 -0
  55. golf/examples/basic/htmlcov/z_6cadab9ec0df475d_run_py.html +222 -0
  56. golf/examples/basic/htmlcov/z_6fcdee0582ba84e4___init___py.html +106 -0
  57. golf/examples/basic/htmlcov/z_6fcdee0582ba84e4__endpoints_fallback_py.html +107 -0
  58. golf/examples/basic/htmlcov/z_7ba499ed22986217___init___py.html +98 -0
  59. golf/examples/basic/htmlcov/z_7ba499ed22986217_builder_auth_py.html +306 -0
  60. golf/examples/basic/htmlcov/z_7ba499ed22986217_builder_metrics_py.html +329 -0
  61. golf/examples/basic/htmlcov/z_7ba499ed22986217_builder_py.html +1471 -0
  62. golf/examples/basic/htmlcov/z_7ba499ed22986217_builder_telemetry_py.html +186 -0
  63. golf/examples/basic/htmlcov/z_7ba499ed22986217_config_py.html +315 -0
  64. golf/examples/basic/htmlcov/z_7ba499ed22986217_parser_py.html +1149 -0
  65. golf/examples/basic/htmlcov/z_7ba499ed22986217_platform_py.html +279 -0
  66. golf/examples/basic/htmlcov/z_7ba499ed22986217_telemetry_py.html +589 -0
  67. golf/examples/basic/htmlcov/z_7ba499ed22986217_transformer_py.html +286 -0
  68. golf/examples/basic/htmlcov/z_7d7da37693a43688___init___py.html +107 -0
  69. golf/examples/basic/htmlcov/z_7d7da37693a43688_collector_py.html +417 -0
  70. golf/examples/basic/htmlcov/z_7d7da37693a43688_registry_py.html +109 -0
  71. golf/examples/basic/htmlcov/z_abe733142b40ad4e___init___py.html +109 -0
  72. golf/examples/basic/htmlcov/z_abe733142b40ad4e_context_py.html +150 -0
  73. golf/examples/basic/htmlcov/z_abe733142b40ad4e_elicitation_py.html +267 -0
  74. golf/examples/basic/htmlcov/z_abe733142b40ad4e_sampling_py.html +318 -0
  75. golf/examples/basic/prompts/__pycache__/welcome.cpython-311.pyc +0 -0
  76. golf/examples/basic/prompts/welcome.py +3 -5
  77. golf/examples/basic/resources/__pycache__/current_time.cpython-311.pyc +0 -0
  78. golf/examples/basic/resources/__pycache__/info.cpython-311.pyc +0 -0
  79. golf/examples/basic/resources/current_time.py +5 -13
  80. golf/examples/basic/resources/weather/__pycache__/common.cpython-311.pyc +0 -0
  81. golf/examples/basic/resources/weather/__pycache__/current.cpython-311.pyc +0 -0
  82. golf/examples/basic/resources/weather/__pycache__/forecast.cpython-311.pyc +0 -0
  83. golf/examples/basic/resources/weather/city.py +46 -0
  84. golf/examples/basic/resources/weather/common.py +4 -11
  85. golf/examples/basic/resources/weather/current.py +5 -5
  86. golf/examples/basic/resources/weather/forecast.py +5 -5
  87. golf/examples/basic/tools/__pycache__/calculator.cpython-311.pyc +0 -0
  88. golf/examples/basic/tools/calculator.py +94 -0
  89. golf/examples/basic/tools/say/__pycache__/hello.cpython-311.pyc +0 -0
  90. golf/examples/basic/tools/say/hello.py +65 -0
  91. golf/metrics/collector.py +100 -19
  92. golf/telemetry/__init__.py +4 -0
  93. golf/telemetry/instrumentation.py +484 -178
  94. golf/utilities/__init__.py +12 -0
  95. golf/utilities/context.py +53 -0
  96. golf/utilities/elicitation.py +170 -0
  97. golf/utilities/sampling.py +221 -0
  98. {golf_mcp-0.1.20.dist-info → golf_mcp-0.2.1.dist-info}/METADATA +51 -104
  99. golf_mcp-0.2.1.dist-info/RECORD +110 -0
  100. golf/auth/oauth.py +0 -861
  101. golf/auth/provider.py +0 -115
  102. golf/examples/api_key/.env +0 -2
  103. golf/examples/api_key/.env.example +0 -1
  104. golf/examples/api_key/README.md +0 -84
  105. golf/examples/api_key/golf.json +0 -8
  106. golf/examples/api_key/pre_build.py +0 -11
  107. golf/examples/api_key/tools/issues/create.py +0 -93
  108. golf/examples/api_key/tools/issues/list.py +0 -92
  109. golf/examples/api_key/tools/repos/list.py +0 -111
  110. golf/examples/api_key/tools/search/code.py +0 -106
  111. golf/examples/api_key/tools/users/get.py +0 -82
  112. golf/examples/basic/.env +0 -5
  113. golf/examples/basic/pre_build.py +0 -28
  114. golf/examples/basic/tools/github_user.py +0 -65
  115. golf/examples/basic/tools/hello.py +0 -34
  116. golf/examples/basic/tools/payments/charge.py +0 -70
  117. golf/examples/basic/tools/payments/common.py +0 -36
  118. golf/examples/basic/tools/payments/refund.py +0 -61
  119. golf_mcp-0.1.20.dist-info/RECORD +0 -60
  120. {golf_mcp-0.1.20.dist-info → golf_mcp-0.2.1.dist-info}/WHEEL +0 -0
  121. {golf_mcp-0.1.20.dist-info → golf_mcp-0.2.1.dist-info}/entry_points.txt +0 -0
  122. {golf_mcp-0.1.20.dist-info → golf_mcp-0.2.1.dist-info}/licenses/LICENSE +0 -0
  123. {golf_mcp-0.1.20.dist-info → golf_mcp-0.2.1.dist-info}/top_level.txt +0 -0
golf/auth/providers.py ADDED
@@ -0,0 +1,446 @@
1
+ """Modern authentication provider configurations for Golf MCP servers.
2
+
3
+ This module provides configuration classes for FastMCP 2.11+ authentication providers,
4
+ replacing the legacy custom OAuth implementation with the new built-in auth system.
5
+ """
6
+
7
+ import os
8
+ from typing import Any, Literal
9
+ from urllib.parse import urlparse
10
+
11
+ from pydantic import BaseModel, Field, field_validator, model_validator
12
+
13
+
14
+ class JWTAuthConfig(BaseModel):
15
+ """Configuration for JWT token verification using FastMCP's JWTVerifier.
16
+
17
+ Use this when you have JWT tokens issued by an external OAuth server
18
+ (like Auth0, Okta, etc.) and want to verify them in your Golf server.
19
+
20
+ Security Note:
21
+ For production use, it's strongly recommended to specify both `issuer` and `audience`
22
+ to ensure tokens are validated against the expected issuer and intended audience.
23
+ This prevents token misuse across different services or environments.
24
+ """
25
+
26
+ provider_type: Literal["jwt"] = "jwt"
27
+
28
+ # JWT verification settings
29
+ public_key: str | None = Field(None, description="PEM-encoded public key for JWT verification")
30
+ jwks_uri: str | None = Field(None, description="URI to fetch JSON Web Key Set for verification")
31
+ issuer: str | None = Field(None, description="Expected JWT issuer claim (strongly recommended for production)")
32
+ audience: str | list[str] | None = Field(
33
+ None, description="Expected JWT audience claim(s) (strongly recommended for production)"
34
+ )
35
+ algorithm: str = Field("RS256", description="JWT signing algorithm")
36
+
37
+ # Scope and access control
38
+ required_scopes: list[str] = Field(default_factory=list, description="Scopes required for all requests")
39
+
40
+ # Environment variable names for runtime configuration
41
+ public_key_env_var: str | None = Field(None, description="Environment variable name for public key")
42
+ jwks_uri_env_var: str | None = Field(None, description="Environment variable name for JWKS URI")
43
+ issuer_env_var: str | None = Field(None, description="Environment variable name for issuer")
44
+ audience_env_var: str | None = Field(None, description="Environment variable name for audience")
45
+
46
+ @model_validator(mode="after")
47
+ def validate_jwt_config(self) -> "JWTAuthConfig":
48
+ """Validate JWT configuration requirements."""
49
+ # Ensure exactly one of public_key or jwks_uri is provided
50
+ if not self.public_key and not self.jwks_uri and not self.public_key_env_var and not self.jwks_uri_env_var:
51
+ raise ValueError("Either public_key, jwks_uri, or their environment variable equivalents must be provided")
52
+
53
+ if (self.public_key or self.public_key_env_var) and (self.jwks_uri or self.jwks_uri_env_var):
54
+ raise ValueError("Provide either public_key or jwks_uri (or their env vars), not both")
55
+
56
+ # Warn about missing issuer/audience in production-like environments
57
+ is_production = (
58
+ os.environ.get("GOLF_ENV", "").lower() in ("prod", "production")
59
+ or os.environ.get("NODE_ENV", "").lower() == "production"
60
+ or os.environ.get("ENVIRONMENT", "").lower() in ("prod", "production")
61
+ )
62
+
63
+ if is_production:
64
+ missing_fields = []
65
+ if not self.issuer and not self.issuer_env_var:
66
+ missing_fields.append("issuer")
67
+ if not self.audience and not self.audience_env_var:
68
+ missing_fields.append("audience")
69
+
70
+ if missing_fields:
71
+ import warnings
72
+
73
+ warnings.warn(
74
+ f"JWT configuration is missing recommended fields for production: {', '.join(missing_fields)}. "
75
+ "This may allow tokens from unintended issuers or audiences to be accepted.",
76
+ UserWarning,
77
+ stacklevel=2,
78
+ )
79
+
80
+ return self
81
+
82
+
83
+ class StaticTokenConfig(BaseModel):
84
+ """Configuration for static token verification for development/testing.
85
+
86
+ Use this for local development and testing when you need predictable
87
+ API keys without setting up a full OAuth server.
88
+
89
+ WARNING: Never use in production!
90
+ """
91
+
92
+ provider_type: Literal["static"] = "static"
93
+
94
+ # Static tokens mapping: token_string -> metadata
95
+ tokens: dict[str, dict[str, Any]] = Field(
96
+ default_factory=dict,
97
+ description="Static tokens with their metadata (client_id, scopes, expires_at)",
98
+ )
99
+
100
+ # Scope and access control
101
+ required_scopes: list[str] = Field(default_factory=list, description="Scopes required for all requests")
102
+
103
+
104
+ class OAuthServerConfig(BaseModel):
105
+ """Configuration for full OAuth authorization server using FastMCP's OAuthProvider.
106
+
107
+ Use this when you want your Golf server to act as a complete OAuth server,
108
+ handling authorization flows and token issuance.
109
+
110
+ Security Considerations:
111
+ - URLs are validated to prevent SSRF attacks
112
+ - Scopes are validated against OAuth 2.0 standards
113
+ - Base URL must use HTTPS in production environments
114
+ - Client registration is disabled for security
115
+ """
116
+
117
+ provider_type: Literal["oauth_server"] = "oauth_server"
118
+
119
+ # OAuth server URLs
120
+ base_url: str = Field(..., description="Public URL of this Golf server (must use HTTPS in production)")
121
+ issuer_url: str | None = Field(None, description="OAuth issuer URL (defaults to base_url, must be HTTPS)")
122
+ service_documentation_url: str | None = Field(None, description="URL of service documentation")
123
+
124
+ # Client registration settings
125
+ valid_scopes: list[str] = Field(
126
+ default_factory=list, description="Valid scopes for client registration (OAuth 2.0 format)"
127
+ )
128
+ default_scopes: list[str] = Field(default_factory=list, description="Default scopes for new clients")
129
+
130
+ # Token revocation settings
131
+ allow_token_revocation: bool = Field(True, description="Allow token revocation")
132
+
133
+ # Access control
134
+ required_scopes: list[str] = Field(default_factory=list, description="Scopes required for all requests")
135
+
136
+ # Environment variable names for runtime configuration
137
+ base_url_env_var: str | None = Field(None, description="Environment variable name for base URL")
138
+
139
+ @field_validator("base_url")
140
+ @classmethod
141
+ def validate_base_url(cls, v: str) -> str:
142
+ """Validate base URL for security and format compliance."""
143
+ if not v or not v.strip():
144
+ raise ValueError("base_url cannot be empty")
145
+
146
+ url = v.strip()
147
+ try:
148
+ parsed = urlparse(url)
149
+ if not parsed.scheme or not parsed.netloc:
150
+ raise ValueError(f"Invalid base URL format: '{url}' - must include scheme and netloc")
151
+
152
+ if parsed.scheme not in ("http", "https"):
153
+ raise ValueError(f"Base URL must use http or https scheme: '{url}'")
154
+
155
+ # Warn about HTTP in production-like environments
156
+ is_production = (
157
+ os.environ.get("GOLF_ENV", "").lower() in ("prod", "production")
158
+ or os.environ.get("NODE_ENV", "").lower() == "production"
159
+ or os.environ.get("ENVIRONMENT", "").lower() in ("prod", "production")
160
+ )
161
+
162
+ if is_production and parsed.scheme == "http":
163
+ import warnings
164
+
165
+ warnings.warn(
166
+ f"Base URL '{url}' uses HTTP in production environment. "
167
+ "HTTPS is strongly recommended for OAuth servers to prevent token interception.",
168
+ UserWarning,
169
+ stacklevel=2,
170
+ )
171
+
172
+ # Prevent common SSRF targets
173
+ if parsed.hostname in ("localhost", "127.0.0.1", "0.0.0.0"):
174
+ if is_production:
175
+ raise ValueError(f"Base URL cannot use localhost/loopback addresses in production: '{url}'")
176
+
177
+ except Exception as e:
178
+ if isinstance(e, ValueError):
179
+ raise
180
+ raise ValueError(f"Invalid base URL '{url}': {e}") from e
181
+
182
+ return url
183
+
184
+ @field_validator("issuer_url", "service_documentation_url")
185
+ @classmethod
186
+ def validate_optional_urls(cls, v: str | None) -> str | None:
187
+ """Validate optional URLs for security and format compliance."""
188
+ if not v:
189
+ return v
190
+
191
+ url = v.strip()
192
+ if not url:
193
+ return None
194
+
195
+ try:
196
+ parsed = urlparse(url)
197
+ if not parsed.scheme or not parsed.netloc:
198
+ raise ValueError(f"Invalid URL format: '{url}' - must include scheme and netloc")
199
+
200
+ if parsed.scheme not in ("http", "https"):
201
+ raise ValueError(f"URL must use http or https scheme: '{url}'")
202
+
203
+ # Check for HTTPS requirement in production for issuer URL
204
+ if v == cls.__dict__.get("issuer_url"): # This is the issuer_url field
205
+ is_production = (
206
+ os.environ.get("GOLF_ENV", "").lower() in ("prod", "production")
207
+ or os.environ.get("NODE_ENV", "").lower() == "production"
208
+ or os.environ.get("ENVIRONMENT", "").lower() in ("prod", "production")
209
+ )
210
+
211
+ if is_production and parsed.scheme == "http":
212
+ import warnings
213
+
214
+ warnings.warn(
215
+ f"Issuer URL '{url}' uses HTTP in production. HTTPS is required for OAuth issuer URLs.",
216
+ UserWarning,
217
+ stacklevel=2,
218
+ )
219
+
220
+ except Exception as e:
221
+ if isinstance(e, ValueError):
222
+ raise
223
+ raise ValueError(f"Invalid URL '{url}': {e}") from e
224
+
225
+ return url
226
+
227
+ @field_validator("valid_scopes", "default_scopes", "required_scopes")
228
+ @classmethod
229
+ def validate_scopes(cls, v: list[str]) -> list[str]:
230
+ """Validate OAuth 2.0 scopes format and security."""
231
+ if not v:
232
+ return v
233
+
234
+ valid_scopes = []
235
+ for scope in v:
236
+ scope = scope.strip()
237
+ if not scope:
238
+ raise ValueError("Scopes cannot be empty or whitespace-only")
239
+
240
+ # OAuth 2.0 scope format validation (RFC 6749)
241
+ # Scopes should be ASCII printable characters except space, and no control characters
242
+ if not all(32 < ord(c) < 127 and c not in ' "\\' for c in scope):
243
+ raise ValueError(
244
+ f"Invalid scope format: '{scope}' - must be ASCII printable without spaces, quotes, or backslashes"
245
+ )
246
+
247
+ # Reasonable length limit to prevent abuse
248
+ if len(scope) > 128:
249
+ raise ValueError(f"Scope too long: '{scope}' - maximum 128 characters")
250
+
251
+ # Prevent potentially dangerous scope names
252
+ dangerous_scopes = {"admin", "root", "superuser", "system", "*", "all"}
253
+ if scope.lower() in dangerous_scopes:
254
+ import warnings
255
+
256
+ warnings.warn(
257
+ f"Potentially dangerous scope detected: '{scope}'. "
258
+ "Consider using more specific, principle-of-least-privilege scopes.",
259
+ UserWarning,
260
+ stacklevel=2,
261
+ )
262
+
263
+ valid_scopes.append(scope)
264
+
265
+ return valid_scopes
266
+
267
+ @model_validator(mode="after")
268
+ def validate_oauth_server_config(self) -> "OAuthServerConfig":
269
+ """Validate OAuth server configuration for security and consistency."""
270
+ # Validate default_scopes are subset of valid_scopes
271
+ if self.default_scopes and self.valid_scopes:
272
+ invalid_defaults = set(self.default_scopes) - set(self.valid_scopes)
273
+ if invalid_defaults:
274
+ raise ValueError(f"default_scopes contains invalid scopes not in valid_scopes: {invalid_defaults}")
275
+
276
+ # Validate required_scopes are subset of valid_scopes
277
+ if self.required_scopes and self.valid_scopes:
278
+ invalid_required = set(self.required_scopes) - set(self.valid_scopes)
279
+ if invalid_required:
280
+ raise ValueError(f"required_scopes contains invalid scopes not in valid_scopes: {invalid_required}")
281
+
282
+ return self
283
+
284
+
285
+ class RemoteAuthConfig(BaseModel):
286
+ """Configuration for remote authorization server integration.
287
+
288
+ Use this when you have token verification logic and want to advertise
289
+ the authorization servers that issue valid tokens (RFC 9728 compliance).
290
+ """
291
+
292
+ provider_type: Literal["remote"] = "remote"
293
+
294
+ # Authorization servers that issue tokens
295
+ authorization_servers: list[str] = Field(
296
+ ..., description="List of authorization server URLs that issue valid tokens"
297
+ )
298
+
299
+ # This server's URL
300
+ resource_server_url: str = Field(..., description="URL of this resource server")
301
+
302
+ # Scopes this resource supports (advertised via /.well-known/oauth-protected-resource)
303
+ scopes_supported: list[str] = Field(
304
+ default_factory=list,
305
+ description="Scopes this resource supports (advertised via /.well-known/oauth-protected-resource)",
306
+ )
307
+
308
+ # Token verification (delegate to another config)
309
+ token_verifier_config: JWTAuthConfig | StaticTokenConfig = Field(
310
+ ..., description="Configuration for the underlying token verifier"
311
+ )
312
+
313
+ # Environment variable names for runtime configuration
314
+ authorization_servers_env_var: str | None = Field(
315
+ None, description="Environment variable name for comma-separated authorization server URLs"
316
+ )
317
+ resource_server_url_env_var: str | None = Field(
318
+ None, description="Environment variable name for resource server URL"
319
+ )
320
+
321
+ @field_validator("authorization_servers")
322
+ @classmethod
323
+ def validate_authorization_servers(cls, v: list[str]) -> list[str]:
324
+ """Validate authorization servers are non-empty and valid URLs."""
325
+ if not v:
326
+ raise ValueError(
327
+ "authorization_servers cannot be empty - at least one authorization server URL is required"
328
+ )
329
+
330
+ valid_urls = []
331
+ for url in v:
332
+ url = url.strip()
333
+ if not url:
334
+ raise ValueError("authorization_servers cannot contain empty URLs")
335
+
336
+ # Validate URL format
337
+ try:
338
+ parsed = urlparse(url)
339
+ if not parsed.scheme or not parsed.netloc:
340
+ raise ValueError(
341
+ f"Invalid URL format for authorization server: '{url}' - must include scheme and netloc"
342
+ )
343
+ if parsed.scheme not in ("http", "https"):
344
+ raise ValueError(f"Authorization server URL must use http or https scheme: '{url}'")
345
+ except Exception as e:
346
+ raise ValueError(f"Invalid authorization server URL '{url}': {e}") from e
347
+
348
+ valid_urls.append(url)
349
+
350
+ return valid_urls
351
+
352
+ @field_validator("resource_server_url")
353
+ @classmethod
354
+ def validate_resource_server_url(cls, v: str) -> str:
355
+ """Validate resource server URL is a valid URL."""
356
+ if not v or not v.strip():
357
+ raise ValueError("resource_server_url cannot be empty")
358
+
359
+ url = v.strip()
360
+ try:
361
+ parsed = urlparse(url)
362
+ if not parsed.scheme or not parsed.netloc:
363
+ raise ValueError(f"Invalid URL format for resource server: '{url}' - must include scheme and netloc")
364
+ if parsed.scheme not in ("http", "https"):
365
+ raise ValueError(f"Resource server URL must use http or https scheme: '{url}'")
366
+ except Exception as e:
367
+ raise ValueError(f"Invalid resource server URL '{url}': {e}") from e
368
+
369
+ return url
370
+
371
+ @field_validator("scopes_supported")
372
+ @classmethod
373
+ def validate_scopes_supported(cls, v: list[str]) -> list[str]:
374
+ """Validate scopes_supported format and security."""
375
+ if not v:
376
+ return v
377
+
378
+ cleaned_scopes = []
379
+ for scope in v:
380
+ scope = scope.strip()
381
+ if not scope:
382
+ raise ValueError("Scopes cannot be empty or whitespace-only")
383
+
384
+ # OAuth 2.0 scope format validation (RFC 6749)
385
+ if not all(32 < ord(c) < 127 and c not in ' "\\' for c in scope):
386
+ raise ValueError(
387
+ f"Invalid scope format: '{scope}' - must be ASCII printable without spaces, quotes, or backslashes"
388
+ )
389
+
390
+ # Reasonable length limit to prevent abuse
391
+ if len(scope) > 128:
392
+ raise ValueError(f"Scope too long: '{scope}' - maximum 128 characters")
393
+
394
+ # Warn about potentially dangerous scope names
395
+ dangerous_scopes = {"admin", "root", "superuser", "system", "*", "all"}
396
+ if scope.lower() in dangerous_scopes:
397
+ import warnings
398
+
399
+ warnings.warn(
400
+ f"Potentially dangerous scope detected: '{scope}'. "
401
+ "Consider using more specific, principle-of-least-privilege scopes.",
402
+ UserWarning,
403
+ stacklevel=2,
404
+ )
405
+
406
+ cleaned_scopes.append(scope)
407
+
408
+ return cleaned_scopes
409
+
410
+ @model_validator(mode="after")
411
+ def validate_token_verifier_compatibility(self) -> "RemoteAuthConfig":
412
+ """Validate that the token verifier config is compatible with token verification."""
413
+ # The duck-typing check is already handled by the factory function, but we can
414
+ # add a basic sanity check here that the config types are ones we know work
415
+ config = self.token_verifier_config
416
+
417
+ if not isinstance(config, JWTAuthConfig | StaticTokenConfig):
418
+ raise ValueError(
419
+ f"token_verifier_config must be JWTAuthConfig or StaticTokenConfig, got {type(config).__name__}"
420
+ )
421
+
422
+ # For JWT configs, ensure they have the minimum required fields
423
+ if isinstance(config, JWTAuthConfig) and (
424
+ not config.public_key
425
+ and not config.jwks_uri
426
+ and not config.public_key_env_var
427
+ and not config.jwks_uri_env_var
428
+ ):
429
+ raise ValueError(
430
+ "JWT token verifier config must provide public_key, jwks_uri, or their environment variable equivalents"
431
+ )
432
+
433
+ # For static token configs, ensure they have tokens
434
+ if isinstance(config, StaticTokenConfig) and not config.tokens:
435
+ raise ValueError("Static token verifier config must provide at least one token")
436
+
437
+ # Convenience: if user didn't set scopes_supported, default to verifier.required_scopes
438
+ if not self.scopes_supported:
439
+ if hasattr(config, "required_scopes") and config.required_scopes:
440
+ self.scopes_supported = list(config.required_scopes)
441
+
442
+ return self
443
+
444
+
445
+ # Union type for all auth configurations
446
+ AuthConfig = JWTAuthConfig | StaticTokenConfig | OAuthServerConfig | RemoteAuthConfig