from __future__ import annotations
from html import escape
from typing import Mapping, Sequence
from urllib.parse import unquote, urlencode, urlsplit
AUTH_STYLESHEET = "/static/auth.css?v=20260806-06"
def _first(query: Mapping[str, Sequence[str]], name: str) -> str:
values = query.get(name, ())
return values[0] if values else ""
def safe_return_target(query: Mapping[str, Sequence[str]]) -> str | None:
"""Return a same-origin path, never an absolute or scheme-relative URL."""
candidate = _first(query, "openid.return_to").strip()
if not candidate or any(ord(character) < 32 for character in candidate):
return None
parsed = urlsplit(candidate)
if parsed.scheme or parsed.netloc:
return None
if not parsed.path.startswith("/") or parsed.path.startswith("//") or "\\" in parsed.path:
return None
decoded_path = parsed.path
for _ in range(2):
decoded_path = unquote(decoded_path)
if (
decoded_path.startswith("//")
or "\\" in decoded_path
or any(ord(character) < 32 for character in decoded_path)
):
return None
return candidate
def _logo() -> str:
return ''
def _hidden_return_target(query: Mapping[str, Sequence[str]]) -> str:
target = safe_return_target(query)
if target is None:
return ""
return (
''
)
def _return_target_suffix(query: Mapping[str, Sequence[str]]) -> str:
target = safe_return_target(query)
return "?" + urlencode({"openid.return_to": target}) if target else ""
def _error_notice(message: str | None) -> str:
if not message:
return ""
return f'
Enter the email address or mobile phone number associated with your Amazon account.
{_error_notice(error)}
Has your email or mobile number changed?
If you no longer use the email address associated with your Amazon account, you may contact Customer Service for help restoring access to your account.
"""
return _document("Amazon Password Assistance", content, body_class="auth-forgot auth-email-stage")
def _mail_delivery_status_markup(
purpose: str,
mail_delivery: Mapping[str, object] | None,
mail_delivery_mode: str,
local_inbox_url: str | None = None,
) -> str:
status = str((mail_delivery or {}).get("status") or "")
if status not in {
"QUEUED",
"LOCAL_ONLY",
"SMTP_PENDING",
"SMTP_SENT",
"SMTP_FAILED",
}:
status = "SMTP_PENDING" if mail_delivery_mode == "SMTP" else "LOCAL_ONLY"
descriptions = {
"QUEUED": (
"If an account matches that address, its verification message is "
"being handled. Refreshing does not reveal whether an account exists."
),
"LOCAL_ONLY": (
"This message is stored only in the protected local outbox; "
"no external email was sent."
),
"SMTP_PENDING": (
"The configured SMTP service is processing this message. "
"Refresh to check the latest delivery result."
),
"SMTP_SENT": "The configured SMTP service accepted this message.",
"SMTP_FAILED": (
"The SMTP attempt failed. No address or provider error is exposed here."
),
}
action_suffix = "?" + urlencode({"purpose": purpose})
actions: list[str] = []
if status in {"QUEUED", "SMTP_PENDING"}:
actions.append(
f'Refresh delivery status'
)
if status == "SMTP_FAILED" and bool((mail_delivery or {}).get("can_retry")):
actions.append(
''
)
if local_inbox_url and status in {"SMTP_PENDING", "SMTP_SENT", "SMTP_FAILED"}:
actions.append(
f''
"Open local SMTP inbox"
)
action_markup = (
f'
{"".join(actions)}
'
if actions
else ""
)
return (
f'
Email delivery
'
f'
{status}{escape(descriptions[status])}
'
f'{action_markup}'
)
def verification_page(
query: Mapping[str, Sequence[str]],
*,
masked_destination: str | None = None,
error: str | None = None,
mail_delivery_mode: str = "LOCAL_ONLY",
mail_delivery: Mapping[str, object] | None = None,
local_inbox_url: str | None = None,
) -> str:
purpose = _first(query, "purpose").lower()
if purpose == "registration" and masked_destination:
lead = (
f"A verification code was queued for {masked_destination}. "
"Enter it below when it arrives."
)
elif purpose == "registration":
lead = "Start account creation again to request a new email verification code."
elif purpose == "password-reset":
lead = (
"If an account matches that address, a verification code was queued. "
"Enter it below if it arrives."
)
else:
lead = "Enter the one-time password that was queued for you."
action_suffix = (
"?" + urlencode({"purpose": purpose})
if purpose in {"registration", "password-reset"}
else ""
)
delivery_status = _mail_delivery_status_markup(
purpose,
mail_delivery,
mail_delivery_mode,
local_inbox_url,
)
content = f"""