feat(tui): account settings — stats, password change, and logout

The settings popup opens with an Account block: who the session is logged in
as, how much it shares, the port peers reach it on, and the daemon it borrows
when the window is attached to one. Two rows follow, both confirmed before
they act. A password is typed twice, because the server sends no verdict on a
change and a typo entered once would leave the account with a password nobody
knows. Logging out closes the window and comes back to the login screen with
the stored password deleted, so the next start asks rather than walking
straight back in. A window attached to a daemon offers no logout: the login is
the daemon's.

"Change it on the server, then store it where the login lives" is one function
over the session and the secret store, so the window, the command and the
daemon share the rule rather than each spelling out its own idea of which
process owns the password.

Headless the same two acts are `account password` and `account logout`, and
the daemon answers `account.password` so a routed run is not a dead end —
`whoami` already reported who is logged in and what they share.
This commit is contained in:
Michel de Graaf
2026-09-10 11:10:58 +02:00
parent 4ea1e9c47c
commit d6d870b39f
26 changed files with 1137 additions and 215 deletions
+59
View File
@@ -0,0 +1,59 @@
//! The account itself: its password on the server, and the credentials this
//! machine keeps for it.
use super::{Ctx, Session};
use crate::cli::PasswordArgs;
use crate::output::{AccountRecord, CliError, CliResult, Out};
use crate::persist::secret::{KeyringStore, SecretStore};
/// Change the account's password and store the new one, so the next start
/// still logs in.
pub fn password(ctx: &Ctx, args: &PasswordArgs) -> CliResult {
let new = new_password(args)?;
let session = Session::open(ctx)?;
let user = session.client.username();
let stored = crate::persist::secret::change_password(
session.client.as_ref(),
&KeyringStore,
&new,
)
.map_err(|e| {
CliError::connection(format!("cannot change the password: {e}"))
})?;
if let Some(e) = stored {
ctx.out
.warn(&format!("password changed, but not stored here: {e}"));
}
ctx.out.emit(&AccountRecord {
user,
action: "password-changed".to_string(),
});
Ok(())
}
/// Forget the stored password. The account is untouched on the server; what
/// goes is this machine's copy of the way in.
pub fn logout(out: &Out, username: Option<&str>) -> CliResult {
let user = username.ok_or_else(crate::run::missing_username)?;
KeyringStore.delete(user).map_err(|e| {
CliError::usage(format!("cannot clear the stored password: {e}"))
})?;
out.emit(&AccountRecord {
user: user.to_string(),
action: "logged-out".to_string(),
});
Ok(())
}
/// Stdin beats the flag, the way `--password-stdin` beats `--password`.
fn new_password(args: &PasswordArgs) -> CliResult<String> {
if args.new_password_stdin {
return crate::run::password_from_stdin();
}
match args.new_password.as_deref() {
Some(password) if !password.is_empty() => Ok(password.to_string()),
_ => Err(CliError::usage(
"no new password: pass --new-password or --new-password-stdin",
)),
}
}
+9 -2
View File
@@ -5,6 +5,7 @@
//! first; the ones that only read the config file or the local machine are
//! dispatched by `main::run` before any credentials are demanded.
pub mod account;
pub mod completions;
pub mod daemon;
pub mod peer;
@@ -15,7 +16,9 @@ pub mod transfer;
pub mod wish;
use crate::api::SessionApi;
use crate::cli::{Commands, MessageCommand, RoomCommand, TransferCommand};
use crate::cli::{
AccountCommand, Commands, MessageCommand, RoomCommand, TransferCommand,
};
use crate::output::{CliError, CliResult, Exit, Out, PortmapRecord};
use crate::port_mapping::{self, PortMapper};
use crate::remote::{Endpoint, RemoteSession};
@@ -314,9 +317,13 @@ fn dispatch(ctx: &Ctx, command: Commands) -> CliResult {
Commands::User(args) => peer::user(ctx, &args),
Commands::Watch(command) => peer::watch(ctx, &command),
Commands::Whoami => peer::whoami(ctx),
Commands::Account(AccountCommand::Password(args)) => {
account::password(ctx, &args)
}
// main.rs runs these itself: some need no account, the rest need the
// config store in scope. The arms only keep the match exhaustive.
Commands::Portmap
Commands::Account(AccountCommand::Logout)
| Commands::Portmap
| Commands::Serve(_)
| Commands::Daemon(_)
| Commands::Wish(_)