Skip to content

Automate browser based workflows with AI

Add Gmail OAuth backend and Gmail-backed OTP lookup (#6769)

Suchintan00bf6e69f2a6
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index 5446db905..607aaa336 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -6,13 +6,14 @@ import time
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
-from datetime import timedelta
+from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, Dict, List
import aiohttp
import httpx
import structlog
from cachetools import TTLCache
+from google.oauth2.credentials import Credentials
from playwright.async_api import Frame, Page
from skyvern.config import settings
@@ -41,6 +42,7 @@ from skyvern.forge.sdk.services import google_oauth_service
from skyvern.forge.sdk.trace import traced
from skyvern.forge.sdk.workflow.models.block import BlockTypeVar
from skyvern.schemas.workflows import FileStorageType, FileUploadDestination
+from skyvern.services.otp_gmail import GmailOTPVerificationContext
from skyvern.webeye.actions.actions import Action
from skyvern.webeye.browser_state import BrowserState
from skyvern.webeye.scraper.scraped_page import ELEMENT_NODE_ATTRIBUTES, CleanupElementTreeFunc, json_to_html
@@ -50,6 +52,7 @@ from skyvern.webeye.utils.page import SkyvernFrame
if TYPE_CHECKING:
from skyvern.forge.sdk.db.enums import WorkflowRunTriggerType
from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, WorkflowRunStatus
+ from skyvern.services.otp_service import OTPValue
LOG = structlog.get_logger()
@@ -1040,26 +1043,25 @@ class AgentFunction:
organization_id: str,
credential_id: str,
required_scopes: list[str] | None = None,
- ) -> object | None:
+ ) -> Credentials | None:
"""Return a refreshed ``google.oauth2.credentials.Credentials``, or None on failure.
- ``required_scopes`` is accepted for forward-compat with cloud overrides
- that may enforce scope checks; the OSS path does not yet use it.
+ ``required_scopes`` gates use of a credential whose grant does not cover
+ the API the caller is about to use.
"""
- if required_scopes:
- # Debug-level so OSS operators don't see this on every call; scope
- # enforcement lives on the cloud override and a future caller can
- # grep for this message if they need to audit usage.
- LOG.debug(
- "required_scopes ignored by OSS get_google_workspace_credentials; cloud override gates this",
- required_scopes=required_scopes,
- credential_id=credential_id,
- )
try:
secrets = await google_oauth_service.load_credential_secrets(
organization_id=organization_id,
credential_id=credential_id,
)
+ if required_scopes and not google_oauth_service.has_required_scopes(secrets.scopes, required_scopes):
+ LOG.info(
+ "Google OAuth credential missing required scopes",
+ organization_id=organization_id,
+ credential_id=credential_id,
+ required_scopes=required_scopes,
+ )
+ return None
return await google_oauth_service.credentials_from_secrets(secrets)
except google_oauth_service.EncryptionNotConfiguredError:
LOG.error(
@@ -1076,6 +1078,19 @@ class AgentFunction:
)
return None
+ async def get_otp_value_from_gmail(
+ self,
+ *,
+ organization_id: str,
+ totp_identifier: str,
+ workflow_id: str | None = None,
+ workflow_run_id: str | None = None,
+ created_after: datetime | None = None,
+ context: GmailOTPVerificationContext | None = None,
+ ) -> "OTPValue | None":
+ """Cloud-only Gmail OTP lookup hook. OSS builds do not read inboxes."""
+ return None
+
async def ensure_sheet_tab(
self,
*,
diff --git a/skyvern/forge/sdk/routes/google_oauth.py b/skyvern/forge/sdk/routes/google_oauth.py
index 971176824..c987ee552 100644
--- a/skyvern/forge/sdk/routes/google_oauth.py
+++ b/skyvern/forge/sdk/routes/google_oauth.py
@@ -60,10 +60,13 @@ async def google_oauth_authorize(
organization_id=current_org.organization_id,
redirect_uri=request.redirect_uri,
credential_name=request.credential_name,
+ scope_profile=request.scope_profile,
app_origin=request.app_origin,
)
except InvalidAppOriginError as exc:
raise HTTPException(status_code=400, detail=str(exc))
+ except google_oauth_service.UnsupportedScopeProfileError as exc:
+ raise HTTPException(status_code=400, detail=str(exc))
except google_oauth_service.InvalidRedirectURIError as exc:
raise HTTPException(status_code=400, detail=str(exc))
except google_oauth_service.EncryptionNotConfiguredError as exc:
diff --git a/skyvern/forge/sdk/schemas/google_oauth.py b/skyvern/forge/sdk/schemas/google_oauth.py
index 85a7b22b9..92180a2e9 100644
--- a/skyvern/forge/sdk/schemas/google_oauth.py
+++ b/skyvern/forge/sdk/schemas/google_oauth.py
@@ -1,4 +1,5 @@
from datetime import datetime
+from typing import Literal
from pydantic import BaseModel, Field, field_validator
@@ -27,6 +28,11 @@ class GoogleOAuthCredentialListResponse(BaseModel):
class CreateGoogleOAuthAuthorizeRequest(BaseModel):
redirect_uri: str = Field(..., description="Redirect URI the consent flow will return to")
credential_name: str = Field(default="Default", description="Human-readable name for this credential")
+ scope_profile: Literal["google_sheets", "gmail"] | None = Field(
+ default=None,
+ description="Allowed Google OAuth scope profile to request. Defaults to google_sheets.",
+ examples=["google_sheets", "gmail"],
+ )
app_origin: str | None = Field(
default=None,
description="Origin the callback should bounce back to (e.g. a Vercel preview URL). "
diff --git a/skyvern/forge/sdk/services/google_gmail_service.py b/skyvern/forge/sdk/services/google_gmail_service.py
new file mode 100644
index 000000000..292376611
--- /dev/null
+++ b/skyvern/forge/sdk/services/google_gmail_service.py
@@ -0,0 +1,183 @@
+import base64
+import binascii
+import re
+from dataclasses import dataclass
+from datetime import datetime, timezone
+from math import ceil
+from typing import Any
+from urllib.parse import quote
+
+import httpx
+
+GMAIL_API_BASE = "https://gmail.googleapis.com/gmail/v1"
+
+_OTP_QUERY_TERMS = "(verification OR verify OR code OR passcode OR otp OR 2fa OR one-time OR password)"
+_SAFE_EMAIL_QUERY_IDENTIFIER = re.compile(r"^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+$")
+
+
+@dataclass(frozen=True)
+class GmailMessageCandidate:
+ message_id: str
+ content: str
+ internal_date: datetime | None = None
+
+
+class GmailAPIError(RuntimeError):
+ def __init__(self, *, status: int, code: str | None, message: str) -> None:
+ super().__init__(message)
+ self.status = status
+ self.code = code
+
+
+async def _get_json(
+ client: httpx.AsyncClient,
+ url: str,
+ *,
+ access_token: str,
+ params: dict[str, Any] | None = None,
+) -> dict[str, Any]:
+ response = await client.get(
+ url,
+ params=params,
+ headers={"Authorization": f"Bearer {access_token}", "Accept": "application/json"},
+ )
+ if response.is_success:
+ return response.json() or {}
+
+ code = None
+ message = response.text[:500] or "Gmail API error"
+ try:
+ err = (response.json() or {}).get("error")
+ if isinstance(err, dict):
+ message = err.get("message") or message
+ details = err.get("errors")
+ if isinstance(details, list) and details and isinstance(details[0], dict):
+ code = details[0].get("reason")
+ except ValueError:
+ pass
+ if response.status_code == 403 and code in {"insufficientPermissions", "insufficientScopes"}:
+ code = "reconnect_required"
+ raise GmailAPIError(status=response.status_code, code=code, message=message)
+
+
+def _as_utc(value: datetime) -> datetime:
+ return value.replace(tzinfo=timezone.utc) if value.tzinfo is None else value.astimezone(timezone.utc)
+
+
+def _newer_than_query(created_after: datetime | None) -> str:
+ if not created_after:
+ return "newer_than:1d"
+ seconds = max(1, (datetime.now(timezone.utc) - _as_utc(created_after)).total_seconds())
+ days = max(1, ceil(seconds / 86_400))
+ return f"newer_than:{days}d"
+
+
+def _build_query(totp_identifier: str, *, created_after: datetime | None = None) -> str | None:
+ identifier = totp_identifier.strip()
+ if not _SAFE_EMAIL_QUERY_IDENTIFIER.fullmatch(identifier):
+ return None
+ quoted = '"' + identifier.replace("\\", "\\\\").replace('"', '\\"') + '"'
+ return f"{_newer_than_query(created_after)} {_OTP_QUERY_TERMS} (to:{quoted} OR deliveredto:{quoted})"
+
+
+def _decode(data: str | None) -> str:
+ if not data:
+ return ""
+ try:
+ return base64.urlsafe_b64decode(f"{data}{'=' * (-len(data) % 4)}").decode("utf-8", errors="replace")
+ except (binascii.Error, UnicodeDecodeError, ValueError):
+ return ""
+
+
+def _payload_text(payload: dict[str, Any]) -> list[str]:
+ texts: list[str] = []
+ body = payload.get("body") if isinstance(payload.get("body"), dict) else {}
+ decoded = _decode(body.get("data") if isinstance(body, dict) else None)
+ mime_type = str(payload.get("mimeType") or "").lower()
+ if decoded and mime_type in {"text/plain", "text/html"}:
+ texts.append(decoded)
+ for part in payload.get("parts") or []:
+ if isinstance(part, dict):
+ texts.extend(_payload_text(part))
+ return texts
+
+
+def _internal_date(message: dict[str, Any]) -> datetime | None:
+ try:
+ return datetime.fromtimestamp(int(message["internalDate"]) / 1000, tz=timezone.utc)
+ except (KeyError, TypeError, ValueError, OSError):
+ return None
+
+
+def _candidate(message: dict[str, Any]) -> GmailMessageCandidate | None:
+ message_id = message.get("id")
+ if not isinstance(message_id, str):
+ return None
+ raw_payload = message.get("payload")
+ payload: dict[str, Any] = raw_payload if isinstance(raw_payload, dict) else {}
+ headers = {
+ h["name"].lower(): h["value"]
+ for h in payload.get("headers") or []
+ if isinstance(h, dict) and isinstance(h.get("name"), str) and isinstance(h.get("value"), str)
+ }
+ snippet = message.get("snippet") if isinstance(message.get("snippet"), str) else None
+ content = "\n".join(
+ part
+ for part in [
+ f"Subject: {headers['subject']}" if headers.get("subject") else "",
+ f"Snippet: {snippet}" if snippet else "",
+ "\n".join(_payload_text(payload)),
+ ]
+ if part
+ ).strip()
+ return (
+ GmailMessageCandidate(message_id=message_id, content=content, internal_date=_internal_date(message))
+ if content
+ else None
+ )
+
+
+async def search_recent_otp_messages(
+ *,
+ access_token: str,
+ totp_identifier: str,
+ created_after: datetime | None = None,
+ max_results: int = 10,
+ client: httpx.AsyncClient | None = None,
+) -> list[GmailMessageCandidate]:
+ query = _build_query(totp_identifier, created_after=created_after)
+ if query is None:
+ return []
+ cutoff = _as_utc(created_after) if created_after else None
+
+ async def _search(client_: httpx.AsyncClient) -> list[GmailMessageCandidate]:
+ payload = await _get_json(
+ client_,
+ f"{GMAIL_API_BASE}/users/me/messages",
+ access_token=access_token,
+ params={"q": query, "maxResults": max(1, min(max_results, 20)), "includeSpamTrash": "false"},
+ )
+ candidates_: list[GmailMessageCandidate] = []
+ for ref in (payload.get("messages") or [])[:max_results]:
+ message_id = ref.get("id") if isinstance(ref, dict) else None
+ if not isinstance(message_id, str):
+ continue
+ message = await _get_json(
+ client_,
+ f"{GMAIL_API_BASE}/users/me/messages/{quote(message_id, safe='')}",
+ access_token=access_token,
+ params={"format": "full"},
+ )
+ candidate = _candidate(message)
+ if candidate and candidate.internal_date and (not cutoff or candidate.internal_date >= cutoff):
+ candidates_.append(candidate)
+ return candidates_
+
+ if client is None:
+ async with httpx.AsyncClient(timeout=20.0) as owned_client:
+ candidates = await _search(owned_client)
+ else:
+ candidates = await _search(client)
+ return sorted(
+ candidates, key=lambda item: item.internal_date or datetime.min.replace(tzinfo=timezone.utc), reverse=True
+ )
diff --git a/skyvern/forge/sdk/services/google_oauth_service.py b/skyvern/forge/sdk/services/google_oauth_service.py
index dd6fb09dd..d2993e176 100644
--- a/skyvern/forge/sdk/services/google_oauth_service.py
+++ b/skyvern/forge/sdk/services/google_oauth_service.py
@@ -56,6 +56,14 @@ GOOGLE_SHEETS_SCOPES: tuple[str, ...] = (
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.metadata.readonly",
)
+GOOGLE_GMAIL_READONLY_SCOPE = "https://www.googleapis.com/auth/gmail.readonly"
+GOOGLE_GMAIL_SCOPES: tuple[str, ...] = (GOOGLE_GMAIL_READONLY_SCOPE,)
+GOOGLE_OAUTH_SCOPE_PROFILE_SHEETS = "google_sheets"
+GOOGLE_OAUTH_SCOPE_PROFILE_GMAIL = "gmail"
+GOOGLE_OAUTH_SCOPE_PROFILES: dict[str, tuple[str, ...]] = {
+ GOOGLE_OAUTH_SCOPE_PROFILE_SHEETS: GOOGLE_SHEETS_SCOPES,
+ GOOGLE_OAUTH_SCOPE_PROFILE_GMAIL: GOOGLE_GMAIL_SCOPES,
+}
CONSENT_TTL_SECONDS = 600
@@ -80,6 +88,10 @@ class InvalidAppOriginError(ValueError):
"""Raised when the supplied app_origin is not on the GOOGLE_OAUTH_APP_ORIGINS allowlist."""
+class UnsupportedScopeProfileError(ValueError):
+ """Raised when a caller asks for an unsupported Google OAuth scope profile."""
+
+
@dataclass(frozen=True)
class GoogleCredentialSecrets:
"""Decrypted credential material needed to mint an access token."""
@@ -116,6 +128,22 @@ def _coerce_scopes(scopes: str | list[str] | tuple[str, ...] | None) -> list[str
return parts or list(GOOGLE_SHEETS_SCOPES)
+def scopes_for_profile(scope_profile: str | None) -> list[str]:
+ if scope_profile is None:
+ scope_profile = GOOGLE_OAUTH_SCOPE_PROFILE_SHEETS
+ if scope_profile not in GOOGLE_OAUTH_SCOPE_PROFILES:
+ raise UnsupportedScopeProfileError(f"Unsupported Google OAuth scope profile: {scope_profile}")
+ return list(GOOGLE_OAUTH_SCOPE_PROFILES[scope_profile])
+
+
+def has_required_scopes(
+ granted_scopes: list[str] | tuple[str, ...] | None,
+ required_scopes: list[str] | tuple[str, ...],
+) -> bool:
+ granted = set(granted_scopes or [])
+ return all(scope in granted for scope in required_scopes)
+
+
_LOOPBACK_HOSTS = frozenset({"localhost", "127.0.0.1", "::1"})
@@ -266,6 +294,7 @@ async def start_authorization(
redirect_uri: str,
credential_name: str = "Default",
scopes_requested: str | list[str] | tuple[str, ...] | None = None,
+ scope_profile: str | None = None,
app_origin: str | None = None,
) -> GoogleAuthorizationStart:
"""Insert a pending consent row, build the authorize URL, persist the PKCE verifier."""
@@ -281,12 +310,15 @@ async def start_authorization(
# pending row that the callback would see as missing the verifier.
code_verifier = secrets.token_urlsafe(64)
now = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
+ requested_scopes = (
+ scopes_for_profile(scope_profile) if scopes_requested is None else _coerce_scopes(scopes_requested)
+ )
await app.DATABASE.google_oauth.insert_pending_credential(
credential_id=credential_id,
organization_id=organization_id,
credential_name=credential_name,
- scopes_requested=_coerce_scopes(scopes_requested),
+ scopes_requested=requested_scopes,
consent_nonce=nonce,
consent_redirect_uri=redirect_uri,
consent_expires_at=now + datetime.timedelta(seconds=CONSENT_TTL_SECONDS),
@@ -297,7 +329,7 @@ async def start_authorization(
authorize_url, _ = build_authorize_url(
redirect_uri=redirect_uri,
state=nonce,
- scopes=scopes_requested,
+ scopes=requested_scopes,
code_verifier=code_verifier,
)
diff --git a/skyvern/services/otp_gmail.py b/skyvern/services/otp_gmail.py
new file mode 100644
index 000000000..822c9cbb1
--- /dev/null
+++ b/skyvern/services/otp_gmail.py
@@ -0,0 +1,12 @@
+from dataclasses import dataclass, field
+from datetime import datetime
+
+
+@dataclass
+class GmailOTPVerificationContext:
+ """Per-poll Gmail OTP lookup cache passed through the AgentFunction hook."""
+
+ credential_ids: list[str] | None = None
+ credential_ids_loaded_at: datetime | None = None
+ last_searched_at_by_credential: dict[str, datetime] = field(default_factory=dict)
+ seen_message_ids: set[str] = field(default_factory=set)
diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.py
index bd134ac7e..9925d003b 100644
--- a/skyvern/services/otp_service.py
+++ b/skyvern/services/otp_service.py
@@ -20,7 +20,9 @@ from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.aiohttp_helper import DEFAULT_REQUEST_TIMEOUT
from skyvern.forge.sdk.core.security import generate_skyvern_webhook_signature
from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
+from skyvern.forge.sdk.schemas.organizations import OrganizationAuthToken
from skyvern.forge.sdk.schemas.totp_codes import OTPType
+from skyvern.services.otp_gmail import GmailOTPVerificationContext
LOG = structlog.get_logger()
@@ -421,12 +423,8 @@ async def poll_otp_value(
timeout = timedelta(minutes=settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS)
start_datetime = datetime.utcnow()
timeout_datetime = start_datetime + timeout
- org_token = await app.DATABASE.organizations.get_valid_org_auth_token(
- organization_id, OrganizationAuthTokenType.api.value
- )
- if not org_token:
- LOG.error("Failed to get organization token when trying to get otp value")
- return None
+ gmail_created_after = created_after or start_datetime
+ db_created_after = created_after
LOG.info(
"Polling otp value",
task_id=task_id,
@@ -435,6 +433,8 @@ async def poll_otp_value(
)
consecutive_failures = 0
last_error_reason: str | None = None
+ org_token: OrganizationAuthToken | None = None
+ gmail_otp_context = GmailOTPVerificationContext()
while True:
await asyncio.sleep(10)
if datetime.utcnow() > timeout_datetime:
@@ -458,7 +458,16 @@ async def poll_otp_value(
)
otp_value: OTPValue | None = None
try:
+ # Keep an explicit webhook as the primary source. Gmail and DB are
+ # intentional backstops only when the webhook has no code yet.
if totp_verification_url:
+ if org_token is None:
+ org_token = await app.DATABASE.organizations.get_valid_org_auth_token(
+ organization_id, OrganizationAuthTokenType.api.value
+ )
+ if not org_token:
+ LOG.error("Failed to get organization token when trying to get otp value")
+ return None
otp_value = await _get_otp_value_from_url(
organization_id,
totp_verification_url,
@@ -467,14 +476,25 @@ async def poll_otp_value(
workflow_run_id=workflow_run_id,
workflow_permanent_id=workflow_permanent_id,
)
- elif totp_identifier:
+ if otp_value is None and totp_identifier:
+ otp_value = await _get_otp_value_from_gmail(
+ organization_id=organization_id,
+ totp_identifier=totp_identifier,
+ workflow_id=workflow_permanent_id,
+ workflow_run_id=workflow_run_id,
+ created_after=gmail_created_after,
+ context=gmail_otp_context,
+ )
+ if otp_value is None and totp_identifier:
+ # Preserve the historical DB behavior: callers that omit
+ # created_after may still read codes inserted before this poll began.
otp_value = await _get_otp_value_from_db(
organization_id,
totp_identifier,
task_id=task_id,
workflow_id=workflow_permanent_id,
workflow_run_id=workflow_run_id,
- created_after=created_after,
+ created_after=db_created_after,
)
except FailedToGetTOTPVerificationCode as e:
consecutive_failures += 1
@@ -611,6 +631,24 @@ async def _get_otp_value_from_url(
return otp_value
+async def _get_otp_value_from_gmail(
+ organization_id: str,
+ totp_identifier: str,
+ workflow_id: str | None = None,
+ workflow_run_id: str | None = None,
+ created_after: datetime | None = None,
+ context: GmailOTPVerificationContext | None = None,
+) -> OTPValue | None:
+ return await app.AGENT_FUNCTION.get_otp_value_from_gmail(
+ organization_id=organization_id,
+ totp_identifier=totp_identifier,
+ workflow_id=workflow_id,
+ workflow_run_id=workflow_run_id,
+ created_after=created_after,
+ context=context,
+ )
+
+
async def _get_otp_value_from_db(
organization_id: str,
totp_identifier: str,
diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.py
index 8dcf27d5f..130fcb2a9 100644
--- a/tests/unit/test_otp_service.py
+++ b/tests/unit/test_otp_service.py
@@ -522,6 +522,150 @@ class TestPollOtpValueRetry:
assert result == otp
assert mock_fetch.call_count == 3
+ @pytest.mark.asyncio
+ @patch("skyvern.services.otp_service.asyncio.sleep", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_db", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_gmail", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service.settings")
+ async def test_falls_back_to_db_when_gmail_has_no_code(
+ self,
+ mock_settings: MagicMock,
+ mock_gmail: AsyncMock,
+ mock_db: AsyncMock,
+ mock_sleep: AsyncMock,
+ ) -> None:
+ mock_settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS = 15
+ mock_gmail.return_value = None
+ otp = OTPValue(value="123456", type=OTPType.TOTP)
+ mock_db.return_value = otp
+
+ result = await poll_otp_value(
+ organization_id="o_test",
+ task_id="tsk_test",
+ workflow_run_id="wr_test",
+ workflow_permanent_id="wpid_test",
+ totp_identifier="[email protected]",
+ )
+
+ assert result == otp
+ mock_gmail.assert_awaited_once()
+ mock_db.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ @patch("skyvern.services.otp_service.asyncio.sleep", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_db", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_url", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_gmail", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service.app")
+ @patch("skyvern.services.otp_service.settings")
+ async def test_prefers_url_before_gmail_and_db_when_url_has_code(
+ self,
+ mock_settings: MagicMock,
+ mock_app: MagicMock,
+ mock_gmail: AsyncMock,
+ mock_url: AsyncMock,
+ mock_db: AsyncMock,
+ mock_sleep: AsyncMock,
+ ) -> None:
+ mock_settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS = 15
+ mock_app.DATABASE.organizations.get_valid_org_auth_token = AsyncMock(return_value=_mock_org_token())
+ otp = OTPValue(value="123456", type=OTPType.TOTP)
+ mock_url.return_value = otp
+ mock_gmail.return_value = OTPValue(value="654321", type=OTPType.TOTP)
+
+ result = await poll_otp_value(
+ organization_id="o_test",
+ task_id="tsk_test",
+ workflow_run_id="wr_test",
+ workflow_permanent_id="wpid_test",
+ totp_identifier="[email protected]",
+ totp_verification_url="https://example.com/mfa",
+ )
+
+ assert result == otp
+ mock_url.assert_awaited_once()
+ mock_gmail.assert_not_awaited()
+ mock_db.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ @patch("skyvern.services.otp_service.asyncio.sleep", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_db", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_url", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_gmail", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service.app")
+ @patch("skyvern.services.otp_service.settings")
+ async def test_falls_back_to_gmail_before_db_when_url_has_no_code(
+ self,
+ mock_settings: MagicMock,
+ mock_app: MagicMock,
+ mock_gmail: AsyncMock,
+ mock_url: AsyncMock,
+ mock_db: AsyncMock,
+ mock_sleep: AsyncMock,
+ ) -> None:
+ mock_settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS = 15
+ mock_app.DATABASE.organizations.get_valid_org_auth_token = AsyncMock(return_value=_mock_org_token())
+ mock_url.return_value = None
+ otp = OTPValue(value="123456", type=OTPType.TOTP)
+ mock_gmail.return_value = otp
+
+ result = await poll_otp_value(
+ organization_id="o_test",
+ task_id="tsk_test",
+ workflow_run_id="wr_test",
+ workflow_permanent_id="wpid_test",
+ totp_identifier="[email protected]",
+ totp_verification_url="https://example.com/mfa",
+ )
+
+ assert result == otp
+ mock_url.assert_awaited_once()
+ mock_gmail.assert_awaited_once()
+ mock_db.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ @patch("skyvern.services.otp_service.asyncio.sleep", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_db", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_url", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service._get_otp_value_from_gmail", new_callable=AsyncMock)
+ @patch("skyvern.services.otp_service.app")
+ @patch("skyvern.services.otp_service.settings")
+ @patch("skyvern.services.otp_service.datetime")
+ async def test_falls_back_to_db_after_url_and_gmail_preserves_db_cutoff_default(
+ self,
+ mock_datetime: MagicMock,
+ mock_settings: MagicMock,
+ mock_app: MagicMock,
+ mock_gmail: AsyncMock,
+ mock_url: AsyncMock,
+ mock_db: AsyncMock,
+ mock_sleep: AsyncMock,
+ ) -> None:
+ start = datetime(2026, 1, 1, 12, 0, 0)
+ mock_datetime.utcnow.return_value = start
+ mock_settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS = 15
+ mock_app.DATABASE.organizations.get_valid_org_auth_token = AsyncMock(return_value=_mock_org_token())
+ mock_url.return_value = None
+ mock_gmail.return_value = None
+ otp = OTPValue(value="123456", type=OTPType.TOTP)
+ mock_db.return_value = otp
+
+ result = await poll_otp_value(
+ organization_id="o_test",
+ task_id="tsk_test",
+ workflow_run_id="wr_test",
+ workflow_permanent_id="wpid_test",
+ totp_identifier="[email protected]",
+ totp_verification_url="https://example.com/mfa",
+ )
+
+ assert result == otp
+ mock_url.assert_awaited_once()
+ mock_gmail.assert_awaited_once()
+ mock_db.assert_awaited_once()
+ assert mock_gmail.await_args.kwargs["created_after"] == start
+ assert mock_db.await_args.kwargs["created_after"] is None
+
@pytest.mark.asyncio
async def test_does_not_short_circuit_during_extended_outage(self) -> None:
"""Persistent webhook errors must not exit polling before the wall-clock timeout fires (SKY-9553)."""