d365fo-client 0.2.4__py3-none-any.whl → 0.3.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.
Files changed (59) hide show
  1. d365fo_client/__init__.py +7 -1
  2. d365fo_client/auth.py +9 -21
  3. d365fo_client/cli.py +25 -13
  4. d365fo_client/client.py +8 -4
  5. d365fo_client/config.py +52 -30
  6. d365fo_client/credential_sources.py +5 -0
  7. d365fo_client/main.py +1 -1
  8. d365fo_client/mcp/__init__.py +3 -1
  9. d365fo_client/mcp/auth_server/__init__.py +5 -0
  10. d365fo_client/mcp/auth_server/auth/__init__.py +30 -0
  11. d365fo_client/mcp/auth_server/auth/auth.py +372 -0
  12. d365fo_client/mcp/auth_server/auth/oauth_proxy.py +989 -0
  13. d365fo_client/mcp/auth_server/auth/providers/__init__.py +0 -0
  14. d365fo_client/mcp/auth_server/auth/providers/apikey.py +83 -0
  15. d365fo_client/mcp/auth_server/auth/providers/azure.py +393 -0
  16. d365fo_client/mcp/auth_server/auth/providers/bearer.py +25 -0
  17. d365fo_client/mcp/auth_server/auth/providers/jwt.py +547 -0
  18. d365fo_client/mcp/auth_server/auth/redirect_validation.py +65 -0
  19. d365fo_client/mcp/auth_server/dependencies.py +136 -0
  20. d365fo_client/mcp/client_manager.py +16 -67
  21. d365fo_client/mcp/fastmcp_main.py +407 -0
  22. d365fo_client/mcp/fastmcp_server.py +598 -0
  23. d365fo_client/mcp/fastmcp_utils.py +431 -0
  24. d365fo_client/mcp/main.py +40 -13
  25. d365fo_client/mcp/mixins/__init__.py +24 -0
  26. d365fo_client/mcp/mixins/base_tools_mixin.py +55 -0
  27. d365fo_client/mcp/mixins/connection_tools_mixin.py +50 -0
  28. d365fo_client/mcp/mixins/crud_tools_mixin.py +311 -0
  29. d365fo_client/mcp/mixins/database_tools_mixin.py +685 -0
  30. d365fo_client/mcp/mixins/label_tools_mixin.py +87 -0
  31. d365fo_client/mcp/mixins/metadata_tools_mixin.py +565 -0
  32. d365fo_client/mcp/mixins/performance_tools_mixin.py +109 -0
  33. d365fo_client/mcp/mixins/profile_tools_mixin.py +713 -0
  34. d365fo_client/mcp/mixins/sync_tools_mixin.py +321 -0
  35. d365fo_client/mcp/prompts/action_execution.py +1 -1
  36. d365fo_client/mcp/prompts/sequence_analysis.py +1 -1
  37. d365fo_client/mcp/tools/crud_tools.py +3 -3
  38. d365fo_client/mcp/tools/sync_tools.py +1 -1
  39. d365fo_client/mcp/utilities/__init__.py +1 -0
  40. d365fo_client/mcp/utilities/auth.py +34 -0
  41. d365fo_client/mcp/utilities/logging.py +58 -0
  42. d365fo_client/mcp/utilities/types.py +426 -0
  43. d365fo_client/metadata_v2/sync_manager_v2.py +2 -0
  44. d365fo_client/metadata_v2/sync_session_manager.py +7 -7
  45. d365fo_client/models.py +139 -139
  46. d365fo_client/output.py +2 -2
  47. d365fo_client/profile_manager.py +62 -27
  48. d365fo_client/profiles.py +118 -113
  49. d365fo_client/settings.py +367 -0
  50. d365fo_client/sync_models.py +85 -2
  51. d365fo_client/utils.py +2 -1
  52. {d365fo_client-0.2.4.dist-info → d365fo_client-0.3.1.dist-info}/METADATA +273 -18
  53. d365fo_client-0.3.1.dist-info/RECORD +85 -0
  54. d365fo_client-0.3.1.dist-info/entry_points.txt +4 -0
  55. d365fo_client-0.2.4.dist-info/RECORD +0 -56
  56. d365fo_client-0.2.4.dist-info/entry_points.txt +0 -3
  57. {d365fo_client-0.2.4.dist-info → d365fo_client-0.3.1.dist-info}/WHEEL +0 -0
  58. {d365fo_client-0.2.4.dist-info → d365fo_client-0.3.1.dist-info}/licenses/LICENSE +0 -0
  59. {d365fo_client-0.2.4.dist-info → d365fo_client-0.3.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,989 @@
1
+ """OAuth Proxy Provider for FastMCP.
2
+
3
+ This provider acts as a transparent proxy to an upstream OAuth Authorization Server,
4
+ handling Dynamic Client Registration locally while forwarding all other OAuth flows.
5
+ This enables authentication with upstream providers that don't support DCR or have
6
+ restricted client registration policies.
7
+
8
+ Key features:
9
+ - Proxies authorization and token endpoints to upstream server
10
+ - Implements local Dynamic Client Registration with fixed upstream credentials
11
+ - Validates tokens using upstream JWKS
12
+ - Maintains minimal local state for bookkeeping
13
+ - Enhanced logging with request correlation
14
+
15
+ This implementation is based on the OAuth 2.1 specification and is designed for
16
+ production use with enterprise identity providers.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import hashlib
22
+ import secrets
23
+ import time
24
+ from base64 import urlsafe_b64encode
25
+ from typing import TYPE_CHECKING, Any, Final
26
+ from urllib.parse import urlencode
27
+
28
+ import httpx
29
+ from authlib.common.security import generate_token
30
+ from authlib.integrations.httpx_client import AsyncOAuth2Client
31
+ from mcp.server.auth.provider import (
32
+ AccessToken,
33
+ AuthorizationCode,
34
+ AuthorizationParams,
35
+ RefreshToken,
36
+ TokenError,
37
+ )
38
+ from mcp.server.auth.settings import (
39
+ ClientRegistrationOptions,
40
+ RevocationOptions,
41
+ )
42
+ from mcp.shared.auth import OAuthClientInformationFull, OAuthToken
43
+ from pydantic import AnyHttpUrl, AnyUrl, SecretStr
44
+ from starlette.requests import Request
45
+ from starlette.responses import RedirectResponse
46
+ from starlette.routing import Route
47
+
48
+ from .auth import OAuthProvider
49
+ from .auth import TokenVerifier
50
+ from .redirect_validation import validate_redirect_uri
51
+ from d365fo_client.mcp.utilities.logging import get_logger
52
+
53
+ if TYPE_CHECKING:
54
+ pass
55
+
56
+ logger = get_logger(__name__)
57
+
58
+
59
+ class ProxyDCRClient(OAuthClientInformationFull):
60
+ """Client for DCR proxy with configurable redirect URI validation.
61
+
62
+ This special client class is critical for the OAuth proxy to work correctly
63
+ with Dynamic Client Registration (DCR). Here's why it exists:
64
+
65
+ Problem:
66
+ --------
67
+ When MCP clients use OAuth, they dynamically register with random localhost
68
+ ports (e.g., http://localhost:55454/callback). The OAuth proxy needs to:
69
+ 1. Accept these dynamic redirect URIs from clients based on configured patterns
70
+ 2. Use its own fixed redirect URI with the upstream provider (Google, GitHub, etc.)
71
+ 3. Forward the authorization code back to the client's dynamic URI
72
+
73
+ Solution:
74
+ ---------
75
+ This class validates redirect URIs against configurable patterns,
76
+ while the proxy internally uses its own fixed redirect URI with the upstream
77
+ provider. This allows the flow to work even when clients reconnect with
78
+ different ports or when tokens are cached.
79
+
80
+ Without proper validation, clients could get "Redirect URI not registered" errors
81
+ when trying to authenticate with cached tokens, or security vulnerabilities could
82
+ arise from accepting arbitrary redirect URIs.
83
+ """
84
+
85
+ def __init__(
86
+ self, *args, allowed_redirect_uri_patterns: list[str] | None = None, **kwargs
87
+ ):
88
+ """Initialize with allowed redirect URI patterns.
89
+
90
+ Args:
91
+ allowed_redirect_uri_patterns: List of allowed redirect URI patterns with wildcard support.
92
+ If None, defaults to localhost-only patterns.
93
+ If empty list, allows all redirect URIs.
94
+ """
95
+ super().__init__(*args, **kwargs)
96
+ self._allowed_redirect_uri_patterns = allowed_redirect_uri_patterns
97
+
98
+ def validate_redirect_uri(self, redirect_uri: AnyUrl | None) -> AnyUrl:
99
+ """Validate redirect URI against allowed patterns.
100
+
101
+ Since we're acting as a proxy and clients register dynamically,
102
+ we validate their redirect URIs against configurable patterns.
103
+ This is essential for cached token scenarios where the client may
104
+ reconnect with a different port.
105
+ """
106
+ if redirect_uri is not None:
107
+ # Validate against allowed patterns
108
+ if validate_redirect_uri(redirect_uri, self._allowed_redirect_uri_patterns):
109
+ return redirect_uri
110
+ # Fall back to normal validation if not in allowed patterns
111
+ return super().validate_redirect_uri(redirect_uri)
112
+ # If no redirect_uri provided, use default behavior
113
+ return super().validate_redirect_uri(redirect_uri)
114
+
115
+
116
+ # Default token expiration times
117
+ DEFAULT_ACCESS_TOKEN_EXPIRY_SECONDS: Final[int] = 60 * 60 # 1 hour
118
+ DEFAULT_AUTH_CODE_EXPIRY_SECONDS: Final[int] = 5 * 60 # 5 minutes
119
+
120
+ # HTTP client timeout
121
+ HTTP_TIMEOUT_SECONDS: Final[int] = 30
122
+
123
+
124
+ class OAuthProxy(OAuthProvider):
125
+ """OAuth provider that presents a DCR-compliant interface while proxying to non-DCR IDPs.
126
+
127
+ Purpose
128
+ -------
129
+ MCP clients expect OAuth providers to support Dynamic Client Registration (DCR),
130
+ where clients can register themselves dynamically and receive unique credentials.
131
+ Most enterprise IDPs (Google, GitHub, Azure AD, etc.) don't support DCR and require
132
+ pre-registered OAuth applications with fixed credentials.
133
+
134
+ This proxy bridges that gap by:
135
+ - Presenting a full DCR-compliant OAuth interface to MCP clients
136
+ - Translating DCR registration requests to use pre-configured upstream credentials
137
+ - Proxying all OAuth flows to the upstream IDP with appropriate translations
138
+ - Managing the state and security requirements of both protocols
139
+
140
+ Architecture Overview
141
+ --------------------
142
+ The proxy maintains a single OAuth app registration with the upstream provider
143
+ while allowing unlimited MCP clients to register and authenticate dynamically.
144
+ It implements the complete OAuth 2.1 + DCR specification for clients while
145
+ translating to whatever OAuth variant the upstream provider requires.
146
+
147
+ Key Translation Challenges Solved
148
+ ---------------------------------
149
+ 1. Dynamic Client Registration:
150
+ - MCP clients expect to register dynamically and get unique credentials
151
+ - Upstream IDPs require pre-registered apps with fixed credentials
152
+ - Solution: Accept DCR requests, return shared upstream credentials
153
+
154
+ 2. Dynamic Redirect URIs:
155
+ - MCP clients use random localhost ports that change between sessions
156
+ - Upstream IDPs require fixed, pre-registered redirect URIs
157
+ - Solution: Use proxy's fixed callback URL with upstream, forward to client's dynamic URI
158
+
159
+ 3. Authorization Code Mapping:
160
+ - Upstream returns codes for the proxy's redirect URI
161
+ - Clients expect codes for their own redirect URIs
162
+ - Solution: Exchange upstream code server-side, issue new code to client
163
+
164
+ 4. State Parameter Collision:
165
+ - Both client and proxy need to maintain state through the flow
166
+ - Only one state parameter available in OAuth
167
+ - Solution: Use transaction ID as state with upstream, preserve client's state
168
+
169
+ 5. Token Management:
170
+ - Clients may expect different token formats/claims than upstream provides
171
+ - Need to track tokens for revocation and refresh
172
+ - Solution: Store token relationships, forward upstream tokens transparently
173
+
174
+ OAuth Flow Implementation
175
+ ------------------------
176
+ 1. Client Registration (DCR):
177
+ - Accept any client registration request
178
+ - Store ProxyDCRClient that accepts dynamic redirect URIs
179
+
180
+ 2. Authorization:
181
+ - Store transaction mapping client details to proxy flow
182
+ - Redirect to upstream with proxy's fixed redirect URI
183
+ - Use transaction ID as state parameter with upstream
184
+
185
+ 3. Upstream Callback:
186
+ - Exchange upstream authorization code for tokens (server-side)
187
+ - Generate new authorization code bound to client's PKCE challenge
188
+ - Redirect to client's original dynamic redirect URI
189
+
190
+ 4. Token Exchange:
191
+ - Validate client's code and PKCE verifier
192
+ - Return previously obtained upstream tokens
193
+ - Clean up one-time use authorization code
194
+
195
+ 5. Token Refresh:
196
+ - Forward refresh requests to upstream using authlib
197
+ - Handle token rotation if upstream issues new refresh token
198
+ - Update local token mappings
199
+
200
+ State Management
201
+ ---------------
202
+ The proxy maintains minimal but crucial state:
203
+ - _clients: DCR registrations (all use ProxyDCRClient for flexibility)
204
+ - _oauth_transactions: Active authorization flows with client context
205
+ - _client_codes: Authorization codes with PKCE challenges and upstream tokens
206
+ - _access_tokens, _refresh_tokens: Token storage for revocation
207
+ - Token relationship mappings for cleanup and rotation
208
+
209
+ Security Considerations
210
+ ----------------------
211
+ - PKCE enforced end-to-end (client to proxy, proxy to upstream)
212
+ - Authorization codes are single-use with short expiry
213
+ - Transaction IDs are cryptographically random
214
+ - All state is cleaned up after use to prevent replay
215
+ - Token validation delegates to upstream provider
216
+
217
+ Provider Compatibility
218
+ ---------------------
219
+ Works with any OAuth 2.0 provider that supports:
220
+ - Authorization code flow
221
+ - Fixed redirect URI (configured in provider's app settings)
222
+ - Standard token endpoint
223
+
224
+ Handles provider-specific requirements:
225
+ - Google: Ensures minimum scope requirements
226
+ - GitHub: Compatible with OAuth Apps and GitHub Apps
227
+ - Azure AD: Handles tenant-specific endpoints
228
+ - Generic: Works with any spec-compliant provider
229
+ """
230
+
231
+ def __init__(
232
+ self,
233
+ *,
234
+ # Upstream server configuration
235
+ upstream_authorization_endpoint: str,
236
+ upstream_token_endpoint: str,
237
+ upstream_client_id: str,
238
+ upstream_client_secret: str,
239
+ upstream_revocation_endpoint: str | None = None,
240
+ # Token validation
241
+ token_verifier: TokenVerifier,
242
+ # FastMCP server configuration
243
+ base_url: AnyHttpUrl | str,
244
+ redirect_path: str = "/auth/callback",
245
+ issuer_url: AnyHttpUrl | str | None = None,
246
+ service_documentation_url: AnyHttpUrl | str | None = None,
247
+ # Client redirect URI validation
248
+ allowed_client_redirect_uris: list[str] | None = None,
249
+ valid_scopes: list[str] | None = None,
250
+ # PKCE configuration
251
+ forward_pkce: bool = True,
252
+ # Token endpoint authentication
253
+ token_endpoint_auth_method: str | None = None,
254
+ # Extra parameters to forward to authorization endpoint
255
+ extra_authorize_params: dict[str, str] | None = None,
256
+ # Extra parameters to forward to token endpoint
257
+ extra_token_params: dict[str, str] | None = None,
258
+ ):
259
+ """Initialize the OAuth proxy provider.
260
+
261
+ Args:
262
+ upstream_authorization_endpoint: URL of upstream authorization endpoint
263
+ upstream_token_endpoint: URL of upstream token endpoint
264
+ upstream_client_id: Client ID registered with upstream server
265
+ upstream_client_secret: Client secret for upstream server
266
+ upstream_revocation_endpoint: Optional upstream revocation endpoint
267
+ token_verifier: Token verifier for validating access tokens
268
+ base_url: Public URL of the server that exposes this FastMCP server; redirect path is
269
+ relative to this URL
270
+ redirect_path: Redirect path configured in upstream OAuth app (defaults to "/auth/callback")
271
+ issuer_url: Issuer URL for OAuth metadata (defaults to base_url)
272
+ service_documentation_url: Optional service documentation URL
273
+ allowed_client_redirect_uris: List of allowed redirect URI patterns for MCP clients.
274
+ Patterns support wildcards (e.g., "http://localhost:*", "https://*.example.com/*").
275
+ If None (default), only localhost redirect URIs are allowed.
276
+ If empty list, all redirect URIs are allowed (not recommended for production).
277
+ These are for MCP clients performing loopback redirects, NOT for the upstream OAuth app.
278
+ valid_scopes: List of all the possible valid scopes for a client.
279
+ These are advertised to clients through the `/.well-known` endpoints. Defaults to `required_scopes` if not provided.
280
+ forward_pkce: Whether to forward PKCE to upstream server (default True).
281
+ Enable for providers that support/require PKCE (Google, Azure, etc.).
282
+ Disable only if upstream provider doesn't support PKCE.
283
+ token_endpoint_auth_method: Token endpoint authentication method for upstream server.
284
+ Common values: "client_secret_basic", "client_secret_post", "none".
285
+ If None, authlib will use its default (typically "client_secret_basic").
286
+ extra_authorize_params: Additional parameters to forward to the upstream authorization endpoint.
287
+ Useful for provider-specific parameters like Auth0's "audience".
288
+ Example: {"audience": "https://api.example.com"}
289
+ extra_token_params: Additional parameters to forward to the upstream token endpoint.
290
+ Useful for provider-specific parameters during token exchange.
291
+ """
292
+ # Always enable DCR since we implement it locally for MCP clients
293
+ client_registration_options = ClientRegistrationOptions(
294
+ enabled=True,
295
+ valid_scopes=valid_scopes or token_verifier.required_scopes,
296
+ )
297
+
298
+ # Enable revocation only if upstream endpoint provided
299
+ revocation_options = (
300
+ RevocationOptions(enabled=True) if upstream_revocation_endpoint else None
301
+ )
302
+
303
+ super().__init__(
304
+ base_url=base_url,
305
+ issuer_url=issuer_url,
306
+ service_documentation_url=service_documentation_url,
307
+ client_registration_options=client_registration_options,
308
+ revocation_options=revocation_options,
309
+ required_scopes=token_verifier.required_scopes,
310
+ )
311
+
312
+ # Store upstream configuration
313
+ self._upstream_authorization_endpoint = upstream_authorization_endpoint
314
+ self._upstream_token_endpoint = upstream_token_endpoint
315
+ self._upstream_client_id = upstream_client_id
316
+ self._upstream_client_secret = SecretStr(upstream_client_secret)
317
+ self._upstream_revocation_endpoint = upstream_revocation_endpoint
318
+ self._default_scope_str = " ".join(self.required_scopes or [])
319
+
320
+ # Store redirect configuration
321
+ self._redirect_path = (
322
+ redirect_path if redirect_path.startswith("/") else f"/{redirect_path}"
323
+ )
324
+ self._allowed_client_redirect_uris = allowed_client_redirect_uris
325
+
326
+ # PKCE configuration
327
+ self._forward_pkce = forward_pkce
328
+
329
+ # Token endpoint authentication
330
+ self._token_endpoint_auth_method = token_endpoint_auth_method
331
+
332
+ # Extra parameters for authorization and token endpoints
333
+ self._extra_authorize_params = extra_authorize_params or {}
334
+ self._extra_token_params = extra_token_params or {}
335
+
336
+ # Local state for DCR and token bookkeeping
337
+ self._clients: dict[str, OAuthClientInformationFull] = {}
338
+ self._access_tokens: dict[str, AccessToken] = {}
339
+ self._refresh_tokens: dict[str, RefreshToken] = {}
340
+
341
+ # Token relation mappings for cleanup
342
+ self._access_to_refresh: dict[str, str] = {}
343
+ self._refresh_to_access: dict[str, str] = {}
344
+
345
+ # OAuth transaction storage for IdP callback forwarding
346
+ self._oauth_transactions: dict[
347
+ str, dict[str, Any]
348
+ ] = {} # txn_id -> transaction_data
349
+ self._client_codes: dict[str, dict[str, Any]] = {} # client_code -> code_data
350
+
351
+ # Use the provided token validator
352
+ self._token_validator = token_verifier
353
+
354
+ logger.debug(
355
+ "Initialized OAuth proxy provider with upstream server %s",
356
+ self._upstream_authorization_endpoint,
357
+ )
358
+
359
+ # -------------------------------------------------------------------------
360
+ # PKCE Helper Methods
361
+ # -------------------------------------------------------------------------
362
+
363
+ def _generate_pkce_pair(self) -> tuple[str, str]:
364
+ """Generate PKCE code verifier and challenge pair.
365
+
366
+ Returns:
367
+ Tuple of (code_verifier, code_challenge) using S256 method
368
+ """
369
+ # Generate code verifier: 43-128 characters from unreserved set
370
+ code_verifier = generate_token(48)
371
+
372
+ # Generate code challenge using S256 (SHA256 + base64url)
373
+ challenge_bytes = hashlib.sha256(code_verifier.encode()).digest()
374
+ code_challenge = urlsafe_b64encode(challenge_bytes).decode().rstrip("=")
375
+
376
+ return code_verifier, code_challenge
377
+
378
+ # -------------------------------------------------------------------------
379
+ # Client Registration (Local Implementation)
380
+ # -------------------------------------------------------------------------
381
+
382
+ async def get_client(self, client_id: str) -> OAuthClientInformationFull | None:
383
+ """Get client information by ID. This is generally the random ID
384
+ provided to the DCR client during registration, not the upstream client ID.
385
+
386
+ For unregistered clients, returns None (which will raise an error in the SDK).
387
+ """
388
+ client = self._clients.get(client_id)
389
+
390
+ return client
391
+
392
+ async def register_client(self, client_info: OAuthClientInformationFull) -> None:
393
+ """Register a client locally
394
+
395
+ When a client registers, we create a ProxyDCRClient that is more
396
+ forgiving about validating redirect URIs, since the DCR client's
397
+ redirect URI will likely be localhost or unknown to the proxied IDP. The
398
+ proxied IDP only knows about this server's fixed redirect URI.
399
+ """
400
+
401
+ # Create a ProxyDCRClient with configured redirect URI validation
402
+ proxy_client = ProxyDCRClient(
403
+ client_id=client_info.client_id,
404
+ client_secret=client_info.client_secret,
405
+ redirect_uris=client_info.redirect_uris or [AnyUrl("http://localhost")],
406
+ grant_types=client_info.grant_types
407
+ or ["authorization_code", "refresh_token"],
408
+ scope=self._default_scope_str,
409
+ token_endpoint_auth_method="none",
410
+ allowed_redirect_uri_patterns=self._allowed_client_redirect_uris,
411
+ )
412
+
413
+ # Store the ProxyDCRClient
414
+ self._clients[client_info.client_id] = proxy_client
415
+
416
+ # Log redirect URIs to help users discover what patterns they might need
417
+ if client_info.redirect_uris:
418
+ for uri in client_info.redirect_uris:
419
+ logger.debug(
420
+ "Client registered with redirect_uri: %s - if restricting redirect URIs, "
421
+ "ensure this pattern is allowed in allowed_client_redirect_uris",
422
+ uri,
423
+ )
424
+
425
+ logger.debug(
426
+ "Registered client %s with %d redirect URIs",
427
+ client_info.client_id,
428
+ len(proxy_client.redirect_uris),
429
+ )
430
+
431
+ # -------------------------------------------------------------------------
432
+ # Authorization Flow (Proxy to Upstream)
433
+ # -------------------------------------------------------------------------
434
+
435
+ async def authorize(
436
+ self,
437
+ client: OAuthClientInformationFull,
438
+ params: AuthorizationParams,
439
+ ) -> str:
440
+ """Start OAuth transaction and redirect to upstream IdP.
441
+
442
+ This implements the DCR-compliant proxy pattern:
443
+ 1. Store transaction with client details and PKCE challenge
444
+ 2. Generate proxy's own PKCE parameters if forwarding is enabled
445
+ 3. Use transaction ID as state for IdP
446
+ 4. Redirect to IdP with our fixed callback URL and proxy's PKCE
447
+ """
448
+ # Generate transaction ID for this authorization request
449
+ txn_id = secrets.token_urlsafe(32)
450
+
451
+ # Generate proxy's own PKCE parameters if forwarding is enabled
452
+ proxy_code_verifier = None
453
+ proxy_code_challenge = None
454
+ if self._forward_pkce and params.code_challenge:
455
+ proxy_code_verifier, proxy_code_challenge = self._generate_pkce_pair()
456
+ logger.debug(
457
+ "Generated proxy PKCE for transaction %s (forwarding client PKCE to upstream)",
458
+ txn_id,
459
+ )
460
+
461
+ # Store transaction data for IdP callback processing
462
+ transaction_data = {
463
+ "client_id": client.client_id,
464
+ "client_redirect_uri": str(params.redirect_uri),
465
+ "client_state": params.state,
466
+ "code_challenge": params.code_challenge,
467
+ "code_challenge_method": getattr(params, "code_challenge_method", "S256"),
468
+ "scopes": params.scopes or [],
469
+ "created_at": time.time(),
470
+ }
471
+
472
+ # Store proxy's PKCE verifier if we're forwarding
473
+ if proxy_code_verifier:
474
+ transaction_data["proxy_code_verifier"] = proxy_code_verifier
475
+
476
+ self._oauth_transactions[txn_id] = transaction_data
477
+
478
+ # Build query parameters for upstream IdP authorization request
479
+ # Use our fixed IdP callback and transaction ID as state
480
+ query_params: dict[str, Any] = {
481
+ "response_type": "code",
482
+ "client_id": self._upstream_client_id,
483
+ "redirect_uri": f"{str(self.base_url).rstrip('/')}{self._redirect_path}",
484
+ "state": txn_id, # Use txn_id as IdP state
485
+ }
486
+
487
+ # Add scopes - use client scopes or fallback to required scopes
488
+ scopes_to_use = params.scopes or self.required_scopes or []
489
+
490
+ if scopes_to_use:
491
+ query_params["scope"] = " ".join(scopes_to_use)
492
+
493
+ # Forward proxy's PKCE challenge to upstream if enabled
494
+ if proxy_code_challenge:
495
+ query_params["code_challenge"] = proxy_code_challenge
496
+ query_params["code_challenge_method"] = "S256"
497
+ logger.debug(
498
+ "Forwarding proxy PKCE challenge to upstream for transaction %s",
499
+ txn_id,
500
+ )
501
+
502
+ # Forward resource parameter if provided (RFC 8707)
503
+ if params.resource:
504
+ query_params["resource"] = params.resource
505
+ logger.debug(
506
+ "Forwarding resource indicator '%s' to upstream for transaction %s",
507
+ params.resource,
508
+ txn_id,
509
+ )
510
+
511
+ # Add any extra authorization parameters configured for this proxy
512
+ if self._extra_authorize_params:
513
+ query_params.update(self._extra_authorize_params)
514
+ logger.debug(
515
+ "Adding extra authorization parameters for transaction %s: %s",
516
+ txn_id,
517
+ list(self._extra_authorize_params.keys()),
518
+ )
519
+
520
+ # Build the upstream authorization URL
521
+ separator = "&" if "?" in self._upstream_authorization_endpoint else "?"
522
+ upstream_url = f"{self._upstream_authorization_endpoint}{separator}{urlencode(query_params)}"
523
+
524
+ logger.debug(
525
+ "Starting OAuth transaction %s for client %s, redirecting to IdP (PKCE forwarding: %s)",
526
+ txn_id,
527
+ client.client_id,
528
+ "enabled" if proxy_code_challenge else "disabled",
529
+ )
530
+ return upstream_url
531
+
532
+ # -------------------------------------------------------------------------
533
+ # Authorization Code Handling
534
+ # -------------------------------------------------------------------------
535
+
536
+ async def load_authorization_code(
537
+ self,
538
+ client: OAuthClientInformationFull,
539
+ authorization_code: str,
540
+ ) -> AuthorizationCode | None:
541
+ """Load authorization code for validation.
542
+
543
+ Look up our client code and return authorization code object
544
+ with PKCE challenge for validation.
545
+ """
546
+ # Look up client code data
547
+ code_data = self._client_codes.get(authorization_code)
548
+ if not code_data:
549
+ logger.debug("Authorization code not found: %s", authorization_code)
550
+ return None
551
+
552
+ # Check if code expired
553
+ if time.time() > code_data["expires_at"]:
554
+ logger.debug("Authorization code expired: %s", authorization_code)
555
+ self._client_codes.pop(authorization_code, None)
556
+ return None
557
+
558
+ # Verify client ID matches
559
+ if code_data["client_id"] != client.client_id:
560
+ logger.debug(
561
+ "Authorization code client ID mismatch: %s vs %s",
562
+ code_data["client_id"],
563
+ client.client_id,
564
+ )
565
+ return None
566
+
567
+ # Create authorization code object with PKCE challenge
568
+ return AuthorizationCode(
569
+ code=authorization_code,
570
+ client_id=client.client_id,
571
+ redirect_uri=code_data["redirect_uri"],
572
+ redirect_uri_provided_explicitly=True,
573
+ scopes=code_data["scopes"],
574
+ expires_at=code_data["expires_at"],
575
+ code_challenge=code_data.get("code_challenge", ""),
576
+ )
577
+
578
+ async def exchange_authorization_code(
579
+ self,
580
+ client: OAuthClientInformationFull,
581
+ authorization_code: AuthorizationCode,
582
+ ) -> OAuthToken:
583
+ """Exchange authorization code for stored IdP tokens.
584
+
585
+ For the DCR-compliant proxy flow, we return the IdP tokens that were obtained
586
+ during the IdP callback exchange. PKCE validation is handled by the MCP framework.
587
+ """
588
+ # Look up stored code data
589
+ code_data = self._client_codes.get(authorization_code.code)
590
+ if not code_data:
591
+ logger.error(
592
+ "Authorization code not found in client codes: %s",
593
+ authorization_code.code,
594
+ )
595
+ raise TokenError("invalid_grant", "Authorization code not found")
596
+
597
+ # Get stored IdP tokens
598
+ idp_tokens = code_data["idp_tokens"]
599
+
600
+ # Clean up client code (one-time use)
601
+ self._client_codes.pop(authorization_code.code, None)
602
+
603
+ # Extract token information for local tracking
604
+ access_token_value = idp_tokens["access_token"]
605
+ refresh_token_value = idp_tokens.get("refresh_token")
606
+ expires_in = int(
607
+ idp_tokens.get("expires_in", DEFAULT_ACCESS_TOKEN_EXPIRY_SECONDS)
608
+ )
609
+ expires_at = int(time.time() + expires_in)
610
+
611
+ # Store access token locally for tracking
612
+ access_token = AccessToken(
613
+ token=access_token_value,
614
+ client_id=client.client_id,
615
+ scopes=authorization_code.scopes,
616
+ expires_at=expires_at,
617
+ )
618
+ self._access_tokens[access_token_value] = access_token
619
+
620
+ # Store refresh token if provided
621
+ if refresh_token_value:
622
+ refresh_token = RefreshToken(
623
+ token=refresh_token_value,
624
+ client_id=client.client_id,
625
+ scopes=authorization_code.scopes,
626
+ expires_at=None, # Refresh tokens typically don't expire
627
+ )
628
+ self._refresh_tokens[refresh_token_value] = refresh_token
629
+
630
+ # Maintain token relationships for cleanup
631
+ self._access_to_refresh[access_token_value] = refresh_token_value
632
+ self._refresh_to_access[refresh_token_value] = access_token_value
633
+
634
+ logger.debug(
635
+ "Successfully exchanged client code for stored IdP tokens (client: %s)",
636
+ client.client_id,
637
+ )
638
+
639
+ return OAuthToken(**idp_tokens) # type: ignore[arg-type]
640
+
641
+ # -------------------------------------------------------------------------
642
+ # Refresh Token Flow
643
+ # -------------------------------------------------------------------------
644
+
645
+ async def load_refresh_token(
646
+ self,
647
+ client: OAuthClientInformationFull,
648
+ refresh_token: str,
649
+ ) -> RefreshToken | None:
650
+ """Load refresh token from local storage."""
651
+ return self._refresh_tokens.get(refresh_token)
652
+
653
+ async def exchange_refresh_token(
654
+ self,
655
+ client: OAuthClientInformationFull,
656
+ refresh_token: RefreshToken,
657
+ scopes: list[str],
658
+ ) -> OAuthToken:
659
+ """Exchange refresh token for new access token using authlib."""
660
+
661
+ # Use authlib's AsyncOAuth2Client for refresh token exchange
662
+ oauth_client = AsyncOAuth2Client(
663
+ client_id=self._upstream_client_id,
664
+ client_secret=self._upstream_client_secret.get_secret_value(),
665
+ token_endpoint_auth_method=self._token_endpoint_auth_method,
666
+ timeout=HTTP_TIMEOUT_SECONDS,
667
+ )
668
+
669
+ try:
670
+ logger.debug("Using authlib to refresh token from upstream")
671
+
672
+ # Let authlib handle the refresh token exchange
673
+ token_response: dict[str, Any] = await oauth_client.refresh_token( # type: ignore[misc]
674
+ url=self._upstream_token_endpoint,
675
+ refresh_token=refresh_token.token,
676
+ scope=" ".join(scopes) if scopes else None,
677
+ )
678
+
679
+ logger.debug(
680
+ "Successfully refreshed access token via authlib (client: %s)",
681
+ client.client_id,
682
+ )
683
+
684
+ except Exception as e:
685
+ logger.error("Authlib refresh token exchange failed: %s", e)
686
+ raise TokenError(
687
+ "invalid_grant", f"Upstream refresh token exchange failed: {e}"
688
+ ) from e
689
+
690
+ # Update local token storage
691
+ new_access_token = token_response["access_token"]
692
+ expires_in = int(
693
+ token_response.get("expires_in", DEFAULT_ACCESS_TOKEN_EXPIRY_SECONDS)
694
+ )
695
+
696
+ self._access_tokens[new_access_token] = AccessToken(
697
+ token=new_access_token,
698
+ client_id=client.client_id,
699
+ scopes=scopes,
700
+ expires_at=int(time.time() + expires_in),
701
+ )
702
+
703
+ # Handle refresh token rotation if new one provided
704
+ if "refresh_token" in token_response:
705
+ new_refresh_token = token_response["refresh_token"]
706
+ if new_refresh_token != refresh_token.token:
707
+ # Remove old refresh token
708
+ self._refresh_tokens.pop(refresh_token.token, None)
709
+ old_access = self._refresh_to_access.pop(refresh_token.token, None)
710
+ if old_access:
711
+ self._access_to_refresh.pop(old_access, None)
712
+
713
+ # Store new refresh token
714
+ self._refresh_tokens[new_refresh_token] = RefreshToken(
715
+ token=new_refresh_token,
716
+ client_id=client.client_id,
717
+ scopes=scopes,
718
+ expires_at=None,
719
+ )
720
+ self._access_to_refresh[new_access_token] = new_refresh_token
721
+ self._refresh_to_access[new_refresh_token] = new_access_token
722
+
723
+ return OAuthToken(**token_response) # type: ignore[arg-type]
724
+
725
+ # -------------------------------------------------------------------------
726
+ # Token Validation
727
+ # -------------------------------------------------------------------------
728
+
729
+ async def load_access_token(self, token: str) -> AccessToken | None:
730
+ """Validate access token using upstream JWKS.
731
+
732
+ Delegates to the JWT verifier which handles signature validation,
733
+ expiration checking, and claims validation using the upstream JWKS.
734
+ """
735
+ result = await self._token_validator.verify_token(token)
736
+ if result:
737
+ logger.debug("Token validated successfully")
738
+ else:
739
+ logger.debug("Token validation failed")
740
+ return result
741
+
742
+ # -------------------------------------------------------------------------
743
+ # Token Revocation
744
+ # -------------------------------------------------------------------------
745
+
746
+ async def revoke_token(self, token: AccessToken | RefreshToken) -> None:
747
+ """Revoke token locally and with upstream server if supported.
748
+
749
+ Removes tokens from local storage and attempts to revoke them with
750
+ the upstream server if a revocation endpoint is configured.
751
+ """
752
+ # Clean up local token storage
753
+ if isinstance(token, AccessToken):
754
+ self._access_tokens.pop(token.token, None)
755
+ # Also remove associated refresh token
756
+ paired_refresh = self._access_to_refresh.pop(token.token, None)
757
+ if paired_refresh:
758
+ self._refresh_tokens.pop(paired_refresh, None)
759
+ self._refresh_to_access.pop(paired_refresh, None)
760
+ else: # RefreshToken
761
+ self._refresh_tokens.pop(token.token, None)
762
+ # Also remove associated access token
763
+ paired_access = self._refresh_to_access.pop(token.token, None)
764
+ if paired_access:
765
+ self._access_tokens.pop(paired_access, None)
766
+ self._access_to_refresh.pop(paired_access, None)
767
+
768
+ # Attempt upstream revocation if endpoint is configured
769
+ if self._upstream_revocation_endpoint:
770
+ try:
771
+ async with httpx.AsyncClient(
772
+ timeout=HTTP_TIMEOUT_SECONDS
773
+ ) as http_client:
774
+ await http_client.post(
775
+ self._upstream_revocation_endpoint,
776
+ data={"token": token.token},
777
+ auth=(
778
+ self._upstream_client_id,
779
+ self._upstream_client_secret.get_secret_value(),
780
+ ),
781
+ )
782
+ logger.debug("Successfully revoked token with upstream server")
783
+ except Exception as e:
784
+ logger.warning("Failed to revoke token with upstream server: %s", e)
785
+ else:
786
+ logger.debug("No upstream revocation endpoint configured")
787
+
788
+ logger.debug("Token revoked successfully")
789
+
790
+ def get_routes(
791
+ self,
792
+ mcp_path: str | None = None,
793
+ mcp_endpoint: Any | None = None,
794
+ ) -> list[Route]:
795
+ """Get OAuth routes with custom proxy token handler.
796
+
797
+ This method creates standard OAuth routes and replaces the token endpoint
798
+ with our proxy handler that forwards requests to the upstream OAuth server.
799
+
800
+ Args:
801
+ mcp_path: The path where the MCP endpoint is mounted (e.g., "/mcp")
802
+ mcp_endpoint: The MCP endpoint handler to protect with auth
803
+ """
804
+ # Get standard OAuth routes from parent class
805
+ routes = super().get_routes(mcp_path, mcp_endpoint)
806
+ custom_routes = []
807
+ token_route_found = False
808
+
809
+ logger.debug(
810
+ f"get_routes called - configuring OAuth routes in {len(routes)} routes"
811
+ )
812
+
813
+ for i, route in enumerate(routes):
814
+ logger.debug(
815
+ f"Route {i}: {route} - path: {getattr(route, 'path', 'N/A')}, methods: {getattr(route, 'methods', 'N/A')}"
816
+ )
817
+
818
+ # Keep all standard OAuth routes unchanged - our DCR-compliant flow handles everything
819
+ custom_routes.append(route)
820
+
821
+ if (
822
+ isinstance(route, Route)
823
+ and route.path == "/token"
824
+ and route.methods is not None
825
+ and "POST" in route.methods
826
+ ):
827
+ token_route_found = True
828
+
829
+ # Add OAuth callback endpoint for forwarding to client callbacks
830
+ custom_routes.append(
831
+ Route(
832
+ path=self._redirect_path,
833
+ endpoint=self._handle_idp_callback,
834
+ methods=["GET"],
835
+ )
836
+ )
837
+
838
+ logger.debug(
839
+ f"✅ OAuth routes configured: token_endpoint={token_route_found}, total routes={len(custom_routes)} (includes OAuth callback)"
840
+ )
841
+ return custom_routes
842
+
843
+ # -------------------------------------------------------------------------
844
+ # IdP Callback Forwarding
845
+ # -------------------------------------------------------------------------
846
+
847
+ async def _handle_idp_callback(self, request: Request) -> RedirectResponse:
848
+ """Handle callback from upstream IdP and forward to client.
849
+
850
+ This implements the DCR-compliant callback forwarding:
851
+ 1. Receive IdP callback with code and txn_id as state
852
+ 2. Exchange IdP code for tokens (server-side)
853
+ 3. Generate our own client code bound to PKCE challenge
854
+ 4. Redirect to client's callback with client code and original state
855
+ """
856
+ try:
857
+ idp_code = request.query_params.get("code")
858
+ txn_id = request.query_params.get("state")
859
+ error = request.query_params.get("error")
860
+
861
+ if error:
862
+ logger.error(
863
+ "IdP callback error: %s - %s",
864
+ error,
865
+ request.query_params.get("error_description"),
866
+ )
867
+ # TODO: Forward error to client callback
868
+ return RedirectResponse(
869
+ url=f"data:text/html,<h1>OAuth Error</h1><p>{error}: {request.query_params.get('error_description', 'Unknown error')}</p>",
870
+ status_code=302,
871
+ )
872
+
873
+ if not idp_code or not txn_id:
874
+ logger.error("IdP callback missing code or transaction ID")
875
+ return RedirectResponse(
876
+ url="data:text/html,<h1>OAuth Error</h1><p>Missing authorization code or transaction ID</p>",
877
+ status_code=302,
878
+ )
879
+
880
+ # Look up transaction data
881
+ transaction = self._oauth_transactions.get(txn_id)
882
+ if not transaction:
883
+ logger.error("IdP callback with invalid transaction ID: %s", txn_id)
884
+ return RedirectResponse(
885
+ url="data:text/html,<h1>OAuth Error</h1><p>Invalid or expired transaction</p>",
886
+ status_code=302,
887
+ )
888
+
889
+ # Exchange IdP code for tokens (server-side)
890
+ oauth_client = AsyncOAuth2Client(
891
+ client_id=self._upstream_client_id,
892
+ client_secret=self._upstream_client_secret.get_secret_value(),
893
+ token_endpoint_auth_method=self._token_endpoint_auth_method,
894
+ timeout=HTTP_TIMEOUT_SECONDS,
895
+ )
896
+
897
+ try:
898
+ idp_redirect_uri = (
899
+ f"{str(self.base_url).rstrip('/')}{self._redirect_path}"
900
+ )
901
+ logger.debug(
902
+ f"Exchanging IdP code for tokens with redirect_uri: {idp_redirect_uri}"
903
+ )
904
+
905
+ # Build token exchange parameters
906
+ token_params = {
907
+ "url": self._upstream_token_endpoint,
908
+ "code": idp_code,
909
+ "redirect_uri": idp_redirect_uri,
910
+ }
911
+
912
+ # Include proxy's code_verifier if we forwarded PKCE
913
+ proxy_code_verifier = transaction.get("proxy_code_verifier")
914
+ if proxy_code_verifier:
915
+ token_params["code_verifier"] = proxy_code_verifier
916
+ logger.debug(
917
+ "Including proxy code_verifier in token exchange for transaction %s",
918
+ txn_id,
919
+ )
920
+
921
+ # Add any extra token parameters configured for this proxy
922
+ if self._extra_token_params:
923
+ token_params.update(self._extra_token_params)
924
+ logger.debug(
925
+ "Adding extra token parameters for transaction %s: %s",
926
+ txn_id,
927
+ list(self._extra_token_params.keys()),
928
+ )
929
+
930
+ idp_tokens: dict[str, Any] = await oauth_client.fetch_token(
931
+ **token_params
932
+ ) # type: ignore[misc]
933
+
934
+ logger.debug(
935
+ f"Successfully exchanged IdP code for tokens (transaction: {txn_id}, PKCE: {bool(proxy_code_verifier)})"
936
+ )
937
+
938
+ except Exception as e:
939
+ logger.error("IdP token exchange failed: %s", e)
940
+ # TODO: Forward error to client callback
941
+ return RedirectResponse(
942
+ url=f"data:text/html,<h1>OAuth Error</h1><p>Token exchange failed: {e}</p>",
943
+ status_code=302,
944
+ )
945
+
946
+ # Generate our own authorization code for the client
947
+ client_code = secrets.token_urlsafe(32)
948
+ code_expires_at = int(time.time() + DEFAULT_AUTH_CODE_EXPIRY_SECONDS)
949
+
950
+ # Store client code with PKCE challenge and IdP tokens
951
+ self._client_codes[client_code] = {
952
+ "client_id": transaction["client_id"],
953
+ "redirect_uri": transaction["client_redirect_uri"],
954
+ "code_challenge": transaction["code_challenge"],
955
+ "code_challenge_method": transaction["code_challenge_method"],
956
+ "scopes": transaction["scopes"],
957
+ "idp_tokens": idp_tokens,
958
+ "expires_at": code_expires_at,
959
+ "created_at": time.time(),
960
+ }
961
+
962
+ # Clean up transaction
963
+ self._oauth_transactions.pop(txn_id, None)
964
+
965
+ # Build client callback URL with our code and original state
966
+ client_redirect_uri = transaction["client_redirect_uri"]
967
+ client_state = transaction["client_state"]
968
+
969
+ callback_params = {
970
+ "code": client_code,
971
+ "state": client_state,
972
+ }
973
+
974
+ # Add query parameters to client redirect URI
975
+ separator = "&" if "?" in client_redirect_uri else "?"
976
+ client_callback_url = (
977
+ f"{client_redirect_uri}{separator}{urlencode(callback_params)}"
978
+ )
979
+
980
+ logger.debug(f"Forwarding to client callback for transaction {txn_id}")
981
+
982
+ return RedirectResponse(url=client_callback_url, status_code=302)
983
+
984
+ except Exception as e:
985
+ logger.error("Error in IdP callback handler: %s", e, exc_info=True)
986
+ return RedirectResponse(
987
+ url="data:text/html,<h1>OAuth Error</h1><p>Internal server error during IdP callback</p>",
988
+ status_code=302,
989
+ )