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
@@ -14,6 +14,66 @@ pub(all) enum Phase {
|
||||
Waiting
|
||||
} derive(Eq, Debug)
|
||||
|
||||
///| Face unlock (docs/face-unlock.md): what the face module in the PAM stack said, for the
|
||||
/// dialog's face row. Chandra's own pam_chandra_face.so says "Face unlock: ..."; Howdy's
|
||||
/// pam_howdy.so says "Attempting facial authentication", "Identified face as ...", and its
|
||||
/// failures as errors.
|
||||
pub(all) enum FaceState {
|
||||
/// No face module spoke in this attempt.
|
||||
NoFace
|
||||
/// Looking for the face; typing the password meanwhile still works (it goes in when PAM asks).
|
||||
Scanning
|
||||
/// Recognized: PAM is finishing.
|
||||
Matched
|
||||
/// Not this time, and why ("Face not recognized"); the password is next.
|
||||
FaceFailed(String)
|
||||
} derive(Eq, Debug)
|
||||
|
||||
///| What a PAM message means for face unlock, if it is about it: Some(state), else None.
|
||||
pub fn face_message(m : String, is_error : Bool) -> FaceState? {
|
||||
let t = m.trim(chars=" \n").to_string()
|
||||
let low = t.to_lower()
|
||||
let ours = low.has_prefix("face unlock:")
|
||||
let body = if ours { t[12:].to_string().trim(chars=" ").to_string() } else { t }
|
||||
let lbody = body.to_lower()
|
||||
if ours {
|
||||
if is_error {
|
||||
let why = if lbody.has_prefix("face not recognized") || lbody == "" {
|
||||
"Face not recognized"
|
||||
} else {
|
||||
upper_first(body)
|
||||
}
|
||||
return Some(FaceFailed(why))
|
||||
}
|
||||
if lbody.has_prefix("recognized") {
|
||||
return Some(Matched)
|
||||
}
|
||||
return Some(Scanning)
|
||||
}
|
||||
// Howdy (pam_howdy.so).
|
||||
if low.has_prefix("attempting facial authentication") || low.has_prefix("attempting face detection") {
|
||||
return Some(Scanning)
|
||||
}
|
||||
if low.has_prefix("identified face as") {
|
||||
return Some(Matched)
|
||||
}
|
||||
if is_error && (low.has_prefix("failure, timeout reached") || low.has_prefix("failure, not a single face")) {
|
||||
return Some(FaceFailed("Face not recognized"))
|
||||
}
|
||||
if is_error && low.has_prefix("face detection image too dark") {
|
||||
return Some(FaceFailed("The camera picture is too dark"))
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
///| "camera not available" -> "Camera not available".
|
||||
fn upper_first(s : String) -> String {
|
||||
if s.length() == 0 {
|
||||
return s
|
||||
}
|
||||
s[:1].to_string().to_upper() + s[1:].to_string()
|
||||
}
|
||||
|
||||
///| The dialog's controls, in Tab order.
|
||||
pub(all) enum Focus {
|
||||
Field
|
||||
@@ -101,6 +161,10 @@ pub(all) struct AuthDialog {
|
||||
mut focus : Focus
|
||||
/// Enter came before PAM's prompt: answer as soon as it does.
|
||||
mut submit_queued : Bool
|
||||
/// Face unlock in this attempt (NoFace when no face module spoke).
|
||||
mut face : FaceState
|
||||
/// When face unlock started looking (monotonic ms; the scan animation's clock).
|
||||
mut face_at : UInt64
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -122,6 +186,8 @@ pub fn AuthDialog::new(req : AuthRequest, desc : Description) -> AuthDialog {
|
||||
shake_at: 0UL,
|
||||
focus: Field,
|
||||
submit_queued: false,
|
||||
face: NoFace,
|
||||
face_at: 0UL,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,8 +273,29 @@ fn AuthDialog::submit(self : AuthDialog) -> DialogEffect {
|
||||
|
||||
///| Something from the helper. `now` is the monotonic clock (for the shake).
|
||||
pub fn AuthDialog::helper(self : AuthDialog, ev : HelperEvent, now : UInt64) -> DialogEffect {
|
||||
match ev {
|
||||
TextInfo(m) | ErrorMsg(m) =>
|
||||
match face_message(m, ev is ErrorMsg(_)) {
|
||||
Some(f) => {
|
||||
if f == Scanning && self.face != Scanning {
|
||||
self.face_at = if now == 0UL { 1UL } else { now }
|
||||
}
|
||||
self.face = f
|
||||
if self.phase == Starting {
|
||||
self.phase = Waiting
|
||||
}
|
||||
return Nothing
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
match ev {
|
||||
PromptEchoOff(p) | PromptEchoOn(p) => {
|
||||
// The face module gave up without saying why (a timeout, or Howdy's silent failures).
|
||||
if self.face == Scanning {
|
||||
self.face = FaceFailed("Face not recognized")
|
||||
}
|
||||
self.phase = Prompt
|
||||
self.prompt = tidy_prompt(p)
|
||||
self.echo = ev is PromptEchoOn(_)
|
||||
@@ -245,6 +332,7 @@ pub fn AuthDialog::helper(self : AuthDialog, ev : HelperEvent, now : UInt64) ->
|
||||
self.submit_queued = false
|
||||
self.phase = Starting
|
||||
self.info = ""
|
||||
self.face = NoFace
|
||||
self.focus = Field
|
||||
Restart
|
||||
}
|
||||
@@ -282,6 +370,7 @@ fn AuthDialog::cycle_user(self : AuthDialog, delta : Int) -> DialogEffect {
|
||||
self.wipe()
|
||||
self.error = ""
|
||||
self.info = ""
|
||||
self.face = NoFace
|
||||
self.phase = Starting
|
||||
self.submit_queued = false
|
||||
Restart
|
||||
@@ -401,6 +490,16 @@ pub fn AuthDialog::shake_offset(self : AuthDialog, now : UInt64) -> Int {
|
||||
tri * amp / period
|
||||
}
|
||||
|
||||
///| What the face row says, "" when there is none.
|
||||
pub fn AuthDialog::face_text(self : AuthDialog) -> String {
|
||||
match self.face {
|
||||
NoFace => ""
|
||||
Scanning => "Looking for your face…"
|
||||
Matched => "Face recognized"
|
||||
FaceFailed(why) => why + ". Use your password."
|
||||
}
|
||||
}
|
||||
|
||||
///| Whether the card is still shaking.
|
||||
pub fn AuthDialog::shaking(self : AuthDialog, now : UInt64) -> Bool {
|
||||
self.shake_at != 0UL && now >= self.shake_at && now - self.shake_at < 420UL
|
||||
|
||||
@@ -325,3 +325,55 @@ test "the setuid route: the helper runs on a socket pair and speaks the protocol
|
||||
s.close()
|
||||
@sys.unlink(script)
|
||||
}
|
||||
|
||||
///|
|
||||
test "face unlock: Chandra's and Howdy's PAM messages" {
|
||||
assert_eq(@polkit.face_message("Face unlock: looking for nandi…", false), Some(Scanning))
|
||||
assert_eq(@polkit.face_message("Face unlock: recognized nandi", false), Some(Matched))
|
||||
assert_eq(@polkit.face_message("Face unlock: face not recognized", true), Some(FaceFailed("Face not recognized")))
|
||||
assert_eq(@polkit.face_message("Face unlock: the camera picture is too dark\n", true), Some(FaceFailed("The camera picture is too dark")))
|
||||
assert_eq(@polkit.face_message("Attempting facial authentication", false), Some(Scanning))
|
||||
assert_eq(@polkit.face_message("Identified face as nandi", false), Some(Matched))
|
||||
assert_eq(@polkit.face_message("Failure, timeout reached", true), Some(FaceFailed("Face not recognized")))
|
||||
assert_eq(@polkit.face_message("Face detection image too dark", true), Some(FaceFailed("The camera picture is too dark")))
|
||||
assert_eq(@polkit.face_message("Place your finger on the fingerprint reader", false), None)
|
||||
assert_eq(@polkit.face_message("The account is locked due to 3 failed logins.", true), None)
|
||||
}
|
||||
|
||||
///|
|
||||
test "dialog: face unlock scans, the password typed meanwhile goes in when it fails" {
|
||||
let d = dialog(1)
|
||||
ignore(d.helper(TextInfo("Face unlock: looking for nandi…"), 50UL))
|
||||
assert_eq(d.face, Scanning)
|
||||
assert_eq(d.face_at, 50UL)
|
||||
assert_eq(d.phase, Waiting)
|
||||
assert_eq(d.info, "") // the face row says it, not the info line
|
||||
// Typing while it looks is kept, and Enter waits for the password prompt.
|
||||
for c in "hunter2" {
|
||||
ignore(d.key(0U, 0U, Some(c)))
|
||||
}
|
||||
assert_true(d.key(@polkit.key_enter, 0U, None) is Nothing)
|
||||
ignore(d.helper(ErrorMsg("Face unlock: face not recognized"), 60UL))
|
||||
assert_eq(d.face, FaceFailed("Face not recognized"))
|
||||
assert_eq(d.error, "")
|
||||
assert_eq(d.face_text(), "Face not recognized. Use your password.")
|
||||
assert_eq(answer_text(d.helper(PromptEchoOff("Password: "), 70UL)), "hunter2")
|
||||
// A wrong password restarts the conversation: the face is tried again.
|
||||
assert_true(d.helper(Failure, 80UL) is Restart)
|
||||
assert_eq(d.face, NoFace)
|
||||
// Recognized: PAM finishes without a password.
|
||||
ignore(d.helper(TextInfo("Attempting facial authentication"), 90UL))
|
||||
assert_eq(d.face, Scanning)
|
||||
ignore(d.helper(TextInfo("Identified face as nandi"), 95UL))
|
||||
assert_eq(d.face, Matched)
|
||||
assert_true(d.helper(Success, 99UL) is Authorized)
|
||||
}
|
||||
|
||||
///|
|
||||
test "dialog: a face module that gives up silently still shows as not recognized" {
|
||||
let d = dialog(1)
|
||||
ignore(d.helper(TextInfo("Attempting facial authentication"), 1UL))
|
||||
ignore(d.helper(PromptEchoOff("Password: "), 2UL))
|
||||
assert_eq(d.face, FaceFailed("Face not recognized"))
|
||||
assert_eq(d.phase, Prompt)
|
||||
}
|
||||
Reference in new issue
Block a user