Zoo

staging

← back to the zoo

3. Sign out everywhere

Signing out at account signs you out of every app in this browser.

rc.sign_out(back) sends the browser to account, which ends the one session behind every app's token. No app has to be told.

This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit 55c71513c5 (staging). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.

e03_sign_out.odin sha256 7ef4dd1c0930 · raw

1package main
2
3// Exhibit 3. rc.sign_out(back) forgets this app's token and sends the browser to account's "sign out
4// everywhere". Every other app drops its token on its next request, because the hint cookie account
5// set on the parent domain is gone.
6//
7// After signing out you are a visitor, so the zoo cannot ask rc.me() who you were. It keeps that in a
8// short-lived signed cookie, only to let you confirm "every card said visitor".
9
10import "core:crypto/hmac"
11import "core:fmt"
12import "core:strings"
13import "ratcloud:rc"
14
15signout_key :: proc() -> string {
16	k, ok := meta("signout_key")
17	if !ok {k = random_hex(16); meta_set("signout_key", k)}
18	return k
19}
20
21signout_sig :: proc(msg: string) -> string {
22	out: [32]u8
23	hmac.sum(.SHA256, out[:], transmute([]u8)msg, transmute([]u8)signout_key())
24	return hex(out[:])
25}
26
27// The cookie's person, if it is ours and under an hour old.
28signout_who :: proc() -> (uid: i64, ok: bool) {
29	c, has := rc.cookie(g_req, "__Host-zoo_out")
30	if !has do return 0, false
31	p := strings.split(c, ".")
32	if len(p) != 3 do return 0, false
33	if !const_eq(signout_sig(cat(p[0], ".", p[1])), p[2]) || now() - atoi(p[1]) >= 3600 do return 0, false
34	return atoi(p[0]), true
35}
36
37const_eq :: proc(a, b: string) -> bool {
38	if len(a) != len(b) do return false
39	x: u8 = 0
40	for i in 0 ..< len(a) do x |= a[i] ~ b[i]
41	return x == 0
42}
43
44e03 :: proc() -> Exhibit {
45	return {
46		n = 3, wing = "Identity", kind = "human",
47		title = "Sign out everywhere",
48		promise = "Signing out at account signs you out of every app in this browser.",
49		block = "rc.sign_out(back) sends the browser to account, which ends the one session behind every app's token. No app has to be told.",
50		src = {"e03_sign_out.odin", #load("e03_sign_out.odin", string)},
51		script = {"web/e03.js", #load("web/e03.js", string)},
52		show = proc(me: ^rc.User) -> string {
53			second := h(rc.app_url("zoo-broken"))
54			if me != nil do return cat(`<p>Press this, confirm at account, and come back: every "who am I" card (here and on <a href="`, second, `/" target="_blank" rel="noopener">the second app</a>) should say visitor.</p><p><a href="/api/3/signout"><button class="primary">Sign out everywhere</button></a></p>`)
55			if uid, ok := signout_who(); ok {
56				name := "?"
57				if u, found := rc.users({uid})[uid]; found do name = u.username
58				return cat(`<p>You signed out as <b>@`, h(name), `</b>. Does every card say visitor, here and on <a href="`, second, `/" target="_blank" rel="noopener">the second app</a>?</p><p><button class="seen-out primary">Yes, every card says visitor</button></p>`)
59			}
60			return `<p>Sign in first; then this button signs you out of every app at once.</p>`
61		},
62		api = proc(a: ^Api) -> bool {
63			switch a.do_ {
64			case "signout":
65				if a.me != nil {
66					t := itoa(now())
67					v := fmt.tprintf("%d.%s", a.me.id, t)
68					rc.set_cookie(a.w, "zoo_out", cat(v, ".", signout_sig(v)), {host_prefix = true, http_only = true, same_site = "Lax", max_age = 3600})
69				}
70				rc.sign_out(self_url("/#e3"))
71			case "confirm":
72				if !a.post do return false
73				if a.me != nil {reply_err(a, 409, "you are signed in again: this card says your name"); return true}
74				uid, ok := signout_who()
75				if !ok {reply_err(a, 400, "no recent sign-out from this browser"); return true}
76				seen(3, uid)
77				rc.set_cookie(a.w, "zoo_out", "", {host_prefix = true, http_only = true, same_site = "Lax", max_age = -1})
78				reply_raw(a, `{"ok":true}`)
79				return true
80			}
81			return false
82		},
83	}
84}

web/e03.js sha256 a6b33dda6261 · raw

1document.querySelector(".seen-out")?.addEventListener("click", async (ev) => {
2  ev.target.disabled = true;
3  const j = await zoo.call("/api/3/confirm", {});
4  ev.target.textContent = j.error || "Recorded, thank you";
5  if (!j.error) setTimeout(() => location.reload(), 600);
6});