add a time progress bar in TOTP mode

This commit is contained in:
bunnie 2022-06-24 03:40:21 +08:00
parent 7765f3dec3
commit 9a5c04cc73
3 changed files with 101 additions and 2 deletions

View file

@ -223,6 +223,10 @@ fn main() -> ! {
icontray_server(conn);
}
});
// spawn the TOTP pumper
let pump_sid = xous::create_server().unwrap();
crate::totp::pumper(mode.clone(), pump_sid, conn);
let pump_conn = xous::connect(pump_sid).unwrap();
let menu_sid = xous::create_server().unwrap();
let menu_mgr = submenu::create_submenu(conn, actions_conn, menu_sid);
@ -288,6 +292,10 @@ fn main() -> ! {
Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0)
).ok();
vaultux.update_mode();
// this will start a periodic pump to keep the UX updating
send_message(pump_conn,
Message::new_scalar(totp::PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)
).ok();
}
"\u{0013}" => {
*mode.lock().unwrap() = VaultMode::Password;

View file

@ -5,6 +5,11 @@ use std::{
convert::TryFrom,
time::{SystemTime, SystemTimeError},
};
use std::sync::{Arc, Mutex};
use xous::{Message, send_message};
use std::thread;
use crate::VaultMode;
use num_traits::*;
// Derived from https://github.com/blakesmith/xous-core/blob/xtotp-time/apps/xtotp/src/main.rs
#[derive(Clone, Copy)]
@ -127,4 +132,46 @@ pub(crate) fn generate_totp_code(unix_timestamp: u64, totp_entry: &TotpEntry) ->
);
Ok(truncated_code)
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub(crate) enum PumpOp {
Pump,
Quit,
}
pub(crate) fn pumper(mode: Arc<Mutex<VaultMode>>, sid: xous::SID, main_conn: xous::CID) {
let _ = thread::spawn({
move || {
let tt = ticktimer_server::Ticktimer::new().unwrap();
let self_conn = xous::connect(sid).unwrap();
loop {
let msg = xous::receive_message(sid).unwrap();
let opcode: Option<PumpOp> = FromPrimitive::from_usize(msg.body.id());
log::trace!("{:?}", opcode);
match opcode {
Some(PumpOp::Pump) => {
send_message(main_conn,
Message::new_scalar(crate::VaultOp::Redraw.to_usize().unwrap(),
0, 0, 0, 0)
).expect("couldn't pump redraw");
let mode_cache = {(*mode.lock().unwrap()).clone()};
{ // we really want mode.lock() to be in a different scope so...
if mode_cache == VaultMode::Totp {
tt.sleep_ms(2000).unwrap();
send_message(self_conn,
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(),
0, 0, 0, 0)
).expect("couldn't restart pump");
}
}
// if not in Totp mode, the restart message doesn't go through, and the redraws automatically stop.
},
Some(PumpOp::Quit) => {break;}
_ => log::warn!("couldn't parse message: {:?}", msg),
}
}
xous::destroy_server(sid).ok();
}
});
}

View file

@ -89,6 +89,10 @@ pub(crate) struct VaultUx {
/// usb interface
usb_dev: usb_device_xous::UsbHid,
/// totp redraw state
last_epoch: u64,
current_time: u64,
}
pub(crate) const DEFAULT_FONT: GlyphStyle = GlyphStyle::Regular;
@ -164,6 +168,8 @@ impl VaultUx {
let item_height = (glyph_height * 2) as i16 + margin.y * 2 + 2; // +2 because of the border width
let items_per_screen = available_height / item_height;
let current_time = totp::get_current_unix_time().unwrap_or(0);
VaultUx {
content,
gam,
@ -184,6 +190,8 @@ impl VaultUx {
actions_conn,
action_active,
usb_dev: usb_device_xous::UsbHid::new(),
last_epoch: current_time / 30,
current_time,
}
}
@ -459,7 +467,27 @@ impl VaultUx {
}
pub(crate) fn redraw(&mut self) -> Result<(), xous::Error> {
// to reduce locking thrash, we cache a copy of the current mode at the top of redraw.
// this could lead to some race conditions that lead to awkward problems, we'll see, but it
// is probably worth the performance improvement.
let mode_at_entry = (*self.mode.lock().unwrap()).clone();
if mode_at_entry == VaultMode::Totp { // always redraw the title in TOTP mode
self.title_dirty = true;
// always grab the current time, regardless of the mode? saves an extra call to get time...
self.current_time = totp::get_current_unix_time().unwrap_or(0);
// duration bar is hard-coded to once every 30 seconds, even if the keys may not change that often.
let epoch = self.current_time / 30;
if self.last_epoch != epoch {
self.last_epoch = epoch;
// force a redraw of all the items if the epoch has changed
for item in self.filtered_list.iter_mut() {
item.dirty = true;
}
}
}
self.clear_area();
// ---- draw title area ----
if self.title_dirty || self.action_active.load(AtomicOrdering::SeqCst) {
let mut title_text = TextView::new(self.content,
@ -473,12 +501,28 @@ impl VaultUx {
title_text.draw_border = false;
title_text.clear_area = true;
title_text.style = GlyphStyle::Large;
match *self.mode.lock().unwrap() {
match mode_at_entry {
VaultMode::Fido => write!(title_text, "FIDO").ok(),
VaultMode::Totp => write!(title_text, "⏳1234").ok(),
VaultMode::Password => write!(title_text, "🔐****").ok(),
};
self.gam.post_textview(&mut title_text).expect("couldn't post title");
if mode_at_entry == VaultMode::Totp {
const BAR_HEIGHT: i16 = 5;
const BAR_GAP: i16 = -10;
// draw the duration bar
let delta = (self.current_time - (self.last_epoch * 30)) as i32;
let width = (self.screensize.x - (self.margin.x * 2)) as i32;
let delta_width = (delta * width * 100) / (30 * 100);
self.gam.draw_rectangle(
self.content,
Rectangle {
tl: Point::new(self.margin.x, TITLE_HEIGHT - (BAR_HEIGHT + BAR_GAP)),
br: Point::new(self.screensize.x - self.margin.x - delta_width as i16, TITLE_HEIGHT - BAR_GAP),
style: DrawStyle { fill_color: Some(PixelColor::Dark), stroke_color: None, stroke_width: 0 }
}
).ok();
}
self.title_dirty = false;
}
if self.action_active.load(AtomicOrdering::SeqCst) {
@ -535,7 +579,7 @@ impl VaultUx {
if index == selected as usize {
box_text.border_width = 4;
}
match *self.mode.lock().unwrap() {
match mode_at_entry {
VaultMode::Fido | VaultMode::Password => {write!(box_text, "{}\n{}", item.name, item.extra).ok();},
VaultMode::Totp => {
let fields = item.extra.split(':').collect::<Vec<&str>>();