Files
codex/codex-rs/user-verification/src/native_operation.rs
T
riley-oai e7637306bc Add macOS user verification with Secure Enclave signing (#43624)
## Why

The user-verification API previously returned an unsupported provider on macOS. Enable native verification using biometric-protected credentials.

## What changed

- Implement credential status, creation, reuse, deletion, and challenge signing with P-256 Secure Enclave keys in the Data Protection Keychain.
- Require biometric authentication through the key's access-control policy and use a fresh `LAContext` for each signature.
- Report Touch ID hardware support independently of enrollment and current readiness, and map native failures to structured verification errors.
- Serialize credential operations across processes with cancellable file-lock waits. Invalidate pending authentication on cancellation and wait for the signer to exit before releasing the lock.

## Testing

Add unit tests for lock contention and cancellation, authentication teardown and late-result rejection, native error classification, and validation of Secure Enclave key attributes.

GitOrigin-RevId: 93268962cd824b2befc1a0c54b99dcb77a0f1044
2026-09-08 00:13:28 +00:00

55 lines
2.0 KiB
Rust

//! Keeps the authentication context on its owner thread while a native operation blocks.
use crate::UserVerificationError;
use crate::UserVerificationFailureReason;
use crate::UserVerificationRequestGuard;
use std::sync::mpsc;
use std::time::Duration;
pub(crate) fn run_with_cancellation<T: Send>(
guard: &UserVerificationRequestGuard,
operation: impl FnOnce() -> Result<T, UserVerificationError> + Send,
cancel: impl FnOnce(),
) -> Result<T, UserVerificationError> {
guard.check()?;
let (done_tx, done_rx) = mpsc::sync_channel(1);
std::thread::scope(|scope| {
let worker = std::thread::Builder::new()
.name("user-verification-sign".to_string())
.spawn_scoped(scope, move || {
let _ = done_tx.send(guard.check().and_then(|()| operation()));
})
.map_err(|error| {
tracing::warn!(%error, "could not start user-verification signer");
worker_error()
})?;
let result = loop {
match done_rx.recv_timeout(Duration::from_millis(/*millis*/ 50)) {
Ok(result) => break Some(result),
Err(mpsc::RecvTimeoutError::Timeout) => {
if !guard.is_active() {
cancel();
break None;
}
}
Err(mpsc::RecvTimeoutError::Disconnected) => break Some(Err(worker_error())),
}
};
// Join before returning so no signer outlives the context or its lifecycle lock.
worker.join().map_err(|_| worker_error())?;
guard.check()?;
result.unwrap_or_else(|| Err(worker_error()))
})
}
fn worker_error() -> UserVerificationError {
UserVerificationError::Failed {
reason: UserVerificationFailureReason::ProviderError,
message: "the user-verification signer could not complete".to_string(),
}
}
#[cfg(test)]
#[path = "native_operation_tests.rs"]
mod tests;