package main // Exhibit 11. A channel declared with `auth` (`channels = mine auth`) makes nginx ask the app before // anyone may listen: the host runs the app with the channel's name and id (rc.serve's channel_auth, // main.odin), and the app says yes or no. The names are guessable on purpose ("u" + your user id): // the gate, not the name, keeps them private. import "core:encoding/json" import "core:fmt" import "core:strings" import "core:time" import "ratcloud:rc" e11 :: proc() -> Exhibit { return { n = 11, wing = "Realtime", title = "Private streams stay private", promise = "A live stream meant for one person can't be read by someone who guesses its name.", block = "Add `auth` to a channel in ratcloud.conf, and the app decides who may listen: nginx asks it first (rc.serve's channel_auth gets the channel and its id).", src = {"e11_private_streams.odin", #load("e11_private_streams.odin", string)}, files = srcs(SELF_SRC, {"robot.odin", #load("robot.odin", string)}), script = {"web/e11.js", #load("web/e11.js", string)}, channel = proc(name, id: string, me: ^rc.User) -> (bool, bool) { if name != "mine" do return false, false return me != nil && id == fmt.tprintf("u%d", me.id), true }, show = proc(me: ^rc.User) -> string { own := signin_line("to get a stream of your own.") if me != nil do own = fmt.tprintf(`
Your stream is mine/u%d.
`, guess, guess)) }, api = proc(a: ^Api) -> bool { if a.do_ != "ping" || !a.post do return false if a.me == nil {reply_err(a, 401, "sign in to have a stream"); return true} ok := publish("mine", fmt.tprintf("u%d", a.me.id), fmt.tprintf(`{{"text":%s,"at":%s}}`, jq(cat("ping for @", a.me.username)), jq(iso(now())))) reply_raw(a, fmt.tprintf(`{{"published":%v}}`, ok)) return true }, // The robot hears its own stream; nobody else, signed in or not, may listen to it. check = proc() -> Check { b, err := robot_at() if err != "" do return fail(err) id := fmt.tprintf("u%d", robot_user().id) l := listen_open(b, {self_url(cat("/rc/sub/mine/", id))}, 2000) defer listen_close(&l) if l.status[0] != 200 do return fail(fmt.tprintf("the robot was refused its own stream (%d)", l.status[0])) t0 := time.tick_now() post(b, self_url("/api/11/ping")) want :: proc(v: json.Value, arg: rawptr) -> bool {return strings.has_prefix(jstr(v, "text"), "ping")} seen, ms := listen_wait(&l, t0, want, nil) if seen == 0 do return fail("the robot's own stream opened but its ping never came") Try :: struct { who: string, b: ^Browser, url: string, } tries := []Try { {"a visitor, on the robot's stream", new_browser(), self_url(cat("/rc/sub/mine/", id))}, {"the robot, on someone else's", b, self_url(fmt.tprintf("/rc/sub/mine/u%d", robot_user().id + 1))}, {"the robot, on a stream with no name", b, self_url("/rc/sub/mine")}, } for t in tries { x := listen_open(t.b, {t.url}, 0) listen_close(&x) if x.status[0] != 403 do return fail(fmt.tprintf("%s: answered %d, not refused", t.who, x.status[0])) } return pass(fmt.tprintf("the robot heard its own ping (%dms); a visitor and the robot on someone else's stream were refused (403)", ms)) }, } }