Download tasks/lowes/solution/site/implementation/clone/backend/accounts.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/lowes/solution/site/implementation/clone/backend/accounts.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/lowes/solution/site/implementation/clone/backend/accounts.py
-
curl -L -o accounts.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/lowes/solution/site/implementation/clone/backend/accounts.py
10.5 kB
| """Lowe's offline clone — synthetic account flow over the vendored auth store. | |
| The observed source surface is the right-hand "Sign In or Create Account" | |
| drawer: one *Email or Phone Number* field, a remembered-device checkbox, a | |
| Continue button, legal links and the Order Status / Projects & Installations / | |
| Lists / Military Discount / First Responder Benefits list. Nothing past the | |
| Continue button was observed (no account was created and no code was sent on | |
| the source), so every later step here is a **clone-local synthetic flow** and is | |
| recorded as such in ``scope/claims.jsonl``. | |
| What this module is *not*: it is not a second auth implementation. Password | |
| hashing, challenge issuing, attempt budgets, lockout, session rotation and the | |
| local mail outbox all belong to ``websitebench.local_clone_auth.LocalAuthStore`` | |
| opened through ``backend/site_backend_integration.py``. This module only | |
| * normalizes the single observed identifier field into a store-compatible | |
| identity (email, or a reserved 555-01xx test phone number mapped to a | |
| non-routable local mailbox), | |
| * sequences the drawer steps, and | |
| * hands the store a cart-migration callback so a session rotation does not lose | |
| an anonymous cart. | |
| The same three points apply to password recovery: the store owns the reset | |
| challenge, its attempt budget, the indistinguishable public response for known | |
| and unknown emails, the password replacement and the session revocation. This | |
| module only normalizes the identifier and orders the steps. | |
| Verification codes are never written to the database, a log, a response header | |
| or an order record. The store keeps the issued code in process memory only; the | |
| development-only helper below reads it back for the *current session* so a | |
| local operator or a benchmark agent can finish the flow offline. | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import re | |
| import sqlite3 | |
| from typing import Any, Mapping | |
| from websitebench.local_clone_auth import ( | |
| AuthConflict, | |
| AuthError, | |
| AuthExpired, | |
| AuthLocked, | |
| AuthRateLimited, | |
| AuthRejected, | |
| AuthValidationError, | |
| LocalAuthStore, | |
| ) | |
| from backend import store as site_store | |
| # Reserved, non-routable mailbox domain for identifiers entered as a phone | |
| # number. `.invalid` is reserved by RFC 2606 and can never resolve. | |
| PHONE_MAILBOX_DOMAIN = "phone.lowes.invalid" | |
| _EMAIL_RE = re.compile(r"^[^@\s]{1,64}@[^@\s]{1,180}\.[A-Za-z]{2,}$") | |
| _DIGITS_RE = re.compile(r"\D+") | |
| # North American fictional-number range (NPA-555-0100 .. NPA-555-0199). Only | |
| # these are accepted so the clone can never be pointed at a real subscriber. | |
| _TEST_PHONE_RE = re.compile(r"^(?P<npa>[2-9]\d{2})5550(?P<line>1\d{2})$") | |
| STEP_IDENTIFY = "identify" | |
| STEP_SIGN_IN = "sign-in" | |
| STEP_CREATE = "create" | |
| STEP_VERIFY = "verify" | |
| PURPOSE_REGISTRATION = "registration" | |
| PURPOSE_PASSWORD_RESET = "password-reset" | |
| SANDBOX_HELP_ENABLED = os.environ.get("LOWES_LOCAL_SANDBOX_HELP", "1") != "0" | |
| class IdentifierError(ValueError): | |
| """The single observed identifier field failed server-side validation.""" | |
| def sandbox_help_enabled() -> bool: | |
| return SANDBOX_HELP_ENABLED | |
| def normalize_identifier(raw: str) -> dict[str, str]: | |
| """Map the observed single field onto one local mailbox identity. | |
| Returns ``{"kind": "email"|"phone", "email": ..., "display": ...}``. | |
| """ | |
| value = " ".join(str(raw or "").split()) | |
| if not value: | |
| raise IdentifierError("Enter an email or phone number.") | |
| if "@" in value: | |
| email = value.casefold() | |
| if not _EMAIL_RE.match(email) or len(email) > 254: | |
| raise IdentifierError("Enter a valid email address.") | |
| return {"kind": "email", "email": email, "display": email} | |
| digits = _DIGITS_RE.sub("", value) | |
| if digits.startswith("1") and len(digits) == 11: | |
| digits = digits[1:] | |
| if len(digits) != 10: | |
| raise IdentifierError("Enter a valid email address or 10-digit phone number.") | |
| match = _TEST_PHONE_RE.match(digits) | |
| if match is None: | |
| raise IdentifierError( | |
| "This offline clone only accepts reserved test phone numbers in the " | |
| "555-0100 to 555-0199 range, for example (213) 555-0142." | |
| ) | |
| display = f"({digits[0:3]}) {digits[3:6]}-{digits[6:10]}" | |
| return { | |
| "kind": "phone", | |
| "email": f"p{digits}@{PHONE_MAILBOX_DOMAIN}", | |
| "display": display, | |
| } | |
| def _rotation_callback(connection: sqlite3.Connection, old: str, new: str) -> None: | |
| site_store.migrate_owner(connection, old, new) | |
| def identify(auth: LocalAuthStore, identifier: str) -> dict[str, Any]: | |
| """Decide which drawer step follows Continue. | |
| The observed drawer copy states that Lowe's "will check if you have an | |
| account", so the clone reproduces that branch instead of the | |
| enumeration-resistant response used by password recovery. | |
| """ | |
| identity = normalize_identifier(identifier) | |
| exists = auth.account_exists(identity["email"]) | |
| return { | |
| "identity": identity, | |
| "step": STEP_SIGN_IN if exists else STEP_CREATE, | |
| "account_exists": exists, | |
| } | |
| def start_registration( | |
| auth: LocalAuthStore, | |
| session_token: str, | |
| *, | |
| identifier: str, | |
| first_name: str, | |
| last_name: str, | |
| password: str, | |
| ) -> dict[str, Any]: | |
| identity = normalize_identifier(identifier) | |
| display_name = " ".join(f"{first_name} {last_name}".split()) | |
| result = auth.start_registration( | |
| session_token, | |
| email=identity["email"], | |
| display_name=display_name, | |
| password=password, | |
| restart_invalid_flow=True, | |
| ) | |
| return {"identity": identity, "flow": result} | |
| def verify_and_complete( | |
| auth: LocalAuthStore, session_token: str, code: str | |
| ) -> dict[str, Any]: | |
| auth.verify_registration_code(session_token, code) | |
| return auth.complete_registration( | |
| session_token, session_rotation_callback=_rotation_callback | |
| ) | |
| def sign_in( | |
| auth: LocalAuthStore, session_token: str, *, identifier: str, password: str | |
| ) -> dict[str, Any]: | |
| identity = normalize_identifier(identifier) | |
| return auth.sign_in( | |
| session_token, | |
| email=identity["email"], | |
| password=password, | |
| session_rotation_callback=_rotation_callback, | |
| ) | |
| def sign_out(auth: LocalAuthStore, session_token: str | None) -> None: | |
| auth.sign_out(session_token) | |
| def start_password_reset( | |
| auth: LocalAuthStore, session_token: str, *, identifier: str | |
| ) -> dict[str, Any]: | |
| """Open a reset challenge without revealing whether the account exists. | |
| The store returns the same public payload for a known and an unknown | |
| identity, and only issues an outbox message when an account matched. This | |
| wrapper adds nothing to that response, so the clone's rendered page stays | |
| indistinguishable for both cases. | |
| """ | |
| identity = normalize_identifier(identifier) | |
| flow = auth.start_password_reset( | |
| session_token, | |
| email=identity["email"], | |
| restart_invalid_flow=True, | |
| ) | |
| return {"identity": identity, "flow": flow} | |
| def verify_password_reset( | |
| auth: LocalAuthStore, session_token: str, code: str | |
| ) -> None: | |
| auth.verify_password_reset_code(session_token, code) | |
| def complete_password_reset( | |
| auth: LocalAuthStore, session_token: str, *, new_password: str | |
| ) -> str: | |
| """Replace the password and return the rotated session token. | |
| The store revokes every prior session for the account inside the same | |
| transaction, so the returned token is the only live session afterwards. | |
| """ | |
| return auth.complete_password_reset( | |
| session_token, | |
| new_password=new_password, | |
| session_rotation_callback=_rotation_callback, | |
| ) | |
| def flow_status( | |
| auth: LocalAuthStore, | |
| session_token: str, | |
| *, | |
| purpose: str = PURPOSE_REGISTRATION, | |
| ) -> dict[str, Any]: | |
| return auth.session_flow_status(session_token, purpose=purpose) | |
| def sandbox_code( | |
| auth: LocalAuthStore, | |
| session_token: str, | |
| *, | |
| purpose: str = PURPOSE_REGISTRATION, | |
| ) -> str | None: | |
| """Development-only: read back the in-memory code for *this* session. | |
| Enabled by default for local runs and disabled by exporting | |
| ``LOWES_LOCAL_SANDBOX_HELP=0``. The value is never persisted: the auth | |
| store holds it in process memory for the lifetime of the challenge, and it | |
| disappears on restart. This is clone scaffolding for offline operation, not | |
| a reproduction of any Lowe's behavior. | |
| For ``password-reset`` the store only remembers a code when an account | |
| actually matched, so ``None`` here is also the honest answer for an unknown | |
| identity and must never be rendered as "no account exists". | |
| """ | |
| if not SANDBOX_HELP_ENABLED: | |
| return None | |
| message = auth.local_mail_for_session(session_token, purpose=purpose) | |
| if not message: | |
| return None | |
| return str(message["verification_code"]) | |
| def friendly_error(exc: Exception) -> tuple[str, str]: | |
| """Map a store exception onto (field, message) for the drawer.""" | |
| if isinstance(exc, IdentifierError): | |
| return "identifier", str(exc) | |
| if isinstance(exc, AuthRateLimited): | |
| return ( | |
| "code", | |
| "Too many verification requests. Wait a moment and try again.", | |
| ) | |
| if isinstance(exc, AuthLocked): | |
| return ( | |
| "code", | |
| "Too many incorrect codes. Start the verification again.", | |
| ) | |
| if isinstance(exc, AuthExpired): | |
| return "code", "That code expired. Request a new one." | |
| if isinstance(exc, AuthConflict): | |
| return "identifier", "That account already exists. Sign in instead." | |
| if isinstance(exc, AuthValidationError): | |
| return "password", str(exc) | |
| if isinstance(exc, AuthRejected): | |
| return "code", "That code is not correct." | |
| if isinstance(exc, AuthError): | |
| return "identifier", "That request could not be completed." | |
| raise exc | |
| def public_account(session: Mapping[str, Any] | None) -> dict[str, Any] | None: | |
| if not session or not session.get("authenticated"): | |
| return None | |
| account = dict(session["account"]) | |
| email = str(account.get("email_normalized", "")) | |
| account["is_phone_identity"] = email.endswith(f"@{PHONE_MAILBOX_DOMAIN}") | |
| if account["is_phone_identity"]: | |
| digits = email.split("@", 1)[0][1:] | |
| account["contact_display"] = f"({digits[0:3]}) {digits[3:6]}-{digits[6:10]}" | |
| else: | |
| account["contact_display"] = email | |
| return account | |