|
8 | 8 |
|
9 | 9 | from airweave.core.credential_sanitizer import safe_log_credentials |
10 | 10 | from airweave.platform.auth_providers._base import BaseAuthProvider |
| 11 | +from airweave.platform.auth_providers.auth_result import AuthResult |
11 | 12 | from airweave.platform.decorators import auth_provider |
12 | 13 |
|
13 | 14 |
|
| 15 | +class PipedreamDefaultOAuthException(Exception): |
| 16 | + """Raised when trying to access credentials for a Pipedream default OAuth client. |
| 17 | +
|
| 18 | + This happens when the connected account uses Pipedream's built-in OAuth client |
| 19 | + rather than a custom OAuth client. In this case, credentials cannot be retrieved |
| 20 | + directly and the proxy must be used. |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, source_short_name: str, message: str = None): |
| 24 | + """Initialize the exception.""" |
| 25 | + self.source_short_name = source_short_name |
| 26 | + if message is None: |
| 27 | + message = ( |
| 28 | + f"Cannot retrieve credentials for {source_short_name}. " |
| 29 | + "This account uses Pipedream's default OAuth client. " |
| 30 | + "Proxy mode must be used for this connection." |
| 31 | + ) |
| 32 | + super().__init__(message) |
| 33 | + |
| 34 | + |
14 | 35 | @auth_provider( |
15 | 36 | name="Pipedream", |
16 | 37 | short_name="pipedream", |
@@ -98,10 +119,10 @@ async def create( |
98 | 119 | instance = cls() |
99 | 120 | instance.client_id = credentials["client_id"] |
100 | 121 | instance.client_secret = credentials["client_secret"] |
101 | | - instance.project_id = config.get("project_id") |
102 | | - instance.account_id = config.get("account_id") |
| 122 | + instance.project_id = config["project_id"] |
| 123 | + instance.account_id = config["account_id"] |
| 124 | + instance.external_user_id = config["external_user_id"] |
103 | 125 | instance.environment = config.get("environment", "production") |
104 | | - instance.external_user_id = config.get("external_user_id") |
105 | 126 |
|
106 | 127 | # Initialize token management |
107 | 128 | instance._access_token = None |
@@ -369,11 +390,7 @@ async def _get_account_with_credentials( |
369 | 390 | "❌ [Pipedream] No credentials in response. This usually means the account " |
370 | 391 | "was created with Pipedream's default OAuth client, not a custom one." |
371 | 392 | ) |
372 | | - raise HTTPException( |
373 | | - status_code=422, |
374 | | - detail="Credentials not available. Pipedream only exposes credentials for " |
375 | | - "accounts created with custom OAuth clients, not default Pipedream OAuth.", |
376 | | - ) |
| 393 | + raise PipedreamDefaultOAuthException(source_short_name) |
377 | 394 |
|
378 | 395 | self.logger.info( |
379 | 396 | f"✅ [Pipedream] Found account '{account_data.get('name')}' " |
@@ -464,3 +481,43 @@ def _extract_and_map_credentials( |
464 | 481 | ) |
465 | 482 |
|
466 | 483 | return found_credentials |
| 484 | + |
| 485 | + async def get_auth_result( |
| 486 | + self, source_short_name: str, source_auth_config_fields: List[str] |
| 487 | + ) -> AuthResult: |
| 488 | + """Get auth result with explicit mode for Pipedream. |
| 489 | +
|
| 490 | + Determines whether to use direct credentials or proxy based on OAuth client type. |
| 491 | + """ |
| 492 | + # Check if source is in blocked list (must use proxy) |
| 493 | + if source_short_name in self.BLOCKED_SOURCES: |
| 494 | + self.logger.info(f"Source {source_short_name} is in blocked list - using proxy mode") |
| 495 | + return AuthResult.proxy( |
| 496 | + { |
| 497 | + "reason": "blocked_source", |
| 498 | + "source": source_short_name, |
| 499 | + } |
| 500 | + ) |
| 501 | + |
| 502 | + # Try to get credentials to determine OAuth client type |
| 503 | + try: |
| 504 | + credentials = await self.get_creds_for_source( |
| 505 | + source_short_name, source_auth_config_fields |
| 506 | + ) |
| 507 | + # Custom OAuth client - can use direct access |
| 508 | + self.logger.info( |
| 509 | + f"Custom OAuth client detected for {source_short_name} - using direct mode" |
| 510 | + ) |
| 511 | + return AuthResult.direct(credentials) |
| 512 | + |
| 513 | + except PipedreamDefaultOAuthException: |
| 514 | + # Default OAuth client - must use proxy |
| 515 | + self.logger.info( |
| 516 | + f"Default OAuth client detected for {source_short_name} - using proxy mode" |
| 517 | + ) |
| 518 | + return AuthResult.proxy( |
| 519 | + { |
| 520 | + "reason": "default_oauth", |
| 521 | + "source": source_short_name, |
| 522 | + } |
| 523 | + ) |
0 commit comments