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'' def _document( title: str, content: str, *, body_class: str, scripts: Sequence[str] = (), ) -> str: script_markup = "".join( f'' for source in scripts ) return f""" {escape(title)} {script_markup}
{_logo()}
{content}
""" def signin_page(query: Mapping[str, Sequence[str]], error: str | None = None) -> str: if _first(query, "stage").lower() == "password": content = f"""

Sign in

Amazon account

{_error_notice(error)}
{_hidden_return_target(query)} Forgot password?
Details

Use this option only on your personal device.

""" return _document("Amazon Sign-In", content, body_class="auth-signin auth-password-stage") content = f"""

Sign in or create account

{_error_notice(error)}
{_hidden_return_target(query)}
Need help?Forgot your password?Create your Amazon account
Buying for work? Create a free business account
""" return _document("Amazon Sign-In", content, body_class="auth-signin auth-identifier-stage") def register_page( query: Mapping[str, Sequence[str]], error: str | None = None, *, external_verification: bool = False, ) -> str: suggested_email = _first(query, "email").strip() verification_controls = "" form_attributes = "" submit_attributes = "" scripts: tuple[str, ...] = () if external_verification: form_attributes = ' data-external-registration="true"' submit_attributes = ' disabled data-auth-create-account' scripts = ("/static/auth-verification.js",) verification_controls = """

""" content = f"""

Create account

{_error_notice(error)}
{_hidden_return_target(query)} {verification_controls}

Passwords must be at least 6 characters.


Already have an account? Sign in

""" return _document( "Amazon Registration", content, body_class="auth-register", scripts=scripts, ) def forgot_password_page( query: Mapping[str, Sequence[str]], error: str | None = None ) -> str: if _first(query, "stage").lower() == "reset-password": content = f"""

Create new password

We'll ask for this password whenever you sign in.

{_error_notice(error)}
""" return _document("Create New Password", content, body_class="auth-forgot auth-reset-stage") content = f"""

Password assistance

Enter the email address or mobile phone number associated with your Amazon account.

{_error_notice(error)}
{_hidden_return_target(query)}

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"""

Verify email address

{escape(lead)}

{_error_notice(error)}
{delivery_status}
""" return _document("Amazon Verification", content, body_class="auth-verification") def page_for(path: str, query: Mapping[str, Sequence[str]]) -> str: if path == "/ap/signin": return signin_page(query) if path == "/ap/register": return register_page(query) if path == "/ap/forgotpassword": return forgot_password_page(query) if path == "/ap/cvf/verify": return verification_page(query) raise ValueError(f"unsupported auth path: {path}")