diff --git a/compositor/host/auth_test.mbt b/compositor/host/auth_test.mbt index d9a3bf7..50cd0ae 100644 --- a/compositor/host/auth_test.mbt +++ b/compositor/host/auth_test.mbt @@ -563,3 +563,83 @@ let real_keymap : String = #|}; #| #|}; + +///| `settle`, composing frames too (the Face unlock window and Settings follow their tool there). +fn fsettle(comp : @host.ChandraCompositor, a : MockAuthority, cond : () -> Bool) -> Bool { + for i = 0; i < 300; i = i + 1 { + a.bus.pump() + comp.step() + ignore(comp.compose_frame()) + if cond() { + return true + } + @sys.sleep_ms(10) + } + false +} + +///| +test "Face unlock window: snaps, Apply, then the password dialog gets the keyboard and the click" { + let bus = match auth_bus("faceapply") { + Some(b) => b + None => return + } + let a = MockAuthority::new(bus) + let hpath = "/tmp/chandra-auth-helper-faceapply-" + @sys.getpid().to_string() + let listen = @sys.unix_listen(hpath) + let comp = auth_comp("faceapply", bus, hpath) + comp.server.set_keymap(@utf8.encode(real_keymap)) + assert_true(fsettle(comp, a, fn() { comp.polkit.map(p => p.registered()).unwrap_or(false) })) + let dir = "/tmp/chandra-face-apply-" + @sys.getpid().to_string() + let tool = fake_face_tool(dir) + ignore(@sys.write_file_bytes(dir + "/wait", b"")) + comp.shell.set_face_tool(["/bin/sh", tool]) + assert_true(comp.shell.open_settings(Session)) + assert_true(fsettle(comp, a, fn() { face_texts(comp).contains("Desk") })) + face_click(comp, "Add or remove faces…", 10U) + assert_true(fsettle(comp, a, fn() { comp.shell.face.window.map(w => w.picture != 0U).unwrap_or(false) })) + let w = comp.shell.face.window.unwrap() + face_key(comp, 57U, 20U) + assert_true(fsettle(comp, a, fn() { w.snaps.length() == 1 })) + face_key(comp, 57U, 30U) + assert_true(fsettle(comp, a, fn() { w.snaps.length() == 2 })) + // Enter: Apply; the root step asks polkit, which asks us. Meanwhile the window steps aside: + // no dimming card, no input of its own (another agent's password window must be reachable). + face_key(comp, 28U, 40U) + assert_true(w.applying) + ignore(comp.compose_frame()) + comp.handle_input(@nested.InputEvent::Key(57U, true), 1U) + comp.handle_input(@nested.InputEvent::Key(57U, false), 1U) + assert_eq(w.snaps.length(), 2) + a.begin("f1", run0_details) + assert_true(fsettle(comp, a, fn() { comp.auth_dialog() is Some(_) })) + let d = comp.auth_dialog().unwrap() + let (h, _) = helper_accept(listen, comp, a) + helper_say(h, "PAM_PROMPT_ECHO_OFF Password: ") + assert_true(fsettle(comp, a, fn() { d.phase == Prompt })) + auth_shot(comp, "18-face-apply-password") + // Typing goes to the password dialog, not to the window under it (Space would snap). + auth_type(comp, "Secret9") + comp.handle_input(@nested.InputEvent::Key(57U, true), 1U) + comp.handle_input(@nested.InputEvent::Key(57U, false), 1U) + assert_eq(d.typed(), 8) + assert_eq(w.snaps.length(), 2) + // The Authenticate button takes the click. + ignore(comp.compose_frame()) + let allow = comp.auth.unwrap().hits.filter(x => x.1 == Allow)[0].0 + comp.handle_input(@nested.InputEvent::PointerMove((allow.x + 4).to_double(), (allow.y + 4).to_double()), 1U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 3U) + assert_true(fsettle(comp, a, fn() { d.phase == Checking })) + assert_eq(helper_lines(h, 1), "Secret9 ") + helper_say(h, "SUCCESS") + assert_true(fsettle(comp, a, fn() { comp.auth_dialog() is None && a.answers.length() == 1 })) + // The root step goes on; the window closes with what it said. + ignore(@sys.write_file_bytes(dir + "/go", b"")) + assert_true(fsettle(comp, a, fn() { comp.shell.face.window is None })) + assert_eq(comp.shell.face.message, "Added Face 2 and removed 1 face") + @sys.close_fd(h) + @sys.close_fd(listen) + @sys.unlink(hpath) + comp.close() +} diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 84dbe41..f01780d 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -1972,8 +1972,9 @@ pub fn ChandraCompositor::desired_cursor(self : ChandraCompositor) -> UInt { Some(ui) => return self.auth_cursor(ui) None => () } - if self.shell.face.window is Some(_) { - return if self.face_window_hit() is Some(_) { 4U } else { 1U } + match self.shell.face.window { + Some(w) if !w.applying => return if self.face_window_hit() is Some(_) { 4U } else { 1U } + _ => () } match self.shot { Some(o) => return self.overlay_cursor(o) diff --git a/compositor/host/face_test.mbt b/compositor/host/face_test.mbt index f3db917..97417f2 100644 --- a/compositor/host/face_test.mbt +++ b/compositor/host/face_test.mbt @@ -32,6 +32,12 @@ fn fake_face_tool(dir : String) -> String { #| drop) kept=$((kept-1)); pic ;; #| apply) #| echo "info Waiting for the password…" + #| # With "$S/wait": hold on until the test's password dialog has been answered. + #| if [ -e "$S/wait" ]; then t=0; while [ ! -e "$S/go" ] && [ $t -lt 200 ]; do sleep 0.05; t=$((t+1)); done; fi + #| # With "$S/refuse": the password was not given; the camera goes on. + #| if [ -e "$S/refuse" ]; then + #| echo "error Nothing changed: the administrator password wasn't given"; echo "applied 1"; pic; continue + #| fi #| for id in $(echo "$arg" | tr ',' ' '); do #| grep -v "^face $id " "$S/faces" > "$S/faces.new"; mv "$S/faces.new" "$S/faces" #| done @@ -146,6 +152,13 @@ test "the Face unlock window: live picture, snaps, a face marked for removal, on ignore(@sys.write_file_bytes(shots + "/17-face-window.png", @renderer.encode_png_compressed(comp.compose_frame()))) } assert_eq(comp.shell.face.faces.length(), 1) + // A refused password keeps the window, the snap and the mark, and says why. + ignore(@sys.write_file_bytes(dir + "/refuse", b"")) + face_key(comp, 28U, 35U) + assert_true(face_settle(comp, fn() { !w.applying && w.error })) + assert_eq(w.message, "Nothing changed: the administrator password wasn't given") + assert_eq((w.snaps.length(), w.remove), (1, ["1"])) + @sys.unlink(dir + "/refuse") // Enter applies: one run for the snap and the removal; the window closes, Settings shows it. face_key(comp, 28U, 40U) assert_true(face_settle(comp, fn() { comp.shell.face.window is None })) diff --git a/compositor/host/face_window.mbt b/compositor/host/face_window.mbt index 844cd0e..e4cf10c 100644 --- a/compositor/host/face_window.mbt +++ b/compositor/host/face_window.mbt @@ -58,8 +58,11 @@ fn ChandraCompositor::face_window_activate(self : ChandraCompositor, h : FaceHit ///| Input while the window is up: all of it goes to the window. fn ChandraCompositor::face_window_input(self : ChandraCompositor, ev : @nested.InputEvent) -> Bool { - if self.shell.face.window is None { - return false + match self.shell.face.window { + // While Apply waits for the password the window steps aside: the password dialog (ours, + // or another agent's window) must get the keyboard and the pointer. + Some(w) if !w.applying => () + _ => return false } match ev { Key(code, pressed) => { @@ -113,6 +116,19 @@ fn ChandraCompositor::face_window_ops(self : ChandraCompositor) -> Array[@scene. } } let p = self.theme.palette + if win.applying { + // Out of the way of the password dialog: a note at the top, no dimming, no input. + self.face_hits.clear() + let fs = self.theme.font_size + let text = "Face unlock: " + win.message + let tw = @renderer.text_width(text, fs - 1, false, false) + 32 + let r = @scene.Rect::new((self.output_w - tw) / 2, 12, tw, fs + 18) + let opaque = @scene.Rgba::new(p.surface_popup.r.to_int(), p.surface_popup.g.to_int(), p.surface_popup.b.to_int(), 255) + ops.push(@scene.DrawOp::FillRect(r, opaque, r.h / 2)) + ops.push(@scene.DrawOp::StrokeRect(r, p.border, 1, r.h / 2)) + ops.push(@scene.DrawOp::Text(r.x + tw / 2, r.y + r.h / 2, text, fs - 1, p.foreground, @scene.TextAlign::Center, false)) + return ops + } let (err_c, _) = auth_colors(p) let fs = self.theme.font_size let rad = self.theme.radius + 2 diff --git a/compositor/shell/face.mbt b/compositor/shell/face.mbt index d35998f..94b25b5 100644 --- a/compositor/shell/face.mbt +++ b/compositor/shell/face.mbt @@ -573,7 +573,15 @@ fn FaceUnlock::window_line(self : FaceUnlock, w : FaceWindow, kind : String, tex w.message = text w.error = true } - "applied" => if text != "0" && !w.error { w.message = "Nothing changed" } + "applied" => + if text != "0" { + // Refused or cancelled: the camera starts again, the snaps and marks are still there. + w.applying = false + if !w.error { + w.message = "Nothing changed: the administrator password wasn't given" + w.error = true + } + } _ => () } self.touch() diff --git a/docs/face-unlock.md b/docs/face-unlock.md index fe4dcdd..e9c1606 100644 --- a/docs/face-unlock.md +++ b/docs/face-unlock.md @@ -46,7 +46,10 @@ the `face-unlock` capability.) Then in **Settings > Session > Face unlock**: says why ("No face in view", "More than one face in view", "Come closer to the camera"). - **Remove** / **Keep** on a saved face marks it for removal, or not. - **Apply** (or Enter) saves the snaps as one new face and removes the marked ones, in one step: - the password dialog asks once, above the window. **Cancel** (Esc) changes nothing. + the password dialog asks once. While it asks, the window steps aside (a note at the top says + it is waiting) so the dialog, Chandra's or another polkit agent's window, gets the keyboard and + the pointer. If the password isn't given, the window comes back with the snaps and marks as + they were and says so. **Cancel** (Esc) changes nothing. Why root at all: the face data is root's (`/var/lib/chandra-face`, 0600) and PAM trusts it to let you into sudo and polkit without a password. If you could write it as yourself, so could any diff --git a/face/chandra-face b/face/chandra-face index dc6325f..c55a63c 100755 --- a/face/chandra-face +++ b/face/chandra-face @@ -669,6 +669,7 @@ def cmd_camera(cfg, user, label, out, cfg_path, no_elevate): about 12 a second; the box is -1s without one face), `snap ID W H LEN` + RGB (a kept snap, cropped to the face), `info` / `error` / `done` lines, `applied CODE` at the end of an apply. In (stdin lines): `snap`, `drop ID`, `apply REMOVE-IDS` (comma-separated, may be empty), `quit`. + After a failed apply (no password, say) the camera starts again and the snaps are kept. The pictures stay in this process's memory: nothing is written to disk; only the embeddings of the snaps kept go to the root `apply`, through a pipe.""" check_user(user) @@ -733,7 +734,10 @@ def cmd_camera(cfg, user, label, out, cfg_path, no_elevate): "remove": removes} code = run_apply(cfg, user, out, req, cfg_path, no_elevate) out.emit("applied", str(code)) - return code + if code == 0: + return 0 + # The password was refused or cancelled: back to the camera, snaps kept. + cam = open_camera(cfg, loop=True) finally: if cam is not None: cam.close() @@ -793,7 +797,7 @@ def run_apply(cfg, user, out, req, cfg_path, no_elevate): sys.stdout.flush() code = p.wait() if not said: - out.error("Nothing changed") + out.error("Nothing changed: the administrator password wasn't given") return code or 1 return code diff --git a/face/test_face.py b/face/test_face.py index 43057a8..e77dd53 100644 --- a/face/test_face.py +++ b/face/test_face.py @@ -337,6 +337,23 @@ class CameraWindowTests(unittest.TestCase): s.finish() self.assertEqual([f["label"] for f in self.vectors()], ["Face 2"]) + def test_a_failed_apply_keeps_the_snaps(self): + self.env.camera(["alice"], frame_delay="0.01") + s = Session(self.env) + s.send("snap") + s.next("snap") + s.send("apply 99") + words, _ = s.next("error") + self.assertEqual(" ".join(words[1:]), "no face 99 for %s" % ME) + self.assertEqual(s.next("applied")[0][1], "1") + s.next("frame") # the camera goes on + s.send("snap") + s.next("snap") + s.send("apply ") + self.assertEqual(s.next("applied")[0][1], "0") + self.assertEqual(s.finish(), 0) + self.assertEqual(len(self.vectors()[0]["vectors"]), 2) + def test_apply_refuses_bad_data(self): for bad in (b"[]", b"{\"add\": [{\"vectors\": [[1, 2]]}]}", b"{\"add\": [{\"vectors\": [[1e999, 0, 0, 0]]}]}", b"{\"remove\": [7]}", b"nonsense"):