Skip to content

Automate browser based workflows with AI

fix: preserve unparsed OTP content for agent-time resolution (#7834)

LawyZheng1ffc8891e70f
diff --git a/alembic/versions/2026_07_29_0814-304752078903_add_parse_status_to_totp_codes.py b/alembic/versions/2026_07_29_0814-304752078903_add_parse_status_to_totp_codes.py
new file mode 100644
index 000000000..493e80ae1
--- /dev/null
+++ b/alembic/versions/2026_07_29_0814-304752078903_add_parse_status_to_totp_codes.py
@@ -0,0 +1,33 @@
+"""add parse status to totp codes
+
+Revision ID: 304752078903
+Revises: b0941c6bd241
+Create Date: 2026-07-29T08:14:26.430848+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "304752078903"
+down_revision: Union[str, None] = "b0941c6bd241"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ op.alter_column("totp_codes", "code", existing_type=sa.String(), nullable=True)
+ op.add_column(
+ "totp_codes",
+ sa.Column("parse_status", sa.String(), nullable=False, server_default="parsed"),
+ )
+
+
+def downgrade() -> None:
+ op.execute("DELETE FROM totp_codes WHERE parse_status = 'raw' OR code IS NULL")
+ op.drop_column("totp_codes", "parse_status")
+ op.alter_column("totp_codes", "code", existing_type=sa.String(), nullable=False)
diff --git a/skyvern/config.py b/skyvern/config.py
index f02054fa0..fb794c8aa 100644
--- a/skyvern/config.py
+++ b/skyvern/config.py
@@ -597,6 +597,8 @@ class Settings(BaseSettings):
# TOTP Settings
TOTP_LIFESPAN_MINUTES: int = 10
+ TOTP_RAW_FALLBACK_ENABLED: bool = False
+ TOTP_RAW_CONTENT_MAX_LENGTH: int = 65536
VERIFICATION_CODE_INITIAL_WAIT_TIME_SECS: int = 40
VERIFICATION_CODE_POLLING_TIMEOUT_MINS: int = 15
diff --git a/skyvern/core/script_generations/real_skyvern_page_ai.py b/skyvern/core/script_generations/real_skyvern_page_ai.py
index 35fda1fc3..2a1630b33 100644
--- a/skyvern/core/script_generations/real_skyvern_page_ai.py
+++ b/skyvern/core/script_generations/real_skyvern_page_ai.py
@@ -368,6 +368,7 @@ class RealSkyvernPageAi(SkyvernPageAi):
workflow_run_id=workflow_run_id,
totp_identifier=totp_identifier,
totp_verification_url=totp_url,
+ expected_otp_type=OTPType.TOTP,
)
if otp_value and otp_value.get_otp_type() == OTPType.TOTP:
verification_code = otp_value.value
diff --git a/skyvern/core/script_generations/script_skyvern_page.py b/skyvern/core/script_generations/script_skyvern_page.py
index 7d4a102a6..468b3af56 100644
--- a/skyvern/core/script_generations/script_skyvern_page.py
+++ b/skyvern/core/script_generations/script_skyvern_page.py
@@ -34,6 +34,7 @@ from skyvern.forge.sdk.api.files import (
from skyvern.forge.sdk.artifact.models import ArtifactType
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.db.utils import ACTION_TYPE_TO_CLASS
+from skyvern.forge.sdk.schemas.totp_codes import OTPType
from skyvern.forge.sdk.services.credentials import generate_totp_code
from skyvern.schemas.steps import AgentStepOutput
from skyvern.services.otp_service import poll_otp_value
@@ -864,6 +865,7 @@ class ScriptSkyvernPage(SkyvernPage):
workflow_run_id=workflow_run_id,
totp_verification_url=totp_url,
totp_identifier=totp_identifier,
+ expected_otp_type=OTPType.TOTP,
)
if totp_value:
# use the totp verification code
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index 18047643c..dc3d3722d 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -6541,6 +6541,7 @@ class ForgeAgent:
workflow_run_id=task.workflow_run_id,
totp_verification_url=task.totp_verification_url,
totp_identifier=task.totp_identifier,
+ expected_otp_type=OTPType.MAGIC_LINK,
)
if not otp_value or otp_value.get_otp_type() != OTPType.MAGIC_LINK:
return []
@@ -6579,7 +6580,7 @@ class ForgeAgent:
return json_response
LOG.info("Need verification code")
- otp_value = await resolve_otp_value(task)
+ otp_value = await resolve_otp_value(task, expected_otp_type=OTPType.TOTP)
if not otp_value or otp_value.get_otp_type() != OTPType.TOTP:
return json_response
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index 937bc4ecb..a1610b396 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -1085,12 +1085,13 @@ class TOTPCodeModel(Base):
workflow_id = Column(String, ForeignKey("workflows.workflow_id"))
workflow_run_id = Column(String, ForeignKey("workflow_runs.workflow_run_id"))
content = Column(String, nullable=False)
- code = Column(String, nullable=False)
+ code = Column(String)
source = Column(String)
created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False, index=True)
modified_at = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow, nullable=False)
expired_at = Column(DateTime, index=True)
otp_type = Column(String, server_default=sqlalchemy.text("'totp'"))
+ parse_status = Column(String, nullable=False, server_default=sqlalchemy.text("'parsed'"))
class ActionModel(Base):
diff --git a/skyvern/forge/sdk/db/repositories/otp.py b/skyvern/forge/sdk/db/repositories/otp.py
index d82478d3c..2b34aad0f 100644
--- a/skyvern/forge/sdk/db/repositories/otp.py
+++ b/skyvern/forge/sdk/db/repositories/otp.py
@@ -2,13 +2,13 @@ from __future__ import annotations
from datetime import datetime, timedelta, timezone
-from sqlalchemy import and_, asc, or_, select
+from sqlalchemy import and_, asc, or_, select, update
from skyvern.config import settings
from skyvern.forge.sdk.db._error_handling import db_operation
from skyvern.forge.sdk.db.base_repository import BaseRepository
from skyvern.forge.sdk.db.models import TOTPCodeModel
-from skyvern.forge.sdk.schemas.totp_codes import OTPType, TOTPCode
+from skyvern.forge.sdk.schemas.totp_codes import OTPType, RawTOTPCode, TOTPCode
class OTPRepository(BaseRepository):
@@ -46,6 +46,7 @@ class OTPRepository(BaseRepository):
select(TOTPCodeModel)
.filter_by(organization_id=organization_id)
.filter_by(totp_identifier=totp_identifier)
+ .filter_by(parse_status="parsed")
.filter(
TOTPCodeModel.created_at > datetime.now(timezone.utc) - timedelta(minutes=valid_lifespan_minutes)
)
@@ -86,6 +87,7 @@ class OTPRepository(BaseRepository):
query = (
select(TOTPCodeModel)
.filter_by(organization_id=organization_id)
+ .filter_by(parse_status="parsed")
.filter(
TOTPCodeModel.created_at > datetime.now(timezone.utc) - timedelta(minutes=valid_lifespan_minutes)
)
@@ -113,7 +115,7 @@ class OTPRepository(BaseRepository):
workflow_run_id filtering.
"""
async with self.Session() as session:
- query = select(TOTPCodeModel).filter_by(organization_id=organization_id)
+ query = select(TOTPCodeModel).filter_by(organization_id=organization_id).filter_by(parse_status="parsed")
if valid_lifespan_minutes is not None:
query = query.filter(
@@ -163,3 +165,97 @@ class OTPRepository(BaseRepository):
await session.commit()
await session.refresh(new_totp_code)
return TOTPCode.model_validate(new_totp_code)
+
+ @db_operation("create_raw_otp_code")
+ async def create_raw_otp_code(
+ self,
+ organization_id: str,
+ totp_identifier: str,
+ content: str,
+ task_id: str | None = None,
+ workflow_id: str | None = None,
+ workflow_run_id: str | None = None,
+ source: str | None = None,
+ expired_at: datetime | None = None,
+ ) -> RawTOTPCode:
+ async with self.Session() as session:
+ row = TOTPCodeModel(
+ organization_id=organization_id,
+ totp_identifier=totp_identifier,
+ content=content,
+ code=None,
+ otp_type=None,
+ parse_status="raw",
+ task_id=task_id or None,
+ workflow_id=workflow_id or None,
+ workflow_run_id=workflow_run_id or None,
+ source=source,
+ expired_at=expired_at,
+ )
+ session.add(row)
+ await session.commit()
+ await session.refresh(row)
+ return RawTOTPCode.model_validate(row)
+
+ @db_operation("get_raw_otp_codes")
+ async def get_raw_otp_codes(
+ self,
+ organization_id: str,
+ totp_identifier: str,
+ valid_lifespan_minutes: int = settings.TOTP_LIFESPAN_MINUTES,
+ workflow_run_id: str | None = None,
+ include_unscoped_workflow_run: bool = False,
+ created_after: datetime | None = None,
+ excluded_ids: set[str] | None = None,
+ limit: int | None = None,
+ ) -> list[RawTOTPCode]:
+ async with self.Session() as session:
+ query = (
+ select(TOTPCodeModel)
+ .filter_by(
+ organization_id=organization_id,
+ totp_identifier=totp_identifier,
+ parse_status="raw",
+ )
+ .filter(
+ TOTPCodeModel.created_at > datetime.now(timezone.utc) - timedelta(minutes=valid_lifespan_minutes)
+ )
+ )
+ if workflow_run_id is not None and include_unscoped_workflow_run:
+ query = query.filter(
+ or_(TOTPCodeModel.workflow_run_id == workflow_run_id, TOTPCodeModel.workflow_run_id.is_(None))
+ )
+ elif workflow_run_id is not None:
+ query = query.filter(TOTPCodeModel.workflow_run_id == workflow_run_id)
+ if created_after is not None:
+ query = query.filter(TOTPCodeModel.created_at >= created_after)
+ if excluded_ids:
+ query = query.filter(TOTPCodeModel.totp_code_id.not_in(excluded_ids))
+ query = query.order_by(TOTPCodeModel.created_at.desc())
+ if limit is not None:
+ query = query.limit(limit)
+ rows = (await session.scalars(query)).all()
+ return [RawTOTPCode.model_validate(row) for row in rows]
+
+ @db_operation("promote_raw_otp_code")
+ async def promote_raw_otp_code(
+ self,
+ totp_code_id: str,
+ organization_id: str,
+ code: str,
+ otp_type: OTPType,
+ ) -> TOTPCode | None:
+ async with self.Session() as session:
+ query = (
+ update(TOTPCodeModel)
+ .where(
+ TOTPCodeModel.totp_code_id == totp_code_id,
+ TOTPCodeModel.organization_id == organization_id,
+ TOTPCodeModel.parse_status == "raw",
+ )
+ .values(code=code, otp_type=otp_type, parse_status="parsed", modified_at=datetime.now(timezone.utc))
+ .returning(TOTPCodeModel)
+ )
+ row = (await session.scalars(query)).one_or_none()
+ await session.commit()
+ return TOTPCode.model_validate(row) if row else None
diff --git a/skyvern/forge/sdk/routes/credentials.py b/skyvern/forge/sdk/routes/credentials.py
index 9b44f20e2..839a3f8f7 100644
--- a/skyvern/forge/sdk/routes/credentials.py
+++ b/skyvern/forge/sdk/routes/credentials.py
@@ -111,7 +111,7 @@ from skyvern.forge.sdk.schemas.organizations import (
Organization,
TestConnectionResponse,
)
-from skyvern.forge.sdk.schemas.totp_codes import OTPType, TOTPCode, TOTPCodeCreate
+from skyvern.forge.sdk.schemas.totp_codes import OTPType, RawTOTPCodeAccepted, TOTPCode, TOTPCodeCreate
from skyvern.forge.sdk.services import org_auth_service
from skyvern.forge.sdk.services.bitwarden import BitwardenService
from skyvern.forge.sdk.services.credential.credential_vault_service import CredentialVaultService
@@ -405,11 +405,15 @@ async def fetch_credential_item_background(item_id: str) -> None:
LOG.exception("Failed to fetch credential item from Bitwarden in background", item_id=item_id, error=str(e))
-@legacy_base_router.post("/totp")
+@legacy_base_router.post(
+ "/totp",
+ responses={202: {"model": RawTOTPCodeAccepted, "description": "Raw OTP content accepted for later parsing"}},
+)
@legacy_base_router.post("/totp/", include_in_schema=False)
@base_router.post(
"/credentials/totp",
response_model=TOTPCode,
+ responses={202: {"model": RawTOTPCodeAccepted, "description": "Raw OTP content accepted for later parsing"}},
summary="Send TOTP code",
description="Forward a TOTP (2FA, MFA) email or sms message containing the code to Skyvern. This endpoint stores the code in database so that Skyvern can use it while running tasks/workflows.",
tags=["Credentials"],
@@ -428,6 +432,7 @@ async def fetch_credential_item_background(item_id: str) -> None:
@base_router.post(
"/credentials/totp/",
response_model=TOTPCode,
+ responses={202: {"model": RawTOTPCodeAccepted, "description": "Raw OTP content accepted for later parsing"}},
include_in_schema=False,
)
async def send_totp_code(
@@ -488,16 +493,40 @@ async def send_totp_code(
)
if not otp_value:
- LOG.error(
- "Failed to parse otp login",
+ if not settings.TOTP_RAW_FALLBACK_ENABLED:
+ LOG.error(
+ "Failed to parse otp login",
+ organization_id=curr_org.organization_id,
+ totp_identifier=redacted_totp_identifier,
+ task_id=data.task_id,
+ workflow_id=data.workflow_id,
+ workflow_run_id=data.workflow_run_id,
+ content_length=len(data.content),
+ )
+ raise HTTPException(status_code=400, detail="Failed to parse otp login")
+ if len(data.content) > settings.TOTP_RAW_CONTENT_MAX_LENGTH:
+ raise HTTPException(status_code=400, detail="Failed to parse otp login")
+ raw_row = await app.DATABASE.otp.create_raw_otp_code(
organization_id=curr_org.organization_id,
- totp_identifier=redacted_totp_identifier,
+ totp_identifier=data.totp_identifier,
+ content=data.content,
task_id=data.task_id,
workflow_id=data.workflow_id,
workflow_run_id=data.workflow_run_id,
+ source=data.source,
+ expired_at=data.expired_at,
+ )
+ LOG.info(
+ "Persisted raw OTP content",
+ organization_id=curr_org.organization_id,
+ totp_identifier=redacted_totp_identifier,
+ totp_code_id=raw_row.totp_code_id,
content_length=len(data.content),
)
- raise HTTPException(status_code=400, detail="Failed to parse otp login")
+ pending = RawTOTPCodeAccepted(totp_code_id=raw_row.totp_code_id)
+ return Response( # type: ignore[return-value]
+ content=pending.model_dump_json(), status_code=202, media_type="application/json"
+ )
return await app.DATABASE.otp.create_otp_code(
organization_id=curr_org.organization_id,
diff --git a/skyvern/forge/sdk/schemas/totp_codes.py b/skyvern/forge/sdk/schemas/totp_codes.py
index 0c5848edb..60e1a1ad4 100644
--- a/skyvern/forge/sdk/schemas/totp_codes.py
+++ b/skyvern/forge/sdk/schemas/totp_codes.py
@@ -1,5 +1,6 @@
from datetime import datetime
from enum import StrEnum
+from typing import Literal
from pydantic import BaseModel, ConfigDict, Field, field_validator
@@ -83,3 +84,23 @@ class TOTPCode(TOTPCodeCreate):
created_at: datetime = Field(..., description="The timestamp when the TOTP code was created.")
modified_at: datetime = Field(..., description="The timestamp when the TOTP code was modified.")
otp_type: OTPType | None = Field(None, description="The type of the OTP code.")
+
+
+class RawTOTPCode(BaseModel):
+ model_config = ConfigDict(from_attributes=True)
+
+ totp_code_id: str
+ totp_identifier: str
+ organization_id: str
+ content: str
+ task_id: str | None
+ workflow_id: str | None
+ workflow_run_id: str | None
+ source: str | None
+ created_at: datetime
+ expired_at: datetime | None
+
+
+class RawTOTPCodeAccepted(BaseModel):
+ totp_code_id: str
+ status: Literal["raw_pending"] = "raw_pending"
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.py
index 3e633c20f..a265ed501 100644
--- a/skyvern/forge/sdk/workflow/models/block.py
+++ b/skyvern/forge/sdk/workflow/models/block.py
@@ -3848,6 +3848,7 @@ async def _poll_code_block_otp(
workflow_permanent_id=workflow_run.workflow_permanent_id,
totp_identifier=totp_identifier,
created_after=workflow_run.started_at,
+ expected_otp_type=OTPType.TOTP,
),
timeout=budget_seconds,
)
diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.py
index 06b43fd15..030f46b55 100644
--- a/skyvern/services/otp_service.py
+++ b/skyvern/services/otp_service.py
@@ -1,6 +1,7 @@
import asyncio
import json
import re
+from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any
@@ -26,6 +27,14 @@ from skyvern.services.otp_gmail import GmailOTPVerificationContext
LOG = structlog.get_logger()
+_RAW_OTP_REPARSE_LIMIT = 3
+
+
+@dataclass
+class RawOTPVerificationContext:
+ misses: set[tuple[str, OTPType]] = field(default_factory=set)
+
+
_MFA_PARAMETER_KEY_HINTS = ("mfa", "otp", "verification")
# Keys that contain an MFA hint but are TOTP *metadata*, not actual OTP codes.
# "totpidentifier" matches "otp" but carries a lookup key, not a 6-digit code.
@@ -387,7 +396,7 @@ def try_generate_totp_from_credential(workflow_run_id: str | None) -> OTPValue |
return try_generate_totp_for_credential(workflow_run_context, candidate_keys[0], workflow_run_id)
-async def resolve_otp_value(task: "Task") -> OTPValue | None:
+async def resolve_otp_value(task: "Task", expected_otp_type: OTPType | None = None) -> OTPValue | None:
"""Resolve the OTP value to use for a verification step.
Priority is payload -> credential-backed TOTP -> webhook polling. The
@@ -427,6 +436,7 @@ async def resolve_otp_value(task: "Task") -> OTPValue | None:
totp_verification_url=task.totp_verification_url,
totp_identifier=task.totp_identifier,
created_after=run_started_at,
+ expected_otp_type=expected_otp_type,
)
return None
@@ -441,6 +451,7 @@ async def poll_otp_value(
totp_verification_url: str | None = None,
totp_identifier: str | None = None,
created_after: datetime | None = None,
+ expected_otp_type: OTPType | None = None,
) -> OTPValue | None:
timeout = timedelta(minutes=settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS)
start_datetime = datetime.utcnow()
@@ -457,6 +468,7 @@ async def poll_otp_value(
last_error_reason: str | None = None
org_token: OrganizationAuthToken | None = None
gmail_otp_context = GmailOTPVerificationContext()
+ raw_otp_context = RawOTPVerificationContext()
while True:
await asyncio.sleep(10)
if datetime.utcnow() > timeout_datetime:
@@ -519,6 +531,8 @@ async def poll_otp_value(
workflow_id=workflow_permanent_id,
workflow_run_id=workflow_run_id,
created_after=db_created_after,
+ expected_otp_type=expected_otp_type,
+ raw_context=raw_otp_context,
)
except FailedToGetTOTPVerificationCode as e:
consecutive_failures += 1
@@ -680,6 +694,8 @@ async def _get_otp_value_from_db(
workflow_id: str | None = None,
workflow_run_id: str | None = None,
created_after: datetime | None = None,
+ expected_otp_type: OTPType | None = None,
+ raw_context: RawOTPVerificationContext | None = None,
) -> OTPValue | None:
# Email/SMS deliveries can arrive through /v1/credentials/totp without run
# scope, so include both exact run matches and unscoped rows in SQL.
@@ -700,4 +716,53 @@ async def _get_otp_value_from_db(
if totp_code.expired_at and totp_code.expired_at < datetime.utcnow():
continue
return OTPValue(value=totp_code.code, type=totp_code.otp_type)
+ if expected_otp_type is None:
+ return None
+ context = raw_context or RawOTPVerificationContext()
+ raw_rows = await app.DATABASE.otp.get_raw_otp_codes(
+ organization_id=organization_id,
+ totp_identifier=totp_identifier,
+ workflow_run_id=workflow_run_id,
+ include_unscoped_workflow_run=workflow_run_id is not None,
+ created_after=created_after,
+ excluded_ids={row_id for row_id, otp_type in context.misses if otp_type == expected_otp_type},
+ )
+ attempts = 0
+ for row in raw_rows:
+ cache_key = (row.totp_code_id, expected_otp_type)
+ if row.workflow_run_id and workflow_run_id and row.workflow_run_id != workflow_run_id:
+ continue
+ if row.workflow_id and workflow_id and row.workflow_id != workflow_id:
+ continue
+ if row.task_id and row.task_id != task_id:
+ continue
+ if row.expired_at and row.expired_at < datetime.utcnow():
+ continue
+ if attempts >= _RAW_OTP_REPARSE_LIMIT:
+ break
+ attempts += 1
+ try:
+ otp_value = await parse_otp_login(row.content, organization_id, enforced_otp_type=expected_otp_type)
+ except Exception:
+ LOG.warning(
+ "Raw OTP reparse failed", totp_code_id=row.totp_code_id, otp_type=expected_otp_type, exc_info=True
+ )
+ continue
+ if otp_value is None or otp_value.get_otp_type() != expected_otp_type:
+ context.misses.add(cache_key)
+ continue
+ await app.DATABASE.otp.promote_raw_otp_code(
+ totp_code_id=row.totp_code_id,
+ organization_id=organization_id,
+ code=otp_value.value,
+ otp_type=expected_otp_type,
+ )
+ LOG.info(
+ "Promoted raw OTP content",
+ organization_id=organization_id,
+ totp_code_id=row.totp_code_id,
+ otp_type=expected_otp_type,
+ content_length=len(row.content),
+ )
+ return otp_value
return None
diff --git a/skyvern/webeye/actions/parse_actions.py b/skyvern/webeye/actions/parse_actions.py
index 2b20f5935..96ab48278 100644
--- a/skyvern/webeye/actions/parse_actions.py
+++ b/skyvern/webeye/actions/parse_actions.py
@@ -997,6 +997,7 @@ async def generate_cua_fallback_actions(
workflow_run_id=task.workflow_run_id,
totp_verification_url=task.totp_verification_url,
totp_identifier=task.totp_identifier,
+ expected_otp_type=OTPType.MAGIC_LINK,
)
if not otp_value or otp_value.get_otp_type() != OTPType.MAGIC_LINK:
raise NoTOTPVerificationCodeFound()
@@ -1047,7 +1048,7 @@ async def generate_cua_fallback_actions(
elif skyvern_action_type == "get_verification_code":
try:
- otp_value = await resolve_otp_value(task)
+ otp_value = await resolve_otp_value(task, expected_otp_type=OTPType.TOTP)
except NoTOTPVerificationCodeFound:
otp_value = None
reasoning_suffix = "No verification code found"
diff --git a/tests/unit/forge/sdk/db/test_repositories.py b/tests/unit/forge/sdk/db/test_repositories.py
index 3ef023b5f..ff2abcb25 100644
--- a/tests/unit/forge/sdk/db/test_repositories.py
+++ b/tests/unit/forge/sdk/db/test_repositories.py
@@ -80,6 +80,18 @@ async def test_otp_repository_can_include_unscoped_workflow_run_rows_in_sql():
assert "totp_codes.workflow_run_id = :workflow_run_id_1" in sql
assert "totp_codes.workflow_run_id IS NULL" in sql
assert " OR " in sql
+ assert "totp_codes.parse_status = :parse_status_1" in sql
+ await repo.get_raw_otp_codes(
+ organization_id="o_test",
+ totp_identifier="[email protected]",
+ workflow_run_id="wr_test",
+ include_unscoped_workflow_run=True,
+ created_after=datetime(2026, 1, 1, tzinfo=timezone.utc),
+ )
+ sql = str(session.query)
+ assert "totp_codes.parse_status = :parse_status_1" in sql
+ assert "totp_codes.workflow_run_id IS NULL" in sql
+ assert "totp_codes.created_at >=" in sql
@pytest.mark.asyncio
@@ -125,6 +137,45 @@ async def test_otp_repository_stores_blank_run_scoping_ids_as_null():
assert session.added.task_id is None
+async def test_otp_repository_creates_raw_row_without_fabricated_code():
+ from skyvern.forge.sdk.db.repositories.otp import OTPRepository
+
+ class CapturingWriteSession:
+ added = None
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, *_args):
+ return False
+
+ def add(self, obj):
+ self.added = obj
+
+ async def commit(self):
+ return None
+
+ async def refresh(self, obj):
+ obj.totp_code_id = "otp_raw"
+ obj.created_at = obj.modified_at = datetime(2026, 1, 1, tzinfo=timezone.utc)
+
+ session = CapturingWriteSession()
+ repo = OTPRepository(session_factory=lambda: session, debug_enabled=False)
+ result = await repo.create_raw_otp_code(
+ organization_id="o_test",
+ totp_identifier="[email protected]",
+ content="unparsed content",
+ workflow_run_id="",
+ )
+
+ assert result.totp_code_id == "otp_raw"
+ assert session.added.code is None
+ assert session.added.otp_type is None
+ assert session.added.parse_status == "raw"
+ assert session.added.workflow_run_id is None
+
+
def test_debug_repository_instantiation():
from skyvern.forge.sdk.db.repositories.debug import DebugRepository
diff --git a/tests/unit/test_agent_otp_routing.py b/tests/unit/test_agent_otp_routing.py
index f927eb5c1..a359ea025 100644
--- a/tests/unit/test_agent_otp_routing.py
+++ b/tests/unit/test_agent_otp_routing.py
@@ -63,7 +63,7 @@ async def test_handle_potential_verification_code_uses_resolver_without_db_looku
agent = ForgeAgent.__new__(ForgeAgent)
await agent.handle_potential_verification_code(task, step, scraped_page, browser_state, json_response)
- resolver.assert_awaited_once_with(task)
+ resolver.assert_awaited_once_with(task, expected_otp_type=OTPType.TOTP)
db_get.assert_not_awaited()
@@ -116,7 +116,7 @@ async def test_handle_potential_verification_code_skips_polling_when_credential_
finally:
skyvern_context.reset()
- resolver.assert_awaited_once_with(task)
+ resolver.assert_awaited_once_with(task, expected_otp_type=OTPType.TOTP)
poll.assert_not_awaited()
db_get.assert_not_awaited()
rescrape.assert_awaited_once()
diff --git a/tests/unit/test_credentials_totp_route_logging.py b/tests/unit/test_credentials_totp_route_logging.py
index eb4fbaf05..dea0311b3 100644
--- a/tests/unit/test_credentials_totp_route_logging.py
+++ b/tests/unit/test_credentials_totp_route_logging.py
@@ -1,16 +1,21 @@
+import json
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
import structlog.testing
-from fastapi import HTTPException
+from fastapi import FastAPI, HTTPException
from skyvern.forge.sdk.routes import credentials
+from skyvern.forge.sdk.routes.routers import base_router, legacy_base_router
from skyvern.forge.sdk.schemas.totp_codes import TOTPCodeCreate
-def _database_with_otp_create(create_otp_code: AsyncMock) -> SimpleNamespace:
- return SimpleNamespace(otp=SimpleNamespace(create_otp_code=create_otp_code))
+def _database_with_otp_create(
+ create_otp_code: AsyncMock, create_raw_otp_code: AsyncMock | None = None
+) -> SimpleNamespace:
+ otp = SimpleNamespace(create_otp_code=create_otp_code, create_raw_otp_code=create_raw_otp_code or AsyncMock())
+ return SimpleNamespace(otp=otp)
def _assert_raw_values_not_logged(logs: list[dict[str, object]], *raw_values: str) -> None:
@@ -57,8 +62,11 @@ async def test_send_totp_code_parse_failure_log_redacts_identifier_and_content(
raw_identifier = "[email protected]"
raw_content = "Use 135790 to finish sign in for [email protected]."
create_otp_code = AsyncMock()
- monkeypatch.setattr(credentials.app, "DATABASE", _database_with_otp_create(create_otp_code))
+ create_raw_otp_code = AsyncMock(return_value=SimpleNamespace(totp_code_id="otp_raw"))
+ monkeypatch.setattr(credentials.app, "DATABASE", _database_with_otp_create(create_otp_code, create_raw_otp_code))
monkeypatch.setattr(credentials, "parse_otp_login", AsyncMock(return_value=None))
+ assert type(credentials.settings).model_fields["TOTP_RAW_FALLBACK_ENABLED"].default is False
+ monkeypatch.setattr(credentials.settings, "TOTP_RAW_FALLBACK_ENABLED", False)
with structlog.testing.capture_logs() as logs:
with pytest.raises(HTTPException) as exc_info:
@@ -72,6 +80,7 @@ async def test_send_totp_code_parse_failure_log_redacts_identifier_and_content(
assert exc_info.value.__cause__ is None
assert exc_info.value.__context__ is None
create_otp_code.assert_not_awaited()
+ create_raw_otp_code.assert_not_awaited()
error_log = next((r for r in logs if r.get("event") == "Failed to parse otp login"), None)
assert error_log is not None
@@ -82,6 +91,19 @@ async def test_send_totp_code_parse_failure_log_redacts_identifier_and_content(
_assert_raw_values_not_logged(logs, raw_identifier, raw_content, "135790")
+def test_send_totp_code_openapi_declares_distinct_200_and_202_schemas() -> None:
+ fastapi_app = FastAPI()
+ fastapi_app.include_router(base_router, prefix="/v1")
+ fastapi_app.include_router(legacy_base_router, prefix="/api/v1")
+
+ responses = fastapi_app.openapi()["paths"]["/v1/credentials/totp"]["post"]["responses"]
+ assert responses["200"]["content"]["application/json"]["schema"]["$ref"].endswith("/TOTPCode")
+ assert responses["202"]["content"]["application/json"]["schema"]["$ref"].endswith("/RawTOTPCodeAccepted")
+
+ legacy_route = next(route for route in legacy_base_router.routes if route.path == "/totp")
+ assert legacy_route.responses[202]["model"].__name__ == "RawTOTPCodeAccepted"
+
+
@pytest.mark.asyncio
async def test_send_totp_code_parser_exception_log_redacts_raw_exception_context(
monkeypatch: pytest.MonkeyPatch,
@@ -125,7 +147,7 @@ async def test_send_totp_code_parser_exception_log_redacts_raw_exception_context
@pytest.mark.asyncio
-async def test_send_totp_code_off_schema_llm_response_yields_400_not_502(
+async def test_send_totp_code_off_schema_llm_response_yields_202_not_502(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from skyvern.services import otp_service
@@ -133,7 +155,9 @@ async def test_send_totp_code_off_schema_llm_response_yields_400_not_502(
raw_identifier = "[email protected]"
raw_content = "Long email body requesting OTP 424242 for [email protected]."
create_otp_code = AsyncMock()
- monkeypatch.setattr(credentials.app, "DATABASE", _database_with_otp_create(create_otp_code))
+ create_raw_otp_code = AsyncMock(return_value=SimpleNamespace(totp_code_id="otp_raw"))
+ monkeypatch.setattr(credentials.app, "DATABASE", _database_with_otp_create(create_otp_code, create_raw_otp_code))
+ monkeypatch.setattr(credentials.settings, "TOTP_RAW_FALLBACK_ENABLED", True)
async def off_schema_handler(*_args: object, **_kwargs: object) -> dict[str, object]:
return {"otp_type": "totp", "otp_value": "424242"}
@@ -142,13 +166,34 @@ async def test_send_totp_code_off_schema_llm_response_yields_400_not_502(
monkeypatch.setattr(otp_service.app, "SECONDARY_LLM_API_HANDLER", off_schema_handler, raising=False)
with structlog.testing.capture_logs() as logs:
- with pytest.raises(HTTPException) as exc_info:
- await credentials.send_totp_code(
- TOTPCodeCreate(totp_identifier=raw_identifier, content=raw_content),
- curr_org=SimpleNamespace(organization_id="o_test"),
- )
+ response = await credentials.send_totp_code(
+ TOTPCodeCreate(totp_identifier=raw_identifier, content=raw_content),
+ curr_org=SimpleNamespace(organization_id="o_test"),
+ )
+
+ assert response.status_code == 202
+ assert json.loads(response.body) == {"totp_code_id": "otp_raw", "status": "raw_pending"}
+ create_otp_code.assert_not_awaited()
+ create_raw_otp_code.assert_awaited_once()
+ _assert_raw_values_not_logged(logs, raw_identifier, raw_content, "424242")
+
+
+async def test_send_totp_code_parse_miss_over_cap_stays_400(monkeypatch: pytest.MonkeyPatch) -> None:
+ create_otp_code = AsyncMock()
+ create_raw_otp_code = AsyncMock()
+ monkeypatch.setattr(credentials.app, "DATABASE", _database_with_otp_create(create_otp_code, create_raw_otp_code))
+ monkeypatch.setattr(credentials, "parse_otp_login", AsyncMock(return_value=None))
+ monkeypatch.setattr(credentials.settings, "TOTP_RAW_FALLBACK_ENABLED", True)
+ monkeypatch.setattr(credentials.settings, "TOTP_RAW_CONTENT_MAX_LENGTH", 12)
+
+ with pytest.raises(HTTPException) as exc_info:
+ await credentials.send_totp_code(
+ TOTPCodeCreate(totp_identifier="[email protected]", content="x" * 13),
+ curr_org=SimpleNamespace(organization_id="o_test"),
+ )
assert exc_info.value.status_code == 400
assert exc_info.value.detail == "Failed to parse otp login"
create_otp_code.assert_not_awaited()
- _assert_raw_values_not_logged(logs, raw_identifier, raw_content, "424242")
+ create_raw_otp_code.assert_not_awaited()
diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.py
index f8dcbcf4d..c045e4959 100644
--- a/tests/unit/test_otp_service.py
+++ b/tests/unit/test_otp_service.py
@@ -14,6 +14,7 @@ from skyvern.exceptions import FailedToGetTOTPVerificationCode, NoTOTPVerificati
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.schemas.totp_codes import OTPType
+from skyvern.services import otp_service
from skyvern.services.otp_service import (
OTPValue,
_get_otp_value_from_db,
@@ -1544,3 +1545,99 @@ async def test_get_otp_value_from_db_preserves_unscoped_lookup_without_workflow_
assert result == OTPValue(value="111111", type=OTPType.TOTP)
assert get_otp_codes.await_args.kwargs["workflow_run_id"] is None
assert get_otp_codes.await_args.kwargs["include_unscoped_workflow_run"] is False
+
+
+def _raw_otp_row(totp_code_id: str = "otp_raw", task_id: str | None = None) -> SimpleNamespace:
+ return SimpleNamespace(
+ totp_code_id=totp_code_id,
+ content="open the sign-in link",
+ workflow_run_id=None,
+ workflow_id=None,
+ task_id=task_id,
+ expired_at=None,
+ )
+
+
+async def test_raw_otp_reparse_promotes_expected_type() -> None:
+ raw = _raw_otp_row()
+ ineligible = [SimpleNamespace(**(vars(raw) | {"totp_code_id": f"skip_{i}", "task_id": "other"})) for i in range(3)]
+ with (
+ patch("skyvern.services.otp_service.app") as mock_app,
+ patch(
+ "skyvern.services.otp_service.parse_otp_login",
+ new=AsyncMock(return_value=OTPValue(value="https://example.test/login", type=OTPType.MAGIC_LINK)),
+ ) as parse,
+ ):
+ mock_app.DATABASE.otp.get_otp_codes = AsyncMock(return_value=[])
+ mock_app.DATABASE.otp.get_raw_otp_codes = AsyncMock(return_value=[*ineligible, raw])
+ mock_app.DATABASE.otp.promote_raw_otp_code = AsyncMock(return_value=SimpleNamespace())
+ result = await _get_otp_value_from_db(
+ "o_test",
+ expected_otp_type=OTPType.MAGIC_LINK,
+ raw_context=otp_service.RawOTPVerificationContext(),
+ )
+ assert result == OTPValue(value="https://example.test/login", type=OTPType.MAGIC_LINK)
+ parse.assert_awaited_once_with(raw.content, "o_test", enforced_otp_type=OTPType.MAGIC_LINK)
+ mock_app.DATABASE.otp.promote_raw_otp_code.assert_awaited_once()
+
+
+async def test_raw_otp_reparse_exception_retries_without_negative_cache() -> None:
+ raw, context = _raw_otp_row(), otp_service.RawOTPVerificationContext()
+ parsed = OTPValue(value="https://example.test/login", type=OTPType.MAGIC_LINK)
+ kwargs = {"expected_otp_type": OTPType.MAGIC_LINK, "raw_context": context}
+ with (
+ patch("skyvern.services.otp_service.app") as mock_app,
+ patch("skyvern.services.otp_service.parse_otp_login", new=AsyncMock(side_effect=[RuntimeError, parsed])),
+ ):
+ mock_app.DATABASE.otp.get_otp_codes = AsyncMock(return_value=[])
+ mock_app.DATABASE.otp.get_raw_otp_codes = AsyncMock(return_value=[raw])
+ mock_app.DATABASE.otp.promote_raw_otp_code = AsyncMock(return_value=SimpleNamespace())
+ assert await _get_otp_value_from_db("o_test", "[email protected]", **kwargs) is None
+ assert context.misses == set()
+ assert await _get_otp_value_from_db("o_test", "[email protected]", **kwargs) == parsed
+ mock_app.DATABASE.otp.promote_raw_otp_code.assert_awaited_once()
+
+
+async def test_raw_otp_wrong_type_is_negative_cached_per_session() -> None:
+ rows = [_raw_otp_row(f"otp_raw_{index}") for index in range(4)]
+ context = otp_service.RawOTPVerificationContext()
+
+ async def get_raw_otp_codes(**kwargs: object) -> list[SimpleNamespace]:
+ excluded_ids = kwargs["excluded_ids"]
+ return [row for row in rows if row.totp_code_id not in excluded_ids][:3]
+
+ with (
+ patch("skyvern.services.otp_service.app") as mock_app,
+ patch(
+ "skyvern.services.otp_service.parse_otp_login",
+ new=AsyncMock(return_value=OTPValue(value="123456", type=OTPType.TOTP)),
+ ) as parse,
+ ):
+ mock_app.DATABASE.otp.get_otp_codes = AsyncMock(return_value=[])
+ mock_app.DATABASE.otp.get_raw_otp_codes = AsyncMock(side_effect=get_raw_otp_codes)
+ mock_app.DATABASE.otp.promote_raw_otp_code = AsyncMock()
+ for _ in range(2):
+ assert (
+ await _get_otp_value_from_db(
+ "o_test",
+ expected_otp_type=OTPType.MAGIC_LINK,
+ raw_context=context,
+ )
+ is None
+ )
+ assert parse.await_count == 4
+ mock_app.DATABASE.otp.promote_raw_otp_code.assert_not_awaited()
+
+
+async def test_raw_otp_rows_are_skipped_without_expected_type() -> None:
+ with patch("skyvern.services.otp_service.app") as mock_app:
+ mock_app.DATABASE.otp.get_otp_codes = AsyncMock(return_value=[])
+ mock_app.DATABASE.otp.get_raw_otp_codes = AsyncMock()
+ assert await _get_otp_value_from_db("o_test", "[email protected]") is None
+ mock_app.DATABASE.otp.get_raw_otp_codes.assert_not_awaited()
diff --git a/tests/unit/test_resolve_otp_value.py b/tests/unit/test_resolve_otp_value.py
index 4bb001dc1..86e4f870f 100644
--- a/tests/unit/test_resolve_otp_value.py
+++ b/tests/unit/test_resolve_otp_value.py
@@ -118,7 +118,7 @@ async def test_falls_through_to_poll_when_no_payload_or_credential(monkeypatch:
patch("skyvern.services.otp_service.try_generate_totp_from_credential", return_value=None),
patch("skyvern.services.otp_service.poll_otp_value", new=AsyncMock(return_value=poll_value)) as poll,
):
- result = await resolve_otp_value(task)
+ result = await resolve_otp_value(task, expected_otp_type=OTPType.TOTP)
assert result is poll_value
db_get.assert_awaited_once_with("wr_test")
@@ -131,6 +131,7 @@ async def test_falls_through_to_poll_when_no_payload_or_credential(monkeypatch:
assert kwargs["workflow_permanent_id"] == "wpid_test"
assert kwargs["totp_verification_url"] == "https://example.com/webhook"
assert kwargs["totp_identifier"] == "[email protected]"
+ assert kwargs["expected_otp_type"] == OTPType.TOTP
@pytest.mark.asyncio