hook the totp rendering list to the internal database

This commit is contained in:
bunnie 2025-11-12 16:25:59 +08:00
parent 59325e0571
commit c650b0fb94
4 changed files with 84 additions and 18 deletions

View file

@ -1105,12 +1105,12 @@ impl ActionManager {
}
if oom_keys != 0 {
log::warn!(
"Ran out of cache space handling FIDO2 tokens. {} tokens are not loaded.",
"Ran out of cache space handling TOTP. {} entries are not loaded.",
oom_keys
);
self.report_err(
&format!(
"Ran out of cache space handling FIDO2 entries. {} tokens not loaded",
"Ran out of cache space handling TOTP. {} entries not loaded",
oom_keys
),
None::<crate::storage::Error>,
@ -1123,8 +1123,8 @@ impl ActionManager {
// this is fine, it just means no entries have been entered yet
}
_ => {
log::error!("Error opening FIDO2 dictionary");
self.report_err("Error opening FIDO2 dictionary", Some(e))
log::error!("Error opening TOTP dictionary");
self.report_err("Error opening TOTP dictionary", Some(e))
}
}
}

View file

@ -341,6 +341,8 @@ impl FilteredListView {
..((1 + page) * self.items_per_screen.get()).min(filterlen)]
}
pub fn full_list(&mut self) -> &mut [ListItem] { &mut self.list[..] }
pub fn nav(&mut self, dir: NavDir) {
log::debug!("index bef: {}, filter: {:?}", self.selection_index, self.filter_range);
match dir {
@ -523,4 +525,8 @@ impl ItemLists {
pub fn selected_page(&mut self, list_type: VaultMode) -> &mut [ListItem] {
self.li_mut(list_type).selected_page()
}
pub fn full_list(&mut self, list_type: VaultMode) -> &mut [ListItem] {
self.li_mut(list_type).full_list()
}
}

View file

@ -285,20 +285,29 @@ fn main() -> ! {
let modals = modals::Modals::new(&xns).unwrap();
vault_ui.apply_glyph_style();
// reload the database
xous::send_message(
actions_conn,
xous::Message::new_blocking_scalar(ActionOp::ReloadDb.to_usize().unwrap(), 0, 0, 0, 0),
)
.ok();
vault_ui.refresh_totp();
// kickstart the pumper
xous::send_message(pump_conn, xous::Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0))
.expect("couldn't start the pumper");
let mut menu_active = false;
loop {
let msg = xous::receive_message(sid).unwrap();
log::debug!("Got message: {:?}", msg.body.id());
log::trace!("Got message: {:?}", msg.body.id());
match FromPrimitive::from_usize(msg.body.id()) {
Some(VaultOp::Redraw) => {
log::debug!("Got redraw");
vault_ui.redraw();
}
Some(VaultOp::MenuDone) => {
menu_active = false;
// update the TOTP codes, in case there were changes
vault_ui.refresh_totp();
allow_totp_rendering.store(true, Ordering::SeqCst);
vault_ui.redraw();
}
@ -336,6 +345,21 @@ fn main() -> ! {
}
}
}),
Some(VaultOp::MenuEditStage1) => {
// stage 1 happens here because the filtered list and selection entry are in the responsive UX
// section.
log::debug!("selecting entry for edit");
if let Some(entry) = vault_ui.selected_entry() {
let buf = Buffer::into_buf(entry).expect("IPC error");
buf.send(actions_conn, ActionOp::MenuEditStage2.to_u32().unwrap())
.expect("messaging error");
} else {
// this will block redraws
allow_totp_rendering.store(false, Ordering::SeqCst);
modals.show_notification(t!("vault.error.nothing_selected", locales::LANG), None).ok();
allow_totp_rendering.store(true, Ordering::SeqCst);
}
}
Some(VaultOp::MenuChangeFont) => {
for item in FONT_LIST {
modals.add_list_item(item).expect("couldn't build radio item list");

View file

@ -97,9 +97,7 @@ impl VaultUi {
.set_margin(TotpLayout::totp_margin())
.pane_size(TotpLayout::list_box())
.style(TotpLayout::list_font());
for i in 0..6 {
totp_list.add_item(0, &format!("example {}", i));
}
let gfx = Gfx::new(&xns).unwrap();
let style = DEFAULT_FONT;
let glyph_height = gfx.glyph_height_hint(style).unwrap() as isize;
@ -117,6 +115,29 @@ impl VaultUi {
}
}
pub(crate) fn refresh_totp(&mut self) {
let mut locked_lists = self.item_lists.lock().unwrap();
let full_list = locked_lists.full_list(VaultMode::Totp);
self.totp_list.clear();
for item in full_list {
self.totp_list.add_item(0, &item.name());
}
}
pub(crate) fn update_selected_totp_code(&mut self) {
if self.totp_list.len() > 0 {
let selected = self.totp_list.get_selected();
let mut locked_lists = self.item_lists.lock().unwrap();
let full_list = locked_lists.full_list(VaultMode::Totp);
if let Some(selected_item) = full_list.iter().find(|item| item.name() == selected) {
match crate::totp::db_str_to_code(&selected_item.extra) {
Ok(s) => self.totp_code = Some(s),
_ => self.totp_code = None,
}
}
}
}
pub(crate) fn store_glyph_style(&mut self, style: GlyphStyle) {
self.pddb
.borrow()
@ -208,6 +229,11 @@ impl VaultUi {
tv.style = TotpLayout::totp_font();
tv.draw_border = false;
if self.totp_code.is_none() && self.totp_list.len() > 0 {
// this handles initial population of the field
self.update_selected_totp_code();
}
match &self.totp_code {
Some(code) => {
write!(tv, "{}", code).ok();
@ -236,14 +262,7 @@ impl VaultUi {
let epoch = (current_time / (30 * 1000)) as u64;
if self.last_epoch != epoch {
self.last_epoch = epoch;
if !self.item_lists.lock().unwrap().is_db_empty(VaultMode::Totp) {
match crate::totp::db_str_to_code(
&self.item_lists.lock().unwrap().selected_extra(VaultMode::Totp),
) {
Ok(s) => self.totp_code = Some(s),
_ => self.totp_code = None,
}
}
self.update_selected_totp_code();
}
let mut timer_remaining = TotpLayout::timer_box();
@ -292,7 +311,24 @@ impl VaultUi {
}
pub(crate) fn nav(&mut self, dir: NavDir) {
self.item_lists.lock().unwrap().nav((*self.mode.lock().unwrap()).clone(), dir);
let mode_at_entry = (*self.mode.lock().unwrap()).clone();
match mode_at_entry {
VaultMode::Password => {
self.item_lists.lock().unwrap().nav((*self.mode.lock().unwrap()).clone(), dir);
}
VaultMode::Totp => {
match dir {
NavDir::Up => {
self.totp_list.key_action('↑');
}
NavDir::Down => {
self.totp_list.key_action('↓');
}
_ => unimplemented!(),
}
self.totp_code = None;
}
}
}
pub(crate) fn filter(&mut self, criteria: &String) {