Face unlock: a Howdy-style PAM module, shown in the password dialog and set up in Settings
CI / test (push) Successful in 2m6s
CI / test (push) Successful in 2m6s
- face/pam_chandra_face.so (`-auth sufficient`): runs `chandra-face compare` as root with a clean
environment, forwards what it says as PAM_TEXT_INFO / PAM_ERROR_MSG, and maps its exit status
to success / auth error / unavailable, so the password always still works. Skips remote
sessions (PAM_RHOST, SSH_CONNECTION), a closed lid and users without faces; refuses a helper
that isn't root's; kills one that hangs (timeout + 6 s).
- face/chandra-face (Python + OpenCV): YuNet finds the face, SFace embeds it, cosine similarity
against the enrolled embeddings (threshold 0.42, two matching frames, dark IR warm-up frames
skipped, optional require_ir). enroll / list / remove / clear / test / status / pam
enable|disable / fetch-models (sha256-pinned opencv_zoo models). Embeddings only, in
/var/lib/chandra-face/USER/faces.json (root, 0600); labels in faces.list (0644). Store, config
and models must be root's and not writable by others. Root commands re-run through run0 (or
pkexec), so polkit asks first. Settings in /etc/chandra/face.conf.
- `just install-face [--pam]`, `just uninstall-face [--purge]` (face/install.py; uninstall takes
the module out of every PAM service first). `pam enable` inserts before the first auth or
@include line (Debian's sudo has only @include common-auth), keeping a .bak.
- Password dialog: a face row ("Looking for your face…" with a sweeping ring, "Face
recognized", or why not, "Type your password instead"); typing and Enter during the scan go in
when PAM asks for the password. Howdy's pam_howdy.so messages are understood too.
- Settings > Session > Face unlock: "Use my face for passwords" (polkit-1 + sudo), the enrolled
faces (Remove takes a second click), "Add a face…", and the tool's progress. New `face-unlock`
capability: tables 24 (status) and 25 (faces), request 44, in the wasmer/wasmtime stub, the Rust
host, the MoonBit wasmhost and chandra:host/plasmoids. compositor/shell/face.mbt runs the tool
on a socket so the compositor never waits.
- Tests: face/test_face.py (fake camera of PGM frames + fake recognizer; --pam builds the module
and runs it through pamtester), dialog states and message parsing (polkit_test), the dialog via
a mock helper (auth_test, shots 14/15), Settings against a fake chandra-face (face_test).
scripts/test.sh runs them (compositor/polkit was missing from its native list), CI installs
libpam0g-dev + pamtester. Also regenerated two stale desktop-module bindings (doc comments).
- docs/face-unlock.md, including the limits of 2D face recognition.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
This commit is contained in:
47 files changed
+2781
-29
No files matched your search
@@ -335,7 +335,7 @@ fn ChandraCompositor::auth_cursor(self : ChandraCompositor, ui : AuthUi) -> UInt
|
||||
///| Whether the dialog animates (a failed attempt shakes it; a spinner while PAM checks).
|
||||
fn ChandraCompositor::auth_animating(self : ChandraCompositor) -> Bool {
|
||||
match self.auth {
|
||||
Some(ui) => ui.dialog.shaking(@sys.monotonic_ms()) || ui.dialog.phase == Checking
|
||||
Some(ui) => ui.dialog.shaking(@sys.monotonic_ms()) || ui.dialog.phase == Checking || ui.dialog.face == Scanning
|
||||
None => false
|
||||
}
|
||||
}
|
||||
@@ -445,6 +445,24 @@ fn auth_button(
|
||||
}
|
||||
}
|
||||
|
||||
///| A face outline (head, eyes, smile) centred at (cx, cy), `r` the head's radius.
|
||||
fn face_glyph(cx : Int, cy : Int, r : Int, c : @scene.Rgba, ops : Array[@scene.DrawOp]) -> Unit {
|
||||
ops.push(@scene.DrawOp::Circle(cx, cy, r, c, false))
|
||||
ops.push(@scene.DrawOp::Circle(cx - r * 2 / 5, cy - r / 4, r / 8 + 1, c, true))
|
||||
ops.push(@scene.DrawOp::Circle(cx + r * 2 / 5, cy - r / 4, r / 8 + 1, c, true))
|
||||
ops.push(@scene.DrawOp::Line(cx - r / 2, cy + r / 4, cx - r / 5, cy + r / 2, c, 2))
|
||||
ops.push(@scene.DrawOp::Line(cx - r / 5, cy + r / 2, cx + r / 5, cy + r / 2, c, 2))
|
||||
ops.push(@scene.DrawOp::Line(cx + r / 5, cy + r / 2, cx + r / 2, cy + r / 4, c, 2))
|
||||
}
|
||||
|
||||
///| Point `k` of 12 on a circle of radius `r` (clockwise from the top).
|
||||
fn ring_point(k : Int, r : Int) -> (Int, Int) {
|
||||
// sin and cos of k * 30 degrees, times 1000.
|
||||
let s = [0, 500, 866, 1000, 866, 500, 0, -500, -866, -1000, -866, -500]
|
||||
let c = [1000, 866, 500, 0, -500, -866, -1000, -866, -500, 0, 500, 866]
|
||||
(s[k % 12] * r / 1000, -c[k % 12] * r / 1000)
|
||||
}
|
||||
|
||||
///| A right-pointing (`down`: down-pointing) chevron centred at (cx, cy).
|
||||
fn chevron(cx : Int, cy : Int, s : Int, down : Bool, left : Bool, c : @scene.Rgba, ops : Array[@scene.DrawOp]) -> Unit {
|
||||
if down {
|
||||
@@ -563,6 +581,55 @@ fn ChandraCompositor::auth_ops(self : ChandraCompositor) -> Array[@scene.DrawOp]
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
// Face unlock: what the face module in the PAM stack is doing (docs/face-unlock.md).
|
||||
if d.face != NoFace {
|
||||
let rh = 48
|
||||
let cx = pad + 25
|
||||
let cy = y + rh / 2
|
||||
let tint = match d.face {
|
||||
FaceFailed(_) => warn_c
|
||||
_ => p.accent
|
||||
}
|
||||
body.push(@scene.DrawOp::FillRect(@scene.Rect::new(pad, y, inner, rh), p.surface_hover, rad))
|
||||
face_glyph(cx, cy, 13, if d.face is FaceFailed(_) { p.foreground_muted } else { p.foreground }, body)
|
||||
match d.face {
|
||||
Scanning => {
|
||||
// A ring of dots sweeping round the face.
|
||||
let t = ((now - d.face_at) / 70UL).to_int()
|
||||
for k = 0; k < 12; k = k + 1 {
|
||||
let a = (k + t) % 12
|
||||
let (dx, dy) = ring_point(a, 19)
|
||||
let alpha = 40 + k * 18
|
||||
body.push(@scene.DrawOp::Circle(cx + dx, cy + dy, 2, @scene.Rgba::new(p.accent.r.to_int(), p.accent.g.to_int(), p.accent.b.to_int(), alpha), true))
|
||||
}
|
||||
}
|
||||
Matched => {
|
||||
body.push(@scene.DrawOp::Circle(cx + 13, cy + 11, 8, p.accent, true))
|
||||
body.push(@scene.DrawOp::Line(cx + 9, cy + 11, cx + 12, cy + 14, p.accent_foreground, 2))
|
||||
body.push(@scene.DrawOp::Line(cx + 12, cy + 14, cx + 17, cy + 8, p.accent_foreground, 2))
|
||||
}
|
||||
FaceFailed(_) => {
|
||||
body.push(@scene.DrawOp::Circle(cx + 13, cy + 11, 8, warn_c, true))
|
||||
body.push(@scene.DrawOp::Line(cx + 10, cy + 8, cx + 16, cy + 14, p.surface_popup, 2))
|
||||
body.push(@scene.DrawOp::Line(cx + 16, cy + 8, cx + 10, cy + 14, p.surface_popup, 2))
|
||||
}
|
||||
NoFace => ()
|
||||
}
|
||||
let (title, sub) = match d.face {
|
||||
Scanning => ("Looking for your face…", "Or type your password")
|
||||
Matched => ("Face recognized", "")
|
||||
FaceFailed(why) => (why, "Type your password instead")
|
||||
NoFace => ("", "")
|
||||
}
|
||||
let tx = pad + 54
|
||||
if sub == "" {
|
||||
body.push(@scene.DrawOp::Text(tx, cy, title, fs, tint, @scene.TextAlign::Left, true))
|
||||
} else {
|
||||
body.push(@scene.DrawOp::Text(tx, cy - 8, title, fs, tint, @scene.TextAlign::Left, true))
|
||||
body.push(@scene.DrawOp::Text(tx, cy + 10, sub, fs - 2, p.foreground_muted, @scene.TextAlign::Left, false))
|
||||
}
|
||||
y = y + rh + 12
|
||||
}
|
||||
// The prompt and the field.
|
||||
body.push(@scene.DrawOp::Text(pad, y + 7, d.prompt, fs - 1, p.foreground_muted, @scene.TextAlign::Left, true))
|
||||
y = y + 20
|
||||
@@ -592,7 +659,7 @@ fn ChandraCompositor::auth_ops(self : ChandraCompositor) -> Array[@scene.DrawOp]
|
||||
if d.typed() == 0 {
|
||||
let hint = match d.phase {
|
||||
Starting => "Preparing…"
|
||||
Waiting => "Waiting…"
|
||||
Waiting => if d.face == Scanning { "Type your password" } else { "Waiting…" }
|
||||
_ => if d.echo { "Type your answer" } else { "Type your password" }
|
||||
}
|
||||
body.push(@scene.DrawOp::Text(pad + 18, cy, hint, fs, @scene.Rgba::new(p.foreground_muted.r.to_int(), p.foreground_muted.g.to_int(), p.foreground_muted.b.to_int(), 150), @scene.TextAlign::Left, false))
|
||||
|
||||
@@ -348,8 +348,16 @@ test "Escape dismisses: polkitd hears Cancelled; polkitd withdrawing a request c
|
||||
assert_eq(comp.auth_dialog().unwrap().user().map(u => u.name), Some("ada"))
|
||||
let (h2, hello) = helper_accept(listen, comp, a)
|
||||
assert_eq(hello, "ada|c1")
|
||||
// Face unlock (pam_chandra_face.so): the face row scans, then gives way to the password.
|
||||
helper_say(h2, "PAM_TEXT_INFO Face unlock: looking for ada…")
|
||||
assert_true(settle(comp, a, fn() { comp.auth_dialog().map(d => d.face == Scanning).unwrap_or(false) }))
|
||||
assert_eq(comp.auth_dialog().unwrap().info, "") // the fingerprint message went with nandi's attempt
|
||||
auth_shot(comp, "14-face-scanning")
|
||||
helper_say(h2, "PAM_ERROR_MSG Face unlock: face not recognized")
|
||||
helper_say(h2, "PAM_PROMPT_ECHO_OFF Password: ")
|
||||
assert_true(settle(comp, a, fn() { comp.auth_dialog().map(d => d.phase == Prompt).unwrap_or(false) }))
|
||||
assert_eq(comp.auth_dialog().unwrap().face, FaceFailed("Face not recognized"))
|
||||
auth_shot(comp, "15-face-failed")
|
||||
auth_shot(comp, "12-other-account")
|
||||
auth_press(comp, 1U) // Escape
|
||||
assert_true(settle(comp, a, fn() { a.answers.length() == 1 }))
|
||||
|
||||
@@ -641,7 +641,8 @@ pub fn ChandraCompositor::has_timed_work(self : ChandraCompositor) -> Bool {
|
||||
self.auth_animating() ||
|
||||
self.thumbs_pending() ||
|
||||
self.overview_animating() ||
|
||||
self.shell.animating()
|
||||
self.shell.animating() ||
|
||||
self.shell.face_busy()
|
||||
}
|
||||
|
||||
///| How long an idle main loop may sleep: `idle_ms` (until the wall clock's next second), or
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
///| Face unlock in Settings > Session (docs/face-unlock.md), against a fake chandra-face: a shell
|
||||
/// script that answers `status` from files and changes them for enroll / remove / pam, the way
|
||||
/// the real one does after run0 or pkexec. CHANDRA_AUTH_SHOTS=dir also saves a PNG of the tab.
|
||||
|
||||
///| Write the fake tool into `dir` and return its path.
|
||||
fn fake_face_tool(dir : String) -> String {
|
||||
let script =
|
||||
#|#!/bin/sh
|
||||
#|S="$(dirname "$0")"
|
||||
#|case "$1" in
|
||||
#|status)
|
||||
#| echo "backend opencv"; echo "device /dev/video2"
|
||||
#| echo "pam $(cat "$S/pam" 2>/dev/null)"
|
||||
#| cat "$S/faces" 2>/dev/null ;;
|
||||
#|enroll)
|
||||
#| echo "info Look at the camera…"
|
||||
#| for i in 1 2 3 4 5; do echo "progress $i/5"; done
|
||||
#| printf 'face 2\tFace 2\t2026-11-26\n' >> "$S/faces"
|
||||
#| echo "done Added Face 2" ;;
|
||||
#|remove)
|
||||
#| grep -v "^face $2 " "$S/faces" > "$S/faces.new"; mv "$S/faces.new" "$S/faces"
|
||||
#| echo "done Removed face $2" ;;
|
||||
#|pam)
|
||||
#| if [ "$2" = enable ]; then echo "polkit-1,sudo" > "$S/pam"; else : > "$S/pam"; fi
|
||||
#| echo "done ok" ;;
|
||||
#|esac
|
||||
ignore(@sys.mkdir_p(dir))
|
||||
let tool = dir + "/chandra-face"
|
||||
ignore(@sys.write_file_bytes(tool, @sys.str_to_utf8_bytes(script)))
|
||||
ignore(@sys.write_file_bytes(dir + "/faces", @sys.str_to_utf8_bytes("face 1\tDesk\t2026-11-20\n")))
|
||||
tool
|
||||
}
|
||||
|
||||
///| Compose frames until `cond` holds (the tool runs on a socket), at most ~3 s.
|
||||
fn face_settle(comp : @host.ChandraCompositor, cond : () -> Bool) -> Bool {
|
||||
for i = 0; i < 300; i = i + 1 {
|
||||
ignore(comp.compose_frame())
|
||||
if cond() {
|
||||
return true
|
||||
}
|
||||
@sys.sleep_ms(10)
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///| The popup's texts.
|
||||
fn face_texts(comp : @host.ChandraCompositor) -> Array[String] {
|
||||
let slot = comp.shell.slot_of("session-settings")
|
||||
comp.shell.slot_popup_texts(slot).map(t => t.0)
|
||||
}
|
||||
|
||||
///| Click the popup text `label`.
|
||||
fn face_click(comp : @host.ChandraCompositor, label : String, t : UInt) -> Unit raise {
|
||||
let slot = comp.shell.slot_of("session-settings")
|
||||
let pr = comp.shell.slot_popup_rect(slot)
|
||||
match comp.shell.slot_popup_texts(slot).iter().find_first(x => x.0 == label) {
|
||||
Some((_, x, y)) => {
|
||||
comp.handle_input(@nested.InputEvent::PointerMove((pr.x + x + 4).to_double(), (pr.y + y).to_double()), t)
|
||||
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U)
|
||||
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 2U)
|
||||
ignore(comp.compose_frame())
|
||||
}
|
||||
None => fail("no \"" + label + "\" in the popup")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "Settings > Session: face unlock lists faces, adds one, turns it on, removes one" {
|
||||
let dir = "/tmp/chandra-face-test-" + @sys.getpid().to_string()
|
||||
let tool = fake_face_tool(dir)
|
||||
let comp = new_comp("face")
|
||||
// spawn-socket runs a program; the script has no x bit (sys has no chmod): through sh.
|
||||
comp.shell.set_face_tool(["/bin/sh", tool])
|
||||
assert_true(comp.shell.open_settings(Session))
|
||||
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Desk") }))
|
||||
let texts = face_texts(comp)
|
||||
assert_true(texts.contains("Face unlock"))
|
||||
assert_true(texts.contains("Use my face for passwords"))
|
||||
assert_true(texts.contains("added 2026-11-20"))
|
||||
assert_true(texts.contains("Add a face…"))
|
||||
assert_false(comp.shell.face.in_use())
|
||||
// Add a face: the tool's progress shows, then the new face.
|
||||
face_click(comp, "Add a face…", 10U)
|
||||
assert_true(face_settle(comp, fn() { face_texts(comp).contains("Face 2") }))
|
||||
assert_true(face_texts(comp).contains("Added Face 2"))
|
||||
// The switch: polkit-1 and sudo try the face first.
|
||||
face_click(comp, "Use my face for passwords", 20U)
|
||||
assert_true(face_settle(comp, fn() { comp.shell.face.in_use() }))
|
||||
let shots = @sys.getenv("CHANDRA_AUTH_SHOTS")
|
||||
if shots != "" {
|
||||
ignore(@sys.write_file_bytes(shots + "/16-settings-face.png", @renderer.encode_png_compressed(comp.compose_frame())))
|
||||
}
|
||||
// Remove takes a second click.
|
||||
face_click(comp, "Remove", 30U)
|
||||
assert_true(face_texts(comp).contains("Click again to remove"))
|
||||
assert_eq(comp.shell.face.faces.length(), 2)
|
||||
face_click(comp, "Click again to remove", 40U)
|
||||
assert_true(face_settle(comp, fn() { comp.shell.face.faces.length() == 1 }))
|
||||
assert_eq(comp.shell.face.faces[0].1, "Face 2")
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///|
|
||||
test "face unlock status lines" {
|
||||
let f = @shell.FaceUnlock::new()
|
||||
f.apply_status([
|
||||
"backend opencv", "device /dev/video0", "problem python3-opencv is not installed", "pam polkit-1,sudo",
|
||||
"howdy login,gdm-password", "face 3\tGlasses\t2026-11-01",
|
||||
])
|
||||
assert_true(f.installed)
|
||||
assert_eq(f.problem, "python3-opencv is not installed")
|
||||
assert_true(f.in_use())
|
||||
assert_eq(f.howdy, "login, gdm-password")
|
||||
assert_eq(f.faces, [("3", "Glasses", "2026-11-01")])
|
||||
// No tool: nothing is installed, whatever else was said.
|
||||
f.apply_status([])
|
||||
assert_false(f.installed)
|
||||
assert_false(f.in_use())
|
||||
assert_eq(@shell.face_progress("progress 2/5"), Some("Look at the camera… 2/5"))
|
||||
assert_eq(@shell.face_progress("error couldn't see a face clearly"), Some("Couldn't see a face clearly"))
|
||||
assert_eq(@shell.face_progress("face 1\tDesk\t2026-11-20"), None)
|
||||
}
|
||||
@@ -1694,7 +1694,8 @@ test "open_settings opens the settings module on the tab asked for" {
|
||||
assert_eq(pr.h, 40 + (1 + 4 + 6) * 28 + 6)
|
||||
assert_true(comp.shell.open_settings(Session))
|
||||
ignore(comp.compose_frame())
|
||||
assert_eq(comp.shell.slot_popup_rect(slot).h, 40 + 4 * 28 + 6) // the four settings
|
||||
// The four settings, then Face unlock (a heading; chandra-face isn't installed in tests).
|
||||
assert_eq(comp.shell.slot_popup_rect(slot).h, 40 + 6 * 28 + 6)
|
||||
}
|
||||
|
||||
///|
|
||||
|
||||
Reference in new issue
Block a user