v0.2.6-pre.006
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-app-wallet-desk/src/app_state.rs
|
||||
// version: 6
|
||||
// version: 7
|
||||
|
||||
//! Shared backend state owned by the Wallet Desk Tauri application.
|
||||
|
||||
@@ -102,7 +102,7 @@ impl AppState {
|
||||
effective_wallets_directory_created_on_startup: self.wallet_config_startup.effective_directory_created_on_startup(),
|
||||
fallback_logging_active: runtime.fallback_active,
|
||||
root_wallets_directory_created_on_startup: self.wallet_config_startup.root_directory_created_on_startup(),
|
||||
shell_phase: "pre.005-wallet-session-create".to_owned(),
|
||||
shell_phase: "pre.006-wallet-unlock".to_owned(),
|
||||
startup_diagnostic: runtime.startup_diagnostic.clone(),
|
||||
wallets_directory: resolved.wallets_directory().to_string_lossy().into_owned(),
|
||||
wallets_subdirectory,
|
||||
@@ -123,10 +123,11 @@ impl AppState {
|
||||
}
|
||||
let root = self.wallet_inventory_root().to_path_buf();
|
||||
let resolved = crate::resolve_locked_wallet(root.as_path(), request.wallet_id).await;
|
||||
let (path, locked_info, dto) = match resolved {
|
||||
let (path, locked_info, mut dto) = match resolved {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
dto.configured_secret_candidate_count = self.secret_candidate_count_or_zero(dto.wallet_id.as_str());
|
||||
let session = self.wallet_session.lock();
|
||||
let mut session = match session {
|
||||
std::result::Result::Ok(value) => value,
|
||||
@@ -180,7 +181,8 @@ impl AppState {
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
let dto = crate::owner_projection(filename.as_str(), view_enabled, &owner);
|
||||
let configured_secret_candidate_count = self.secret_candidate_count_or_zero(filename.as_str());
|
||||
let dto = crate::owner_projection(filename.as_str(), view_enabled, configured_secret_candidate_count, &owner);
|
||||
let session = self.wallet_session.lock();
|
||||
let mut session = match session {
|
||||
std::result::Result::Ok(value) => value,
|
||||
@@ -191,6 +193,144 @@ impl AppState {
|
||||
return std::result::Result::Ok(dto);
|
||||
}
|
||||
|
||||
/// Opens the selected locked Wallet with one manual VIEW password supplied frontend -> Rust.
|
||||
pub(crate) async fn unlock_wallet_view_manual(&self, request: crate::WalletUnlockRequestDto) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let context = self.begin_unlock_operation(crate::WalletUnlockCapability::View);
|
||||
let context = match context {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let password = ksp_wallet_lib::ViewPassword::new(request.password);
|
||||
let opened = ksp_wallet_lib::open_wallet_view_file_v1(context.path.as_path(), password).await;
|
||||
let view = match opened {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), error_domain = error.code().domain(), error_code = error.code().code(), "Manual Wallet unlock failed");
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
return self.finish_view_unlock(context, view);
|
||||
}
|
||||
|
||||
/// Opens the selected locked Wallet with one manual OWNER password supplied frontend -> Rust.
|
||||
pub(crate) async fn unlock_wallet_owner_manual(&self, request: crate::WalletUnlockRequestDto) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let context = self.begin_unlock_operation(crate::WalletUnlockCapability::Owner);
|
||||
let context = match context {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let password = ksp_wallet_lib::OwnerPassword::new(request.password);
|
||||
let opened = ksp_wallet_lib::open_wallet_owner_file_v1(context.path.as_path(), password).await;
|
||||
let owner = match opened {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), error_domain = error.code().domain(), error_code = error.code().code(), "Manual Wallet unlock failed");
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
return self.finish_owner_unlock(context, owner);
|
||||
}
|
||||
|
||||
/// Explicitly attempts Config-owned Wallet password candidates until VIEW unlock succeeds or candidates are exhausted.
|
||||
pub(crate) async fn unlock_wallet_view_configured(&self) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let context = self.begin_unlock_operation(crate::WalletUnlockCapability::View);
|
||||
let context = match context {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let candidates = crate::discover_wallet_secret_candidates(&self.config_management, context.wallet_id.as_str());
|
||||
let candidates = match candidates {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
let candidate_count = candidates.len();
|
||||
if candidate_count == 0 {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
return std::result::Result::Err(configured_secret_missing_error());
|
||||
}
|
||||
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SECRET, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), candidate_count, "Explicit configured-secret Wallet unlock started");
|
||||
let mut last_error = std::option::Option::None;
|
||||
for candidate in candidates {
|
||||
let secret = candidate.reveal(&self.config_management);
|
||||
let secret = match secret {
|
||||
std::result::Result::Ok(std::option::Option::Some(value)) => value,
|
||||
std::result::Result::Ok(std::option::Option::None) => continue,
|
||||
std::result::Result::Err(error) => {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
let opened = ksp_wallet_lib::open_wallet_view_file_v1(context.path.as_path(), ksp_wallet_lib::ViewPassword::new(secret)).await;
|
||||
match opened {
|
||||
std::result::Result::Ok(view) => {
|
||||
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SECRET, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), candidate_count, "Configured-secret Wallet unlock succeeded");
|
||||
return self.finish_view_unlock_with_count(context, view, candidate_count);
|
||||
},
|
||||
std::result::Result::Err(error) => last_error = std::option::Option::Some(error),
|
||||
}
|
||||
}
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SECRET, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), candidate_count, "Configured-secret Wallet unlock exhausted all candidates");
|
||||
return match last_error {
|
||||
std::option::Option::Some(error) => std::result::Result::Err(error),
|
||||
std::option::Option::None => std::result::Result::Err(configured_secret_missing_error()),
|
||||
};
|
||||
}
|
||||
|
||||
/// Explicitly attempts Config-owned Wallet password candidates until OWNER unlock succeeds or candidates are exhausted.
|
||||
pub(crate) async fn unlock_wallet_owner_configured(&self) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let context = self.begin_unlock_operation(crate::WalletUnlockCapability::Owner);
|
||||
let context = match context {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let candidates = crate::discover_wallet_secret_candidates(&self.config_management, context.wallet_id.as_str());
|
||||
let candidates = match candidates {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
let candidate_count = candidates.len();
|
||||
if candidate_count == 0 {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
return std::result::Result::Err(configured_secret_missing_error());
|
||||
}
|
||||
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SECRET, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), candidate_count, "Explicit configured-secret Wallet unlock started");
|
||||
let mut last_error = std::option::Option::None;
|
||||
for candidate in candidates {
|
||||
let secret = candidate.reveal(&self.config_management);
|
||||
let secret = match secret {
|
||||
std::result::Result::Ok(std::option::Option::Some(value)) => value,
|
||||
std::result::Result::Ok(std::option::Option::None) => continue,
|
||||
std::result::Result::Err(error) => {
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
let opened = ksp_wallet_lib::open_wallet_owner_file_v1(context.path.as_path(), ksp_wallet_lib::OwnerPassword::new(secret)).await;
|
||||
match opened {
|
||||
std::result::Result::Ok(owner) => {
|
||||
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SECRET, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), candidate_count, "Configured-secret Wallet unlock succeeded");
|
||||
return self.finish_owner_unlock_with_count(context, owner, candidate_count);
|
||||
},
|
||||
std::result::Result::Err(error) => last_error = std::option::Option::Some(error),
|
||||
}
|
||||
}
|
||||
self.restore_locked_after_unlock_failure(&context);
|
||||
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SECRET, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), candidate_count, "Configured-secret Wallet unlock exhausted all candidates");
|
||||
return match last_error {
|
||||
std::option::Option::Some(error) => std::result::Result::Err(error),
|
||||
std::option::Option::None => std::result::Result::Err(configured_secret_missing_error()),
|
||||
};
|
||||
}
|
||||
|
||||
/// Drops an authorized handle, re-inspects the selected file and returns the session to `Locked`.
|
||||
pub(crate) async fn lock_wallet(&self) -> ksp_core_lib::Result<crate::LockedWalletDto> {
|
||||
let previous = {
|
||||
@@ -206,7 +346,23 @@ impl AppState {
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(crate::ERROR_CODE_WALLET_SESSION_INVALID, "No Wallet is selected to lock"));
|
||||
},
|
||||
crate::WalletSession::Locked { wallet_id, path, .. } => (wallet_id, path),
|
||||
crate::WalletSession::Owner { wallet_id, path, wallet } => {
|
||||
crate::WalletSession::PrivilegedOperation { wallet_id, path, locked_info, capability } => {
|
||||
let session = self.wallet_session.lock();
|
||||
let mut session = match session {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
|
||||
};
|
||||
*session = crate::WalletSession::PrivilegedOperation { wallet_id, path, locked_info, capability };
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(
|
||||
crate::ERROR_CODE_WALLET_SESSION_INVALID,
|
||||
"Wallet unlock operation is already in progress",
|
||||
));
|
||||
},
|
||||
crate::WalletSession::View { wallet_id, path, wallet, .. } => {
|
||||
drop(wallet);
|
||||
(wallet_id, path)
|
||||
},
|
||||
crate::WalletSession::Owner { wallet_id, path, wallet, .. } => {
|
||||
drop(wallet);
|
||||
(wallet_id, path)
|
||||
},
|
||||
@@ -216,7 +372,9 @@ impl AppState {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let configured_secret_candidate_count = self.secret_candidate_count_or_zero(wallet_id.as_str());
|
||||
let dto = crate::LockedWalletDto {
|
||||
configured_secret_candidate_count,
|
||||
filename: wallet_id.clone(),
|
||||
format_version: locked.format_version(),
|
||||
wallet_id: wallet_id.clone(),
|
||||
@@ -232,6 +390,140 @@ impl AppState {
|
||||
return std::result::Result::Ok(dto);
|
||||
}
|
||||
|
||||
fn begin_unlock_operation(&self, capability: crate::WalletUnlockCapability) -> ksp_core_lib::Result<WalletUnlockContext> {
|
||||
let session = self.wallet_session.lock();
|
||||
let mut session = match session {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
|
||||
};
|
||||
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
|
||||
let (wallet_id, path, locked_info) = match previous {
|
||||
crate::WalletSession::Locked { wallet_id, path, locked_info } => (wallet_id, path, locked_info),
|
||||
other => {
|
||||
*session = other;
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(
|
||||
crate::ERROR_CODE_WALLET_SESSION_INVALID,
|
||||
"Wallet must be selected and locked before unlock",
|
||||
));
|
||||
},
|
||||
};
|
||||
if capability == crate::WalletUnlockCapability::View && !locked_info.view_enabled() {
|
||||
*session = crate::WalletSession::Locked { wallet_id, path, locked_info };
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(
|
||||
crate::ERROR_CODE_WALLET_VIEW_DISABLED,
|
||||
"Selected Wallet does not expose an enabled VIEW capability",
|
||||
));
|
||||
}
|
||||
let context = WalletUnlockContext { capability, path: path.clone(), wallet_id: wallet_id.clone() };
|
||||
*session = crate::WalletSession::PrivilegedOperation { capability, wallet_id, path, locked_info };
|
||||
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = context.wallet_id.as_str(), capability = context.capability.label(), "Explicit Wallet unlock operation started");
|
||||
return std::result::Result::Ok(context);
|
||||
}
|
||||
|
||||
fn restore_locked_after_unlock_failure(&self, context: &WalletUnlockContext) {
|
||||
let session = self.wallet_session.lock();
|
||||
let mut session = match session {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(_) => return,
|
||||
};
|
||||
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
|
||||
match previous {
|
||||
crate::WalletSession::PrivilegedOperation { capability, wallet_id, path, locked_info }
|
||||
if capability == context.capability && wallet_id == context.wallet_id && path == context.path =>
|
||||
{
|
||||
*session = crate::WalletSession::Locked { wallet_id, path, locked_info };
|
||||
},
|
||||
other => *session = other,
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_view_unlock(&self, context: WalletUnlockContext, view: ksp_wallet_lib::WalletView) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let candidate_count = self.secret_candidate_count_or_zero(context.wallet_id.as_str());
|
||||
return self.finish_view_unlock_with_count(context, view, candidate_count);
|
||||
}
|
||||
|
||||
fn finish_view_unlock_with_count(
|
||||
&self,
|
||||
context: WalletUnlockContext,
|
||||
view: ksp_wallet_lib::WalletView,
|
||||
configured_secret_candidate_count: usize,
|
||||
) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let session = self.wallet_session.lock();
|
||||
let mut session = match session {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
|
||||
};
|
||||
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
|
||||
let (wallet_id, path, view_enabled) = match previous {
|
||||
crate::WalletSession::PrivilegedOperation { capability, wallet_id, path, locked_info }
|
||||
if capability == context.capability && wallet_id == context.wallet_id && path == context.path =>
|
||||
{
|
||||
(wallet_id, path, locked_info.view_enabled())
|
||||
},
|
||||
other => {
|
||||
*session = other;
|
||||
drop(view);
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(
|
||||
crate::ERROR_CODE_WALLET_SESSION_INVALID,
|
||||
"Wallet unlock completion no longer owns the selected session",
|
||||
));
|
||||
},
|
||||
};
|
||||
let dto = crate::view_projection(wallet_id.as_str(), view_enabled, configured_secret_candidate_count, &view);
|
||||
*session = crate::WalletSession::View { wallet_id: wallet_id.clone(), path, wallet: std::boxed::Box::new(view) };
|
||||
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), capability = "view", "Wallet VIEW session opened");
|
||||
return std::result::Result::Ok(dto);
|
||||
}
|
||||
|
||||
fn finish_owner_unlock(&self, context: WalletUnlockContext, owner: ksp_wallet_lib::WalletOwner) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let candidate_count = self.secret_candidate_count_or_zero(context.wallet_id.as_str());
|
||||
return self.finish_owner_unlock_with_count(context, owner, candidate_count);
|
||||
}
|
||||
|
||||
fn finish_owner_unlock_with_count(
|
||||
&self,
|
||||
context: WalletUnlockContext,
|
||||
owner: ksp_wallet_lib::WalletOwner,
|
||||
configured_secret_candidate_count: usize,
|
||||
) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
|
||||
let session = self.wallet_session.lock();
|
||||
let mut session = match session {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
|
||||
};
|
||||
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
|
||||
let (wallet_id, path, view_enabled) = match previous {
|
||||
crate::WalletSession::PrivilegedOperation { capability, wallet_id, path, locked_info }
|
||||
if capability == context.capability && wallet_id == context.wallet_id && path == context.path =>
|
||||
{
|
||||
(wallet_id, path, locked_info.view_enabled())
|
||||
},
|
||||
other => {
|
||||
*session = other;
|
||||
drop(owner);
|
||||
return std::result::Result::Err(ksp_core_lib::Error::new(
|
||||
crate::ERROR_CODE_WALLET_SESSION_INVALID,
|
||||
"Wallet unlock completion no longer owns the selected session",
|
||||
));
|
||||
},
|
||||
};
|
||||
let dto = crate::owner_projection(wallet_id.as_str(), view_enabled, configured_secret_candidate_count, &owner);
|
||||
*session = crate::WalletSession::Owner { wallet_id: wallet_id.clone(), path, wallet: std::boxed::Box::new(owner) };
|
||||
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), capability = "owner", "Wallet OWNER session opened");
|
||||
return std::result::Result::Ok(dto);
|
||||
}
|
||||
|
||||
fn secret_candidate_count_or_zero(&self, wallet_id: &str) -> usize {
|
||||
let count = crate::wallet_secret_candidate_count(&self.config_management, wallet_id);
|
||||
return match count {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SECRET, wallet_id, error_domain = error.code().domain(), error_code = error.code().code(), "Wallet configured-secret candidate count is unavailable");
|
||||
0
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the resolved common splash timings captured during bootstrap.
|
||||
#[must_use]
|
||||
pub(crate) const fn splash_settings(&self) -> crate::SplashSettings {
|
||||
@@ -260,6 +552,16 @@ fn session_lock_error() -> ksp_core_lib::Error {
|
||||
return ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_LOCK_FAILED, "Wallet Desk Wallet session state lock is poisoned");
|
||||
}
|
||||
|
||||
struct WalletUnlockContext {
|
||||
capability: crate::WalletUnlockCapability,
|
||||
path: std::path::PathBuf,
|
||||
wallet_id: String,
|
||||
}
|
||||
|
||||
fn configured_secret_missing_error() -> ksp_core_lib::Error {
|
||||
return ksp_core_lib::Error::new(crate::ERROR_CODE_WALLET_SECRET_CANDIDATES_MISSING, "No effective Config-owned Wallet password candidate is available");
|
||||
}
|
||||
|
||||
struct LoggingRuntimeState {
|
||||
guard: ksp_logging_lib::LoggingGuard,
|
||||
active_profile_id: std::option::Option<String>,
|
||||
|
||||
Reference in New Issue
Block a user