mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
add in some more of the totp functional frameworks + itemcache
these frameworks are mostly unused at the moment but it lets us hook more easily into these artifacts once they are populated. next up...menu behavior in the totp widget
This commit is contained in:
parent
79b841142d
commit
1e680727a8
6 changed files with 79 additions and 34 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -6266,6 +6266,7 @@ dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"chrono",
|
"chrono",
|
||||||
"cram-hal-service",
|
"cram-hal-service",
|
||||||
|
"cramium-api",
|
||||||
"cramium-emu",
|
"cramium-emu",
|
||||||
"digest 0.9.0",
|
"digest 0.9.0",
|
||||||
"enum-iterator",
|
"enum-iterator",
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use core::num::NonZeroUsize;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
use crate::ux::framework::NavDir;
|
use crate::ux::NavDir;
|
||||||
use crate::{SelectedEntry, VaultMode};
|
use crate::{SelectedEntry, VaultMode};
|
||||||
|
|
||||||
pub struct ListKey {
|
pub struct ListKey {
|
||||||
|
|
@ -140,6 +140,7 @@ pub struct FilteredListView {
|
||||||
items_per_screen: NonZeroUsize,
|
items_per_screen: NonZeroUsize,
|
||||||
filter_range: Option<Range<usize>>,
|
filter_range: Option<Range<usize>>,
|
||||||
}
|
}
|
||||||
|
#[allow(dead_code)]
|
||||||
impl FilteredListView {
|
impl FilteredListView {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -429,24 +430,17 @@ impl FilteredListView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub struct ItemLists {
|
pub struct ItemLists {
|
||||||
fido: FilteredListView,
|
|
||||||
totp: FilteredListView,
|
totp: FilteredListView,
|
||||||
pw: FilteredListView,
|
pw: FilteredListView,
|
||||||
}
|
}
|
||||||
|
#[allow(dead_code)]
|
||||||
impl ItemLists {
|
impl ItemLists {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self { ItemLists { totp: FilteredListView::new(), pw: FilteredListView::new() } }
|
||||||
ItemLists {
|
|
||||||
fido: FilteredListView::new(),
|
|
||||||
totp: FilteredListView::new(),
|
|
||||||
pw: FilteredListView::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_db_empty(&self, list_type: VaultMode) -> bool { self.li(list_type).is_db_empty() }
|
pub fn is_db_empty(&self, list_type: VaultMode) -> bool { self.li(list_type).is_db_empty() }
|
||||||
|
|
||||||
fn li_mut(&mut self, list_type: VaultMode) -> &mut FilteredListView {
|
fn li_mut(&mut self, list_type: VaultMode) -> &mut FilteredListView {
|
||||||
match list_type {
|
match list_type {
|
||||||
VaultMode::Fido => &mut self.fido,
|
|
||||||
VaultMode::Totp => &mut self.totp,
|
VaultMode::Totp => &mut self.totp,
|
||||||
VaultMode::Password => &mut self.pw,
|
VaultMode::Password => &mut self.pw,
|
||||||
}
|
}
|
||||||
|
|
@ -454,7 +448,6 @@ impl ItemLists {
|
||||||
|
|
||||||
fn li(&self, list_type: VaultMode) -> &FilteredListView {
|
fn li(&self, list_type: VaultMode) -> &FilteredListView {
|
||||||
match list_type {
|
match list_type {
|
||||||
VaultMode::Fido => &self.fido,
|
|
||||||
VaultMode::Totp => &self.totp,
|
VaultMode::Totp => &self.totp,
|
||||||
VaultMode::Password => &self.pw,
|
VaultMode::Password => &self.pw,
|
||||||
}
|
}
|
||||||
|
|
@ -479,13 +472,11 @@ impl ItemLists {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_items_per_screen(&mut self, ips: isize) {
|
pub fn set_items_per_screen(&mut self, ips: isize) {
|
||||||
self.fido.set_items_per_screen(ips as usize);
|
|
||||||
self.totp.set_items_per_screen(ips as usize);
|
self.totp.set_items_per_screen(ips as usize);
|
||||||
self.pw.set_items_per_screen(ips as usize);
|
self.pw.set_items_per_screen(ips as usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mark_all_dirty(&mut self) {
|
pub fn mark_all_dirty(&mut self) {
|
||||||
self.fido.mark_all_dirty();
|
|
||||||
self.totp.mark_all_dirty();
|
self.totp.mark_all_dirty();
|
||||||
self.pw.mark_all_dirty();
|
self.pw.mark_all_dirty();
|
||||||
}
|
}
|
||||||
|
|
@ -495,7 +486,6 @@ impl ItemLists {
|
||||||
pub fn clear(&mut self, list_type: VaultMode) { self.li_mut(list_type).clear(); }
|
pub fn clear(&mut self, list_type: VaultMode) { self.li_mut(list_type).clear(); }
|
||||||
|
|
||||||
pub fn clear_all(&mut self) {
|
pub fn clear_all(&mut self) {
|
||||||
self.fido.clear();
|
|
||||||
self.pw.clear();
|
self.pw.clear();
|
||||||
self.totp.clear();
|
self.totp.clear();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
|
|
||||||
mod ux;
|
|
||||||
use totp::PumpOp;
|
use totp::PumpOp;
|
||||||
|
mod ux;
|
||||||
use ux::*;
|
use ux::*;
|
||||||
|
mod itemcache;
|
||||||
|
use itemcache::*;
|
||||||
mod totp;
|
mod totp;
|
||||||
|
|
||||||
pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_";
|
pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_";
|
||||||
|
|
@ -24,6 +25,13 @@ pub enum VaultMode {
|
||||||
Password,
|
Password,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)]
|
||||||
|
pub struct SelectedEntry {
|
||||||
|
pub key_guid: String,
|
||||||
|
pub description: String,
|
||||||
|
pub mode: VaultMode,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
log_server::init_wait().unwrap();
|
log_server::init_wait().unwrap();
|
||||||
log::set_max_level(log::LevelFilter::Info);
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
|
|
@ -35,11 +43,12 @@ fn main() -> ! {
|
||||||
let sid = xns.register_name(SERVER_NAME_VAULT2, None).expect("can't register server");
|
let sid = xns.register_name(SERVER_NAME_VAULT2, None).expect("can't register server");
|
||||||
let conn = xous::connect(sid).unwrap();
|
let conn = xous::connect(sid).unwrap();
|
||||||
|
|
||||||
let mut vault_ui = VaultUi::new(&xns, conn);
|
|
||||||
|
|
||||||
// global shared state
|
// global shared state
|
||||||
let mode = Arc::new(Mutex::new(VaultMode::Totp));
|
let mode = Arc::new(Mutex::new(VaultMode::Totp));
|
||||||
let allow_totp_rendering = Arc::new(AtomicBool::new(true));
|
let allow_totp_rendering = Arc::new(AtomicBool::new(true));
|
||||||
|
let item_lists = Arc::new(Mutex::new(ItemLists::new()));
|
||||||
|
|
||||||
|
let mut vault_ui = VaultUi::new(&xns, conn, item_lists.clone());
|
||||||
|
|
||||||
// spawn the TOTP pumper
|
// spawn the TOTP pumper
|
||||||
let pump_sid = xous::create_server().unwrap();
|
let pump_sid = xous::create_server().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use hmac::{Hmac, Mac};
|
use hmac::{Hmac, Mac};
|
||||||
|
use locales::t;
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use sha1::Sha1;
|
use sha1::Sha1;
|
||||||
use xous::{Message, send_message};
|
use xous::{Message, send_message};
|
||||||
|
|
@ -21,6 +22,11 @@ pub enum TotpAlgorithm {
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum TotpError {
|
||||||
|
BadRecord,
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for TotpAlgorithm {
|
impl Default for TotpAlgorithm {
|
||||||
fn default() -> Self { Self::None }
|
fn default() -> Self { Self::None }
|
||||||
}
|
}
|
||||||
|
|
@ -194,3 +200,32 @@ pub(crate) fn pumper(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn db_str_to_code(db_str: &str) -> Result<String, TotpError> {
|
||||||
|
let fields = db_str.split(':').collect::<Vec<&str>>();
|
||||||
|
if fields.len() == 5 {
|
||||||
|
let shared_secret =
|
||||||
|
base32::decode(base32::Alphabet::RFC4648 { padding: false }, fields[0]).unwrap_or(vec![]);
|
||||||
|
let digit_count = u8::from_str_radix(fields[1], 10).unwrap_or(6);
|
||||||
|
let step_seconds = u64::from_str_radix(fields[2], 10).unwrap_or(30);
|
||||||
|
let algorithm = TotpAlgorithm::try_from(fields[3]).unwrap_or(TotpAlgorithm::HmacSha1);
|
||||||
|
let is_hotp = fields[4].to_uppercase() == "HOTP";
|
||||||
|
let totp = TotpEntry {
|
||||||
|
step_seconds: if !is_hotp { step_seconds } else { 1 }, /* step_seconds is re-used
|
||||||
|
* by hotp as the code. */
|
||||||
|
shared_secret,
|
||||||
|
digit_count,
|
||||||
|
algorithm,
|
||||||
|
};
|
||||||
|
let code = if !is_hotp {
|
||||||
|
generate_totp_code(get_current_unix_time().unwrap_or(0), &totp)
|
||||||
|
.unwrap_or(t!("vault.error.record_error", locales::LANG).to_string())
|
||||||
|
} else {
|
||||||
|
generate_totp_code(step_seconds, &totp)
|
||||||
|
.unwrap_or(t!("vault.error.record_error", locales::LANG).to_string())
|
||||||
|
};
|
||||||
|
Ok(code)
|
||||||
|
} else {
|
||||||
|
Err(TotpError::BadRecord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,22 @@
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use blitstr2::GlyphStyle;
|
use blitstr2::GlyphStyle;
|
||||||
use locales::t;
|
|
||||||
use num_traits::*;
|
|
||||||
use ux_api::minigfx::*;
|
use ux_api::minigfx::*;
|
||||||
use ux_api::service::api::Gid;
|
use ux_api::service::api::Gid;
|
||||||
use ux_api::service::gfx::Gfx;
|
use ux_api::service::gfx::Gfx;
|
||||||
use ux_api::widgets::ScrollableList;
|
use ux_api::widgets::ScrollableList;
|
||||||
use xous::CID;
|
use xous::CID;
|
||||||
|
|
||||||
use crate::VaultOp;
|
use crate::ItemLists;
|
||||||
|
use crate::VaultMode;
|
||||||
|
|
||||||
|
pub enum NavDir {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
PageUp,
|
||||||
|
PageDown,
|
||||||
|
}
|
||||||
|
|
||||||
/// Centralizes tunable UI parameters for TOTP
|
/// Centralizes tunable UI parameters for TOTP
|
||||||
struct TotpLayout {}
|
struct TotpLayout {}
|
||||||
|
|
@ -37,14 +44,15 @@ pub struct VaultUi {
|
||||||
main_cid: CID,
|
main_cid: CID,
|
||||||
gfx: Gfx,
|
gfx: Gfx,
|
||||||
totp_list: ScrollableList,
|
totp_list: ScrollableList,
|
||||||
|
item_lists: Arc<Mutex<ItemLists>>,
|
||||||
|
|
||||||
/// totp redraw state
|
/// totp redraw state
|
||||||
totp_code: Option<u32>,
|
totp_code: Option<String>,
|
||||||
last_epoch: u64,
|
last_epoch: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VaultUi {
|
impl VaultUi {
|
||||||
pub fn new(xns: &xous_names::XousNames, cid: xous::CID) -> Self {
|
pub fn new(xns: &xous_names::XousNames, cid: xous::CID, item_lists: Arc<Mutex<ItemLists>>) -> Self {
|
||||||
let mut totp_list = ScrollableList::default()
|
let mut totp_list = ScrollableList::default()
|
||||||
.set_margin(TotpLayout::totp_margin())
|
.set_margin(TotpLayout::totp_margin())
|
||||||
.pane_size(TotpLayout::list_box())
|
.pane_size(TotpLayout::list_box())
|
||||||
|
|
@ -56,6 +64,7 @@ impl VaultUi {
|
||||||
main_cid: cid,
|
main_cid: cid,
|
||||||
gfx: Gfx::new(&xns).unwrap(),
|
gfx: Gfx::new(&xns).unwrap(),
|
||||||
totp_list,
|
totp_list,
|
||||||
|
item_lists,
|
||||||
totp_code: None,
|
totp_code: None,
|
||||||
last_epoch: crate::totp::get_current_unix_time().expect("couldn't get current time") / 30,
|
last_epoch: crate::totp::get_current_unix_time().expect("couldn't get current time") / 30,
|
||||||
}
|
}
|
||||||
|
|
@ -85,12 +94,12 @@ impl VaultUi {
|
||||||
tv.style = TotpLayout::totp_font();
|
tv.style = TotpLayout::totp_font();
|
||||||
tv.draw_border = false;
|
tv.draw_border = false;
|
||||||
|
|
||||||
match self.totp_code {
|
match &self.totp_code {
|
||||||
Some(code) => {
|
Some(code) => {
|
||||||
write!(tv, "{} {}", code / 1000, code % 1000).ok();
|
write!(tv, "{}", code).ok();
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
write!(tv, "*** ***").ok();
|
write!(tv, "******").ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.gfx.draw_textview(&mut tv).expect("couldn't draw text");
|
self.gfx.draw_textview(&mut tv).expect("couldn't draw text");
|
||||||
|
|
@ -113,6 +122,14 @@ impl VaultUi {
|
||||||
let epoch = (current_time / (30 * 1000)) as u64;
|
let epoch = (current_time / (30 * 1000)) as u64;
|
||||||
if self.last_epoch != epoch {
|
if self.last_epoch != epoch {
|
||||||
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut timer_remaining = TotpLayout::timer_box();
|
let mut timer_remaining = TotpLayout::timer_box();
|
||||||
|
|
|
||||||
|
|
@ -19,13 +19,6 @@ use crate::actions::ActionOp;
|
||||||
use crate::totp::{TotpAlgorithm, TotpEntry, generate_totp_code, get_current_unix_time};
|
use crate::totp::{TotpAlgorithm, TotpEntry, generate_totp_code, get_current_unix_time};
|
||||||
use crate::{ItemLists, SelectedEntry, VaultMode};
|
use crate::{ItemLists, SelectedEntry, VaultMode};
|
||||||
|
|
||||||
pub enum NavDir {
|
|
||||||
Up,
|
|
||||||
Down,
|
|
||||||
PageUp,
|
|
||||||
PageDown,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct VaultUx {
|
pub struct VaultUx {
|
||||||
/// the content area
|
/// the content area
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue