#!/usr/bin/env python3 """An invented site that signs in by an emailed link or the code that link shows (assets/design/chat/SITES.md), for walking Sites on a simulator. Serves on localhost:8765. Nothing real: the person, the club and the codes are invented, made fresh at each start, and only ever shown in the page. / signed in: "Signed in as "; else an email form /send "We sent a link", and a box for the code the link shows elsewhere /in?t=... the link, once: signs this browser in (a cookie) and goes home /code?t=... the link opened on another device: shows a code to type there /verify the code typed: signs this browser in /login a user name and password form (any password signs in; none is kept or shown), for filling a login from Secrets """ import html, http.server, secrets, sys, urllib.parse PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8765 TOKENS = {"walk-link": "kavi@invented.example"} # the dev fixture's mail carries this one CODES = {} # code -> email SESSIONS = {} # cookie -> email TRACKERS = """Tracker test

Tracker test

waiting

""" PAGE = """ Invented Club

Invented Club

{}""" class Site(http.server.BaseHTTPRequestHandler): def who(self): for part in (self.headers.get("Cookie") or "").split(";"): k, _, v = part.strip().partition("=") if k == "club" and v in SESSIONS: return SESSIONS[v] def raw(self, body, kind): data = body.encode() self.send_response(200) self.send_header("Content-Type", kind) self.send_header("Content-Length", str(len(data))) self.end_headers() self.wfile.write(data) def send(self, body, status=200, headers=()): data = PAGE.format(body).encode() self.send_response(status) self.send_header("Content-Type", "text/html; charset=utf-8") for k, v in headers: self.send_header(k, v) self.send_header("Content-Length", str(len(data))) self.end_headers() self.wfile.write(data) def sign_in(self, email): cookie = secrets.token_hex(16) SESSIONS[cookie] = email self.send_response(302) self.send_header("Set-Cookie", f"club={cookie}; Path=/; Max-Age=31536000; HttpOnly") self.send_header("Location", "/") self.end_headers() def do_GET(self): url = urllib.parse.urlparse(self.path) q = dict(urllib.parse.parse_qsl(url.query)) # Trackers measured (IPHONE READY 1): three real tracker scripts on hosts in Sites' list, a script from this # page's own host and one from 127.0.0.1 (another host, not on the list). Each says on the page whether it # loaded; a blocked one errors at once without leaving the phone. if url.path == "/trackers": return self.raw(TRACKERS, "text/html; charset=utf-8") if url.path == "/pixel.js": return self.raw("window.cottonPixel = (window.cottonPixel || 0) + 1;", "text/javascript") if url.path == "/": email = self.who() if email: return self.send(f'

Signed in as {html.escape(email)}

Sign out

') return self.send('

Not signed in.

' '
') # A link works once, as a real sign-in link does: opened anywhere first, it is spent. if url.path == "/in" and q.get("t") in TOKENS: return self.sign_in(TOKENS.pop(q["t"])) if url.path == "/code" and q.get("t") in TOKENS: code = f"{secrets.randbelow(10**6):06d}" CODES[code] = TOKENS.pop(q["t"]) return self.send(f"

Type this code where you asked to sign in:

{code}

") if url.path == "/login": return self.send('

Sign in

' '

' '

' '
') if url.path == "/out": self.send_response(302) self.send_header("Set-Cookie", "club=; Path=/; Max-Age=0") self.send_header("Location", "/") return self.end_headers() self.send("

That link has expired.

", 404) def do_POST(self): body = self.rfile.read(int(self.headers.get("Content-Length") or 0)).decode() form = dict(urllib.parse.parse_qsl(body)) if self.path == "/send": token = secrets.token_hex(8) TOKENS[token] = form.get("email", "") print(f"invented-site: a link was made for /in?t={token} and /code?t={token}", flush=True) return self.send("

We sent you a link. If it shows a code instead, type it here.

" "
" "
") if self.path == "/login" and form.get("email") and form.get("password"): return self.sign_in(form["email"]) if self.path == "/verify" and form.get("code") in CODES: return self.sign_in(CODES.pop(form["code"])) self.send("

That code is not right.

", 400) def log_message(self, *a): pass if __name__ == "__main__": print(f"invented-site: http://localhost:{PORT}/", flush=True) http.server.ThreadingHTTPServer(("127.0.0.1", PORT), Site).serve_forever()