add qr acqusition for totp

This commit is contained in:
bunnie 2025-11-27 16:53:35 +08:00
parent 74134bfac2
commit 833fa96041

View file

@ -49,6 +49,10 @@ pub enum ActionOp {
UpdateOneItem,
ReloadDb,
Quit,
/// QR ops
AcquireQr,
#[cfg(feature = "vault-testing")]
/// Testing
GenerateTests,
@ -69,6 +73,8 @@ pub struct ActionManager {
mode_cache: VaultMode,
main_conn: xous::CID,
keystore: Keystore,
gfx: ux_api::service::gfx::Gfx,
}
impl ActionManager {
pub fn new(
@ -96,6 +102,7 @@ impl ActionManager {
action_active,
main_conn,
keystore: Keystore::new(&xns),
gfx: ux_api::service::gfx::Gfx::new(&xns).unwrap(),
}
}
@ -1131,6 +1138,75 @@ impl ActionManager {
log::debug!("heap usage B: {}", heap_usage());
}
pub(crate) fn acquire_qr(&mut self) {
match self.gfx.acquire_qr() {
Ok(qr_data) => {
if let Some(meta) = qr_data.meta {
log::info!("QR code metadata: {}", meta);
}
if let Some(qr_uri) = qr_data.content {
// now parse the QR URL. See spec below
/*
- otpauth://
- enroll TOTP token
- overwrites if previous exists
- pwauth://pass/<URL>?time=2025-11-27T13:06:02+08:00
- prompts to type password if it's in the database
- prompts to add new password (autogenerated) if it's not (need a new special character flow: ! @ # $ % ^ * ( ) - _ + = allowed )
- pwauth://new/<URL>?pass=<password>
- add a password to the password store (no 'time?' to simplify parsing of complicated passwords)
- prompt to allow, since the QR code is unauthenticated
- time://2025-11-27T13:06:02+08:00 -> time setting
- search://<string to search> -> search for an item in a list
- search://<string to search>?time=2025-11-27T13:06:02+08:00 -> search for an item in a list & sets time
*/
if let Some((request, data)) = qr_uri.split_once("://") {
match request {
"otpauth" => {
if let Ok(mut totp) = TotpRecord::from_uri(data) {
match self.storage.borrow_mut().new_record(&mut totp, None, true) {
Ok(_) => (),
Err(error) => {
log::error!("internal error");
self.report_err(
t!("vault.error.internal_error", locales::LANG),
Some(error),
);
}
};
let li = make_totp_item_from_record(&storage::hex(totp.hash()), totp);
self.item_lists.lock().unwrap().insert_unique(self.mode_cache, li);
} else {
self.modals
.show_notification(t!("vault.error.qr", locales::LANG), None)
.ok();
}
}
"pwauth" => {
// todo
}
"time" => {
// todo
}
"search" => {
// todo
}
_ => {
self.modals
.show_notification(t!("vault.error.qr_unrecognized", locales::LANG), None)
.ok();
}
}
}
}
}
Err(e) => {
log::error!("QR acquisition failed: {:?}", e);
// on error, etc. just note the issue and move on
}
}
}
/// This is just a placeholder function - needs to be upgraded to consider prompting the user for
/// a PIN value, etc.
pub(crate) fn get_pin_key(&self) -> [u8; 32] {