package main // Exhibit 3. rc.sign_out(back) forgets this app's token and sends the browser to account's "sign out // everywhere". Every other app drops its token on its next request, because the hint cookie account // set on the parent domain is gone. // // After signing out you are a visitor, so the zoo cannot ask rc.me() who you were. It keeps that in a // short-lived signed cookie, only to let you confirm "every card said visitor". import "core:crypto/hmac" import "core:fmt" import "core:strings" import "ratcloud:rc" signout_key :: proc() -> string { k, ok := meta("signout_key") if !ok {k = random_hex(16); meta_set("signout_key", k)} return k } signout_sig :: proc(msg: string) -> string { out: [32]u8 hmac.sum(.SHA256, out[:], transmute([]u8)msg, transmute([]u8)signout_key()) return hex(out[:]) } // The cookie's person, if it is ours and under an hour old. signout_who :: proc() -> (uid: i64, ok: bool) { c, has := rc.cookie(g_req, "__Host-zoo_out") if !has do return 0, false p := strings.split(c, ".") if len(p) != 3 do return 0, false if !const_eq(signout_sig(cat(p[0], ".", p[1])), p[2]) || now() - atoi(p[1]) >= 3600 do return 0, false return atoi(p[0]), true } const_eq :: proc(a, b: string) -> bool { if len(a) != len(b) do return false x: u8 = 0 for i in 0 ..< len(a) do x |= a[i] ~ b[i] return x == 0 } e03 :: proc() -> Exhibit { return { n = 3, wing = "Identity", kind = "human", title = "Sign out everywhere", promise = "Signing out at account signs you out of every app in this browser.", 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.", src = {"e03_sign_out.odin", #load("e03_sign_out.odin", string)}, script = {"web/e03.js", #load("web/e03.js", string)}, show = proc(me: ^rc.User) -> string { second := h(rc.app_url("zoo-broken")) if me != nil do return cat(`

Press this, confirm at account, and come back: every "who am I" card (here and on the second app) should say visitor.

`) if uid, ok := signout_who(); ok { name := "?" if u, found := rc.users({uid})[uid]; found do name = u.username return cat(`

You signed out as @`, h(name), `. Does every card say visitor, here and on the second app?

`) } return `

Sign in first; then this button signs you out of every app at once.

` }, api = proc(a: ^Api) -> bool { switch a.do_ { case "signout": if a.me != nil { t := itoa(now()) v := fmt.tprintf("%d.%s", a.me.id, t) rc.set_cookie(a.w, "zoo_out", cat(v, ".", signout_sig(v)), {host_prefix = true, http_only = true, same_site = "Lax", max_age = 3600}) } rc.sign_out(self_url("/#e3")) case "confirm": if !a.post do return false if a.me != nil {reply_err(a, 409, "you are signed in again: this card says your name"); return true} uid, ok := signout_who() if !ok {reply_err(a, 400, "no recent sign-out from this browser"); return true} seen(3, uid) rc.set_cookie(a.w, "zoo_out", "", {host_prefix = true, http_only = true, same_site = "Lax", max_age = -1}) reply_raw(a, `{"ok":true}`) return true } return false }, } }