Let the user name an app that declares no metadata
A client that sends no metadata — the bunker direction often sends none —
leaves only an npub, and there was no way to name it.
Add `App.label`, the user's own name, kept separate from the client's `name`:
`approve` rebuilds the entry on every pairing, so a label stored in `name`
would be lost the next time a metadata-less client reconnects. `display_name()`
prefers the label, and `AppRegistry::client()` returns it, so the approval
dialog and the activity log show what the user calls the app; a declared name
that differs stays visible as a "Declared name" row.
The "wants to connect" dialog asks for a name, right under "Application", when
the client declared none. The answer to a prompt becomes
`Answer { decision, name }` so the name reaches the responder's `approve`
(headless `--pair` passes none). `approve` preserves an existing label when the
dialog collected none.
The Apps page gets a per-app `view-more` menu — Rename… and Revoke — replacing
the visible Revoke suffix, and a rename dialog.
This commit is contained in:
10 files changed
+404
-81
No files matched your search
@@ -142,7 +142,11 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
|
||||
bounded, scrollable card, so a long note cannot push the buttons off screen.
|
||||
"Signing as" and "Application" stay above the fold; the verifiable-but-secondary
|
||||
fields (client key, declared URL, kind number, raw tags, method, peer, "asked
|
||||
for") live in a collapsed **Details** expander. The "name and URL are supplied
|
||||
for") live in a collapsed **Details** expander. A pairing whose client declared
|
||||
no name also gets a **name field** there — under "Application", above the
|
||||
Details — because that is the one prompt where the user decides to trust an
|
||||
app and can still give it a name; the entered name travels back in the
|
||||
`Answer`. The "name and URL are supplied
|
||||
by the application" warning is shown on the **pair** screen only — on an
|
||||
already-approved app's request it is banner blindness. Both this dialog and
|
||||
"Connect an app" use `plain::DIALOG_WIDTH` (480) so neither resizes as the user
|
||||
@@ -314,13 +318,24 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
|
||||
- **Create `AppState` once per process** (in `application.rs`, captured by the
|
||||
activate closure), not per window. A second `AppState` would mean a second
|
||||
registry and a second signer on the same device key.
|
||||
- The Apps page shows one `AdwExpanderRow` per app: per-app *Revoke* as a
|
||||
suffix (visible while collapsed), one *Remove* row per stored permission
|
||||
(kind for `sign_event`, method for encryption), and *Forget all*. The
|
||||
collapsed byline is the **host** the app declares (`declared_host`), falling
|
||||
back to the shortened npub when it declares none; the full public key and the
|
||||
full URL are rows in the details. A declared URL is display data the client
|
||||
controls — it proves nothing, and the host is shown so the byline stays short.
|
||||
- The Apps page shows one `AdwExpanderRow` per app, with a per-app `view-more`
|
||||
menu as its only suffix (*Rename…* and *Revoke*), one *Remove* row per stored
|
||||
permission (kind for `sign_event`, method for encryption), and *Forget all*.
|
||||
The title is the app's **display name**: the user's own `label` first, then
|
||||
what the client declared, else the shortened npub. The collapsed byline is the
|
||||
**host** the app declares (`declared_host`), falling back to the shortened npub
|
||||
when it declares none; the full public key and the full URL are rows in the
|
||||
details. A declared URL is display data the client controls — it proves
|
||||
nothing, and the host is shown so the byline stays short.
|
||||
- **A client may declare no name at all** (the bunker direction often sends no
|
||||
metadata), leaving only an npub. `App.label` is the user's own name for it,
|
||||
deliberately separate from the client's `name`: `approve` rebuilds the entry on
|
||||
every pairing, so a label kept in `name` would be lost the next time a
|
||||
metadata-less client reconnects. `App::display_name()` prefers the label, and
|
||||
`AppRegistry::client()` returns it, so the approval dialog and the activity log
|
||||
show what the user calls the app. The "wants to connect" dialog asks for a name
|
||||
when the client declared none, and `approve` preserves an existing label when
|
||||
the dialog collected none.
|
||||
- The activity log (`store/audit.rs`) records **metadata only**: app, method,
|
||||
event kind, outcome, time. `Entry` has a typed `kind: Option<u16>` and
|
||||
deliberately **no free-form detail field**, so event content, plaintext and
|
||||
|
||||
+3
-3
@@ -23,7 +23,7 @@ use zeroize::Zeroizing;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::runtime;
|
||||
use crate::signer::approval::{Approver, Decision, Notice, Prompt};
|
||||
use crate::signer::approval::{Answer, Approver, Notice, Prompt};
|
||||
use crate::signer::{ClientPairing, Signer, SignerConfig, parse_client_uri};
|
||||
use crate::store::apps::AppRegistry;
|
||||
use crate::store::audit::ActivityLog;
|
||||
@@ -125,7 +125,7 @@ async fn serve(
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(Decision::Deny).await;
|
||||
let _ = reply.send(Answer::denied()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let _ = reply.send(false).await;
|
||||
@@ -200,7 +200,7 @@ async fn finish_pairing(
|
||||
pairing: &ClientPairing,
|
||||
identity: PublicKey,
|
||||
) -> Result<(), String> {
|
||||
crate::signer::pair_client(signer, registry, activity, changes, pairing, identity)
|
||||
crate::signer::pair_client(signer, registry, activity, changes, pairing, identity, None)
|
||||
.await
|
||||
.map_err(|error| format!("pairing failed: {error}"))?;
|
||||
|
||||
|
||||
+41
-10
@@ -106,6 +106,37 @@ pub enum Decision {
|
||||
Always,
|
||||
}
|
||||
|
||||
/// The user's answer to a prompt: the decision, plus any name they gave the
|
||||
/// application in the "wants to connect" dialog.
|
||||
///
|
||||
/// The name is only ever set there, and only for a client that declared none —
|
||||
/// it is the user's own label for an otherwise nameless npub.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Answer {
|
||||
pub decision: Decision,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
impl Answer {
|
||||
/// No answer — a timeout, a missing UI, or a refusal.
|
||||
pub fn denied() -> Self {
|
||||
Self {
|
||||
decision: Decision::Deny,
|
||||
name: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A plain decision with no name — what every non-pairing prompt answers.
|
||||
impl From<Decision> for Answer {
|
||||
fn from(decision: Decision) -> Self {
|
||||
Self {
|
||||
decision,
|
||||
name: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything the user is shown, and what the answer would apply to.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Question {
|
||||
@@ -123,7 +154,7 @@ pub enum Prompt {
|
||||
/// Decide on a request.
|
||||
Approve {
|
||||
question: Question,
|
||||
reply: Sender<Decision>,
|
||||
reply: Sender<Answer>,
|
||||
/// When the dialog must give up and answer `Deny`.
|
||||
///
|
||||
/// **Absolute, and fixed when the ask starts** — not a duration the UI
|
||||
@@ -184,7 +215,7 @@ impl Approver {
|
||||
}
|
||||
|
||||
/// Ask, and treat a timeout or a missing UI as a denial.
|
||||
pub async fn ask(&self, question: Question) -> Decision {
|
||||
pub async fn ask(&self, question: Question) -> Answer {
|
||||
self.ask_within(ASK_TIMEOUT, ASK_BACKSTOP, question).await
|
||||
}
|
||||
|
||||
@@ -197,7 +228,7 @@ impl Approver {
|
||||
deadline: Duration,
|
||||
within: Duration,
|
||||
question: Question,
|
||||
) -> Decision {
|
||||
) -> Answer {
|
||||
let (reply, answer) = bounded(1);
|
||||
if self
|
||||
.inbox
|
||||
@@ -209,11 +240,11 @@ impl Approver {
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return Decision::Deny;
|
||||
return Answer::denied();
|
||||
}
|
||||
match tokio::time::timeout(within, answer.recv()).await {
|
||||
Ok(Ok(decision)) => decision,
|
||||
_ => Decision::Deny,
|
||||
Ok(Ok(answer)) => answer,
|
||||
_ => Answer::denied(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,7 +317,7 @@ mod tests {
|
||||
question(Request::Pair { requested: None }),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(decision, Decision::Deny);
|
||||
assert_eq!(decision, Answer::denied());
|
||||
holding.abort();
|
||||
}
|
||||
|
||||
@@ -297,7 +328,7 @@ mod tests {
|
||||
let drain = tokio::spawn(async move { while questions.recv().await.is_ok() {} });
|
||||
assert_eq!(
|
||||
approver.ask(question(Request::Pair { requested: None })).await,
|
||||
Decision::Deny
|
||||
Answer::denied()
|
||||
);
|
||||
drain.abort();
|
||||
}
|
||||
@@ -327,8 +358,8 @@ mod tests {
|
||||
approver.ask_within(deadline, backstop, question(Request::Pair { requested: None })),
|
||||
approver.ask_within(deadline, backstop, question(Request::Pair { requested: None })),
|
||||
);
|
||||
assert_eq!(first, Decision::Deny);
|
||||
assert_eq!(second, Decision::Deny);
|
||||
assert_eq!(first, Answer::denied());
|
||||
assert_eq!(second, Answer::denied());
|
||||
|
||||
let mut deadlines = Vec::new();
|
||||
while let Ok(prompt) = prompts.try_recv() {
|
||||
|
||||
+19
-12
@@ -507,12 +507,13 @@ pub async fn pair_client(
|
||||
changes: &async_channel::Sender<()>,
|
||||
pairing: &ClientPairing,
|
||||
identity: PublicKey,
|
||||
label: Option<&str>,
|
||||
) -> Result<()> {
|
||||
let was_approved = registry.lock().unwrap().contains(&pairing.client);
|
||||
registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&pairing.client_info(), identity)?;
|
||||
.approve(&pairing.client_info(), identity, label)?;
|
||||
|
||||
if let Err(error) = signer.complete_pairing(pairing).await {
|
||||
rollback_pairing(registry, &pairing.client, was_approved);
|
||||
@@ -678,7 +679,8 @@ async fn handle_request(event: &Event, config: &SignerConfig) -> Result<Option<E
|
||||
identity: signing_key(config, identity),
|
||||
rebind_from,
|
||||
};
|
||||
let outcome = match config.approver.ask(question).await {
|
||||
let answer = config.approver.ask(question).await;
|
||||
let outcome = match answer.decision {
|
||||
Decision::Deny => {
|
||||
record(
|
||||
config,
|
||||
@@ -695,7 +697,7 @@ async fn handle_request(event: &Event, config: &SignerConfig) -> Result<Option<E
|
||||
.registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&client, identity)
|
||||
.approve(&client, identity, answer.name.as_deref())
|
||||
.map(|()| "ack".to_string())
|
||||
.map_err(|error| {
|
||||
format!("could not record the approval: {error}")
|
||||
@@ -908,7 +910,8 @@ async fn gate(
|
||||
identity: signing_key(config, identity),
|
||||
rebind_from: None,
|
||||
};
|
||||
let decision = config.approver.ask(question).await;
|
||||
let answer = config.approver.ask(question).await;
|
||||
let decision = answer.decision;
|
||||
|
||||
// The user may have revoked the app while the dialog was open. Re-check
|
||||
// before acting, otherwise a revoked client still gets its answer.
|
||||
@@ -1351,7 +1354,7 @@ mod tests {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(decision).await;
|
||||
let _ = reply.send(decision.into()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let _ = reply.send(false).await;
|
||||
@@ -1382,7 +1385,7 @@ mod tests {
|
||||
Prompt::Approve { question, reply, .. } => {
|
||||
let decision = decide(&question.request);
|
||||
log.lock().unwrap().push(question.request.clone());
|
||||
let _ = reply.send(decision).await;
|
||||
let _ = reply.send(decision.into()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let _ = reply.send(false).await;
|
||||
@@ -1760,7 +1763,7 @@ mod tests {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(Decision::Once).await;
|
||||
let _ = reply.send(Decision::Once.into()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let opened = unlocking.unlock("hunter2").await.is_ok();
|
||||
@@ -1782,7 +1785,7 @@ mod tests {
|
||||
.registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&bare_client(&client.public_key()), identity)
|
||||
.approve(&bare_client(&client.public_key()), identity, None)
|
||||
.unwrap();
|
||||
|
||||
assert!(store.is_locked());
|
||||
@@ -1816,7 +1819,7 @@ mod tests {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(Decision::Once).await;
|
||||
let _ = reply.send(Decision::Once.into()).await;
|
||||
}
|
||||
// The user closes the unlock dialog without opening it.
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
@@ -1838,7 +1841,7 @@ mod tests {
|
||||
.registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&bare_client(&client.public_key()), identity)
|
||||
.approve(&bare_client(&client.public_key()), identity, None)
|
||||
.unwrap();
|
||||
// A stored "allow always": without the vault gate this would be answered
|
||||
// silently, which is exactly what must not happen while locked.
|
||||
@@ -2204,7 +2207,7 @@ mod tests {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(Decision::Once).await;
|
||||
let _ = reply.send(Decision::Once.into()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let _ = reply.send(false).await;
|
||||
@@ -2248,7 +2251,7 @@ mod tests {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(Decision::Always).await;
|
||||
let _ = reply.send(Decision::Always.into()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let _ = reply.send(false).await;
|
||||
@@ -2496,6 +2499,7 @@ mod tests {
|
||||
image: None,
|
||||
},
|
||||
keys.public_key(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
if let Some(kind) = kind {
|
||||
@@ -2555,6 +2559,7 @@ mod tests {
|
||||
image: None,
|
||||
},
|
||||
expected,
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -2661,6 +2666,7 @@ mod tests {
|
||||
image: None,
|
||||
},
|
||||
identity_b.public_key(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -2725,6 +2731,7 @@ mod tests {
|
||||
image: None,
|
||||
},
|
||||
missing.public_key(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
||||
+131
-11
@@ -47,6 +47,12 @@ pub struct App {
|
||||
/// Self-declared by the client; a hint, not a fact.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
/// A name the *user* gave it, for a client that declared none (or one the
|
||||
/// user liked less). Deliberately separate from `name`: `approve` rebuilds
|
||||
/// the entry on every pairing, so a label kept in `name` would be lost the
|
||||
/// next time a metadata-less client reconnects.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub label: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub url: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -63,6 +69,30 @@ pub struct App {
|
||||
pub policy: Policy,
|
||||
}
|
||||
|
||||
impl App {
|
||||
/// What to call the application: the user's own label first, then what the
|
||||
/// client declared. `None` means "unnamed" — the caller shortens the npub.
|
||||
pub fn display_name(&self) -> Option<&str> {
|
||||
self.label
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|label| !label.is_empty())
|
||||
.or_else(|| {
|
||||
self.name
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|name| !name.is_empty())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Trim a user- or client-supplied name and cap it, because it reaches
|
||||
/// `apps.json`, a dialog and the activity log.
|
||||
fn bound_label(value: &str) -> Option<String> {
|
||||
let value: String = value.trim().chars().take(64).collect();
|
||||
(!value.is_empty()).then_some(value)
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct AppRegistry {
|
||||
/// Where this registry is saved. Empty means "ephemeral", which is what
|
||||
@@ -153,6 +183,10 @@ impl AppRegistry {
|
||||
}
|
||||
|
||||
/// The display hints recorded for an approved application.
|
||||
///
|
||||
/// `name` is the *display* name — the user's own label first, then what the
|
||||
/// client declared — so the approval dialog and the activity log show what
|
||||
/// the user calls the app.
|
||||
pub fn client(&self, public_key: &PublicKey) -> Option<ClientInfo> {
|
||||
let hex = public_key.to_hex();
|
||||
self.apps
|
||||
@@ -160,7 +194,7 @@ impl AppRegistry {
|
||||
.find(|app| app.public_key == hex)
|
||||
.map(|app| ClientInfo {
|
||||
public_key: *public_key,
|
||||
name: app.name.clone(),
|
||||
name: app.display_name().map(str::to_string),
|
||||
url: app.url.clone(),
|
||||
image: app.image.clone(),
|
||||
})
|
||||
@@ -227,6 +261,19 @@ impl AppRegistry {
|
||||
self.save()
|
||||
}
|
||||
|
||||
/// Set or clear the user's own name for an application.
|
||||
///
|
||||
/// Trimmed and bounded; an empty value clears it, so the display falls back
|
||||
/// to whatever the client declared. Unknown applications are ignored.
|
||||
pub fn rename(&mut self, public_key: &PublicKey, label: Option<&str>) -> Result<()> {
|
||||
let hex = public_key.to_hex();
|
||||
let Some(app) = self.apps.iter_mut().find(|app| app.public_key == hex) else {
|
||||
return Ok(());
|
||||
};
|
||||
app.label = label.and_then(bound_label);
|
||||
self.save()
|
||||
}
|
||||
|
||||
/// The identity key a client is bound to, if it is approved.
|
||||
pub fn identity_for(&self, public_key: &PublicKey) -> Option<PublicKey> {
|
||||
let hex = public_key.to_hex();
|
||||
@@ -245,20 +292,30 @@ impl AppRegistry {
|
||||
/// B. (A client that reuses one keypair for two identities cannot be
|
||||
/// distinguished — requests carry no identity — so rebinding is the escape
|
||||
/// hatch.)
|
||||
pub fn approve(&mut self, client: &ClientInfo, identity: PublicKey) -> Result<()> {
|
||||
pub fn approve(
|
||||
&mut self,
|
||||
client: &ClientInfo,
|
||||
identity: PublicKey,
|
||||
label: Option<&str>,
|
||||
) -> Result<()> {
|
||||
let public_key = client.public_key.to_hex();
|
||||
let identity = identity.to_hex();
|
||||
let policy = self
|
||||
.apps
|
||||
.iter()
|
||||
.find(|app| app.public_key == public_key)
|
||||
let existing = self.apps.iter().find(|app| app.public_key == public_key);
|
||||
let policy = existing
|
||||
.filter(|app| app.identity == identity)
|
||||
.map(|app| app.policy.clone())
|
||||
.unwrap_or_default();
|
||||
// A label the pairing dialog did not collect keeps whatever the user set
|
||||
// before: re-pairing a metadata-less client must not wipe its name.
|
||||
let label = match label {
|
||||
Some(label) => bound_label(label),
|
||||
None => existing.and_then(|app| app.label.clone()),
|
||||
};
|
||||
self.apps.retain(|app| app.public_key != public_key);
|
||||
self.apps.push(App {
|
||||
public_key,
|
||||
name: client.name.clone(),
|
||||
label,
|
||||
url: client.url.clone(),
|
||||
image: client.image.clone(),
|
||||
approved_at: std::time::SystemTime::now()
|
||||
@@ -375,7 +432,7 @@ mod tests {
|
||||
|
||||
let mut registry = AppRegistry::at(path.clone());
|
||||
registry
|
||||
.approve(&client(keys.public_key()), keys.public_key())
|
||||
.approve(&client(keys.public_key()), keys.public_key(), None)
|
||||
.unwrap();
|
||||
registry.allow(&keys.public_key(), kind.clone()).unwrap();
|
||||
registry.allow(&keys.public_key(), method.clone()).unwrap();
|
||||
@@ -409,7 +466,7 @@ mod tests {
|
||||
let keys = Keys::generate();
|
||||
let mut registry = AppRegistry::default();
|
||||
registry
|
||||
.approve(&client(keys.public_key()), keys.public_key())
|
||||
.approve(&client(keys.public_key()), keys.public_key(), None)
|
||||
.unwrap();
|
||||
assert!(registry.contains(&keys.public_key()));
|
||||
}
|
||||
@@ -423,14 +480,14 @@ mod tests {
|
||||
let second = Keys::generate().public_key();
|
||||
let mut registry = AppRegistry::default();
|
||||
|
||||
registry.approve(&client(client_keys.public_key()), first).unwrap();
|
||||
registry.approve(&client(client_keys.public_key()), first, None).unwrap();
|
||||
registry
|
||||
.allow(&client_keys.public_key(), Permission::Kind(1))
|
||||
.unwrap();
|
||||
assert!(registry.permits(&client_keys.public_key(), &Permission::Kind(1)));
|
||||
assert_eq!(registry.identity_for(&client_keys.public_key()), Some(first));
|
||||
|
||||
registry.approve(&client(client_keys.public_key()), second).unwrap();
|
||||
registry.approve(&client(client_keys.public_key()), second, None).unwrap();
|
||||
assert_eq!(registry.identity_for(&client_keys.public_key()), Some(second));
|
||||
assert!(
|
||||
!registry.permits(&client_keys.public_key(), &Permission::Kind(1)),
|
||||
@@ -441,10 +498,72 @@ mod tests {
|
||||
registry
|
||||
.allow(&client_keys.public_key(), Permission::Kind(1))
|
||||
.unwrap();
|
||||
registry.approve(&client(client_keys.public_key()), second).unwrap();
|
||||
registry.approve(&client(client_keys.public_key()), second, None).unwrap();
|
||||
assert!(registry.permits(&client_keys.public_key(), &Permission::Kind(1)));
|
||||
}
|
||||
|
||||
/// A user-given name outranks what the client declared, survives a
|
||||
/// re-pairing, and can be cleared back to the declared name.
|
||||
#[test]
|
||||
fn a_user_label_wins_and_outlives_a_reapproval() {
|
||||
let keys = Keys::generate();
|
||||
let identity = Keys::generate().public_key();
|
||||
let mut registry = AppRegistry::default();
|
||||
registry
|
||||
.approve(&client(keys.public_key()), identity, None)
|
||||
.unwrap();
|
||||
assert_eq!(registry.list()[0].display_name(), Some("Test"));
|
||||
|
||||
registry
|
||||
.rename(&keys.public_key(), Some(" My app "))
|
||||
.unwrap();
|
||||
assert_eq!(registry.list()[0].display_name(), Some("My app"));
|
||||
assert_eq!(
|
||||
registry.client(&keys.public_key()).unwrap().name.as_deref(),
|
||||
Some("My app"),
|
||||
"the display name follows the label"
|
||||
);
|
||||
|
||||
// A re-pairing that collects no name keeps it.
|
||||
registry
|
||||
.approve(&client(keys.public_key()), identity, None)
|
||||
.unwrap();
|
||||
assert_eq!(registry.list()[0].display_name(), Some("My app"));
|
||||
|
||||
// A name the pairing dialog did collect replaces it.
|
||||
registry
|
||||
.approve(&client(keys.public_key()), identity, Some("Pairing name"))
|
||||
.unwrap();
|
||||
assert_eq!(registry.list()[0].display_name(), Some("Pairing name"));
|
||||
|
||||
// Clearing falls back to what the client declared; blank counts as clear.
|
||||
registry.rename(&keys.public_key(), None).unwrap();
|
||||
assert_eq!(registry.list()[0].display_name(), Some("Test"));
|
||||
registry.rename(&keys.public_key(), Some(" ")).unwrap();
|
||||
assert_eq!(registry.list()[0].display_name(), Some("Test"));
|
||||
}
|
||||
|
||||
/// A metadata-less client has no display name until the user gives it one.
|
||||
#[test]
|
||||
fn a_label_names_an_otherwise_unnamed_client() {
|
||||
let keys = Keys::generate();
|
||||
let unnamed = ClientInfo {
|
||||
public_key: keys.public_key(),
|
||||
name: None,
|
||||
url: None,
|
||||
image: None,
|
||||
};
|
||||
let mut registry = AppRegistry::default();
|
||||
registry
|
||||
.approve(&unnamed, keys.public_key(), Some("Work signer"))
|
||||
.unwrap();
|
||||
assert_eq!(registry.list()[0].display_name(), Some("Work signer"));
|
||||
assert_eq!(
|
||||
registry.client(&keys.public_key()).unwrap().name.as_deref(),
|
||||
Some("Work signer")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_apps_are_bound_once() {
|
||||
let identity = Keys::generate().public_key();
|
||||
@@ -453,6 +572,7 @@ mod tests {
|
||||
registry.apps.push(App {
|
||||
public_key: Keys::generate().public_key().to_hex(),
|
||||
name: None,
|
||||
label: None,
|
||||
url: None,
|
||||
image: None,
|
||||
approved_at: 0,
|
||||
|
||||
+57
-9
@@ -21,7 +21,7 @@ use libadwaita as adw;
|
||||
|
||||
use crate::runtime;
|
||||
use crate::runner::SignerRunner;
|
||||
use crate::signer::approval::{ClientInfo, Decision, Notice, Prompt, Question, Request};
|
||||
use crate::signer::approval::{Answer, ClientInfo, Decision, Notice, Prompt, Question, Request};
|
||||
use crate::state::AppState;
|
||||
use crate::store::keys::KeyStore;
|
||||
use crate::ui::{content, kind, plain};
|
||||
@@ -65,7 +65,7 @@ pub fn install(
|
||||
// has already passed, answer without raising a notification
|
||||
// or flashing a dialog for a request that is already gone.
|
||||
if expired(deadline) {
|
||||
let _ = reply.send(Decision::Deny).await;
|
||||
let _ = reply.send(Answer::denied()).await;
|
||||
continue;
|
||||
}
|
||||
// Notify while the window is still hidden — the user is away
|
||||
@@ -73,13 +73,13 @@ pub fn install(
|
||||
let notified = notify_pending(&widget, notification_body(&question.request));
|
||||
primary.present_for_prompt();
|
||||
runner.set_pending(true);
|
||||
let decision =
|
||||
let answer =
|
||||
present_question(&widget, &question, Some(deadline), &state).await;
|
||||
runner.set_pending(false);
|
||||
if notified {
|
||||
withdraw_pending(&widget);
|
||||
}
|
||||
let _ = reply.send(decision).await;
|
||||
let _ = reply.send(answer).await;
|
||||
}
|
||||
Prompt::Unlock { reply, deadline } => {
|
||||
if expired(deadline) {
|
||||
@@ -264,6 +264,39 @@ enum Choices {
|
||||
OnceAlways,
|
||||
}
|
||||
|
||||
/// A name field for a pairing, and **only** when the client declared no name —
|
||||
/// an application that named itself does not need the user to.
|
||||
fn name_entry(question: &Question) -> Option<adw::EntryRow> {
|
||||
let paired = matches!(question.request, Request::Pair { .. });
|
||||
let unnamed = question
|
||||
.client
|
||||
.name
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.map_or(true, str::is_empty);
|
||||
(paired && unnamed).then(|| adw::EntryRow::builder().title("Name").build())
|
||||
}
|
||||
|
||||
/// The name field's group: a line saying why it is being asked, then the entry.
|
||||
fn name_field(entry: &adw::EntryRow) -> gtk::Box {
|
||||
let container = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(6)
|
||||
.build();
|
||||
let note = plain::label(
|
||||
"This application did not send a name. Give it one so you can recognise it later.",
|
||||
);
|
||||
note.add_css_class("dim-label");
|
||||
container.append(¬e);
|
||||
let list = gtk::ListBox::builder()
|
||||
.selection_mode(gtk::SelectionMode::None)
|
||||
.css_classes(["boxed-list"])
|
||||
.build();
|
||||
list.append(entry);
|
||||
container.append(&list);
|
||||
container
|
||||
}
|
||||
|
||||
/// Show one approval question and return the user's answer.
|
||||
///
|
||||
/// Used by the responder's drain ([`install`]) and by user-initiated flows such
|
||||
@@ -274,7 +307,7 @@ pub async fn present_question(
|
||||
question: &Question,
|
||||
deadline: Option<Instant>,
|
||||
state: &Rc<AppState>,
|
||||
) -> Decision {
|
||||
) -> Answer {
|
||||
let request = &question.request;
|
||||
let choices = match request {
|
||||
Request::Pair { .. } => Choices::Pair,
|
||||
@@ -282,10 +315,14 @@ pub async fn present_question(
|
||||
_ => Choices::OnceAlways,
|
||||
};
|
||||
|
||||
// A client that declared no name gets a name field: naming an otherwise
|
||||
// nameless npub belongs with the decision to trust it.
|
||||
let name_entry = name_entry(question);
|
||||
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(heading(request))
|
||||
.body(story(request))
|
||||
.extra_child(&details(question, widget, deadline, state))
|
||||
.extra_child(&details(question, widget, deadline, state, name_entry.as_ref()))
|
||||
.build();
|
||||
// Heading and body are plain text too: the heading carries no client data
|
||||
// today, but the extra child is where all of it belongs.
|
||||
@@ -317,20 +354,25 @@ pub async fn present_question(
|
||||
let (sender, answer) = async_channel::bounded(1);
|
||||
{
|
||||
let sender = sender.clone();
|
||||
let name_entry = name_entry.clone();
|
||||
dialog.connect_response(None, move |_, response| {
|
||||
let decision = match response {
|
||||
"always" => Decision::Always,
|
||||
"once" | "allow" => Decision::Once,
|
||||
_ => Decision::Deny,
|
||||
};
|
||||
let _ = sender.try_send(decision);
|
||||
let name = name_entry
|
||||
.as_ref()
|
||||
.map(|entry| entry.text().trim().to_string())
|
||||
.filter(|name| !name.is_empty());
|
||||
let _ = sender.try_send(Answer { decision, name });
|
||||
});
|
||||
}
|
||||
dialog.present(window_of(widget).as_ref());
|
||||
expire(&dialog, &sender, Decision::Deny, deadline);
|
||||
expire(&dialog, &sender, Answer::denied(), deadline);
|
||||
|
||||
// Dropped without an answer (window closed, app gone) is a denial.
|
||||
answer.recv().await.unwrap_or(Decision::Deny)
|
||||
answer.recv().await.unwrap_or_else(|_| Answer::denied())
|
||||
}
|
||||
|
||||
/// Ask for the vault passphrase and open it.
|
||||
@@ -474,6 +516,7 @@ fn details(
|
||||
widget: &impl IsA<gtk::Widget>,
|
||||
deadline: Option<Instant>,
|
||||
state: &Rc<AppState>,
|
||||
name_entry: Option<&adw::EntryRow>,
|
||||
) -> gtk::Box {
|
||||
let client = &question.client;
|
||||
let content = gtk::Box::builder()
|
||||
@@ -483,6 +526,11 @@ fn details(
|
||||
|
||||
content.append(&identity_field(state, &question.identity));
|
||||
content.append(&application_field(state, client));
|
||||
// A client that declared no name: naming it sits right under "Application",
|
||||
// above the payload and the details, because it is part of the decision.
|
||||
if let Some(entry) = name_entry {
|
||||
content.append(&name_field(entry));
|
||||
}
|
||||
// The kind is part of what is being signed, so it sits with the payload
|
||||
// rather than collapsed under Details.
|
||||
if let Request::Sign { kind, .. } = &question.request {
|
||||
|
||||
+95
-10
@@ -210,7 +210,7 @@ impl Ui {
|
||||
.map(|app| {
|
||||
(
|
||||
app.public_key.clone(),
|
||||
app.name.clone(),
|
||||
app.display_name().map(str::to_string),
|
||||
app.url.clone(),
|
||||
self.state.icons().texture(&app.public_key).is_some(),
|
||||
app.policy.clone(),
|
||||
@@ -262,8 +262,9 @@ impl Ui {
|
||||
.unwrap_or_else(|_| app.public_key.clone());
|
||||
// A client that declares no usable name is labelled by its key rather than
|
||||
// a generic word, and the avatar shows libadwaita's glyph instead of
|
||||
// initials of a placeholder.
|
||||
let name = app.name.clone().filter(|name| !name.trim().is_empty());
|
||||
// initials of a placeholder. The user's own label wins over what the
|
||||
// client declared.
|
||||
let name = app.display_name().map(str::to_string);
|
||||
let title = name
|
||||
.clone()
|
||||
.unwrap_or_else(|| plain::shorten_npub(&npub));
|
||||
@@ -296,6 +297,22 @@ impl Ui {
|
||||
if let Some(url) = url {
|
||||
row.add_row(&plain::row("Declared URL", url));
|
||||
}
|
||||
// What the client claimed, kept visible when the user gave it their own
|
||||
// name: the label must not hide the application's own words.
|
||||
if let (Some(label), Some(declared)) = (
|
||||
app.label
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|label| !label.is_empty()),
|
||||
app.name
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|name| !name.is_empty()),
|
||||
) {
|
||||
if label != declared {
|
||||
row.add_row(&plain::row("Declared name", declared));
|
||||
}
|
||||
}
|
||||
|
||||
// Which identity this app signs as. This is the binding the responder
|
||||
// resolves per request.
|
||||
@@ -355,13 +372,26 @@ impl Ui {
|
||||
}
|
||||
}
|
||||
|
||||
// Revoking the whole application stays visible while collapsed.
|
||||
// A per-app menu: name it, or revoke it. The menu is the row's one
|
||||
// suffix, like an identity row; revoking is no longer a button of its
|
||||
// own.
|
||||
let menu = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(2)
|
||||
.build();
|
||||
let popover = gtk::Popover::new();
|
||||
popover.set_child(Some(&menu));
|
||||
|
||||
let rename = plain::menu_button("Rename…");
|
||||
{
|
||||
let ui = Rc::clone(self);
|
||||
let app = app.clone();
|
||||
rename.connect_clicked(move |_| ui.present_rename(&app));
|
||||
}
|
||||
menu.append(&rename);
|
||||
|
||||
if let Ok(public_key) = PublicKey::parse(&app.public_key) {
|
||||
let revoke = gtk::Button::builder()
|
||||
.label("Revoke")
|
||||
.valign(gtk::Align::Center)
|
||||
.css_classes(["destructive-action"])
|
||||
.build();
|
||||
let revoke = plain::menu_button("Revoke");
|
||||
let ui = Rc::clone(self);
|
||||
let key = app.public_key.clone();
|
||||
revoke.connect_clicked(move |_| {
|
||||
@@ -372,12 +402,67 @@ impl Ui {
|
||||
ui.state.icons().forget(&key);
|
||||
ui.state.notify_changes();
|
||||
});
|
||||
row.add_suffix(&revoke);
|
||||
menu.append(&revoke);
|
||||
}
|
||||
|
||||
// A plain popover does not close when a button inside it is clicked —
|
||||
// only menu items do — so close it after any action.
|
||||
let mut child = menu.first_child();
|
||||
while let Some(widget) = child {
|
||||
if let Some(button) = widget.downcast_ref::<gtk::Button>() {
|
||||
let popover = popover.clone();
|
||||
button.connect_clicked(move |_| popover.popdown());
|
||||
}
|
||||
child = widget.next_sibling();
|
||||
}
|
||||
|
||||
let button = gtk::MenuButton::builder()
|
||||
.icon_name("view-more-symbolic")
|
||||
.popover(&popover)
|
||||
.valign(gtk::Align::Center)
|
||||
.css_classes(["flat"])
|
||||
.build();
|
||||
row.add_suffix(&button);
|
||||
|
||||
row
|
||||
}
|
||||
|
||||
/// Name an application the user can recognise — for a client that declared
|
||||
/// no name, or one the user wants to call something else.
|
||||
fn present_rename(self: &Rc<Self>, app: &App) {
|
||||
let entry = gtk::Entry::builder()
|
||||
.text(app.display_name().unwrap_or_default())
|
||||
.build();
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading("Name application")
|
||||
.body("Only you see this name; what the application declared is not changed.")
|
||||
.extra_child(&entry)
|
||||
.build();
|
||||
plain::dialog(&dialog);
|
||||
dialog.add_response("cancel", "Cancel");
|
||||
dialog.add_response("rename", "Save");
|
||||
dialog.set_default_response(Some("rename"));
|
||||
dialog.set_response_appearance("rename", adw::ResponseAppearance::Suggested);
|
||||
dialog.set_close_response("cancel");
|
||||
|
||||
let public_key = app.public_key.clone();
|
||||
let ui = Rc::clone(self);
|
||||
dialog.connect_response(None, move |_, response| {
|
||||
if response != "rename" {
|
||||
return;
|
||||
}
|
||||
let Ok(key) = PublicKey::parse(&public_key) else {
|
||||
return;
|
||||
};
|
||||
let name = entry.text().to_string();
|
||||
if let Err(error) = ui.state.registry().lock().unwrap().rename(&key, Some(&name)) {
|
||||
eprintln!("regent: could not rename an application: {error}");
|
||||
}
|
||||
ui.state.notify_changes();
|
||||
});
|
||||
dialog.present(self.body.root().and_downcast::<gtk::Window>().as_ref());
|
||||
}
|
||||
|
||||
fn permission_row(
|
||||
self: &Rc<Self>,
|
||||
app: &App,
|
||||
|
||||
+17
-5
@@ -402,14 +402,16 @@ impl ConnectDialog {
|
||||
// The same approval dialog as a responder question: the app is
|
||||
// registered *before* the response goes out, and the user is the
|
||||
// one who decides.
|
||||
let decision =
|
||||
let answer =
|
||||
approval::present_question(&parent, &question, None, &connect.state).await;
|
||||
if decision == Decision::Deny {
|
||||
if answer.decision == Decision::Deny {
|
||||
return;
|
||||
}
|
||||
// The name the user gave a client that declared none, if any.
|
||||
let name = answer.name;
|
||||
|
||||
match connect.runner.status() {
|
||||
Status::Running(signer) => connect.pair_now(&signer, pairing, identity),
|
||||
Status::Running(signer) => connect.pair_now(&signer, pairing, identity, name),
|
||||
Status::Starting => connect.report_error(
|
||||
"The signer is still starting. Try again in a moment.",
|
||||
),
|
||||
@@ -423,7 +425,7 @@ impl ConnectDialog {
|
||||
connect.runner.start(
|
||||
extra,
|
||||
Some(Box::new(move |result| match result {
|
||||
Ok(signer) => after.pair_now(&signer, pairing, identity),
|
||||
Ok(signer) => after.pair_now(&signer, pairing, identity, name),
|
||||
Err(error) => after.report_error(&error),
|
||||
})),
|
||||
);
|
||||
@@ -437,6 +439,7 @@ impl ConnectDialog {
|
||||
signer: &Arc<Signer>,
|
||||
pairing: ClientPairing,
|
||||
identity: PublicKey,
|
||||
label: Option<String>,
|
||||
) {
|
||||
let registry = self.state.registry();
|
||||
let activity = self.state.activity();
|
||||
@@ -447,7 +450,16 @@ impl ConnectDialog {
|
||||
runtime::spawn(
|
||||
async move {
|
||||
signer.add_client_relays(&pairing.relays).await?;
|
||||
pair_client(&signer, ®istry, &activity, &changes, &pairing, identity).await?;
|
||||
pair_client(
|
||||
&signer,
|
||||
®istry,
|
||||
&activity,
|
||||
&changes,
|
||||
&pairing,
|
||||
identity,
|
||||
label.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
Ok::<(), crate::signer::Error>(())
|
||||
},
|
||||
move |result| match result {
|
||||
|
||||
+9
-13
@@ -196,7 +196,7 @@ fn key_row(
|
||||
popover.set_child(Some(&menu));
|
||||
|
||||
if let Some(public_key) = public_key {
|
||||
let rename = menu_button("Rename…");
|
||||
let rename = plain::menu_button("Rename…");
|
||||
{
|
||||
let (handles, state) = (handles.clone(), Rc::clone(state));
|
||||
let name = name.to_string();
|
||||
@@ -206,7 +206,7 @@ fn key_row(
|
||||
}
|
||||
menu.append(&rename);
|
||||
|
||||
let copy = menu_button("Copy public key");
|
||||
let copy = plain::menu_button("Copy public key");
|
||||
{
|
||||
let handles = handles.clone();
|
||||
let npub = npub.clone();
|
||||
@@ -221,14 +221,14 @@ fn key_row(
|
||||
}
|
||||
menu.append(©);
|
||||
|
||||
let export = menu_button("Export…");
|
||||
let export = plain::menu_button("Export…");
|
||||
{
|
||||
let (handles, state) = (handles.clone(), Rc::clone(state));
|
||||
export.connect_clicked(move |_| present_export(handles.clone(), Rc::clone(&state), public_key));
|
||||
}
|
||||
menu.append(&export);
|
||||
|
||||
let remove = menu_button("Remove…");
|
||||
let remove = plain::menu_button("Remove…");
|
||||
{
|
||||
let (handles, state) = (handles.clone(), Rc::clone(state));
|
||||
let name = name.to_string();
|
||||
@@ -260,14 +260,6 @@ fn key_row(
|
||||
row
|
||||
}
|
||||
|
||||
fn menu_button(label: &str) -> gtk::Button {
|
||||
gtk::Button::builder()
|
||||
.label(label)
|
||||
.halign(gtk::Align::Start)
|
||||
.css_classes(["flat"])
|
||||
.build()
|
||||
}
|
||||
|
||||
/// A passphrase field for the add/import dialogs, plus a confirmation when this
|
||||
/// is the call that *sets* the vault passphrase.
|
||||
struct PassphraseFields {
|
||||
@@ -600,7 +592,11 @@ fn present_remove(
|
||||
} else {
|
||||
let apps: Vec<String> = paired
|
||||
.iter()
|
||||
.map(|app| app.name.clone().unwrap_or_else(|| "an unnamed application".to_string()))
|
||||
.map(|app| {
|
||||
app.display_name()
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| "an unnamed application".to_string())
|
||||
})
|
||||
.collect();
|
||||
format!(
|
||||
"Remove “{name}”? These applications are paired with it and will be revoked: {}.",
|
||||
|
||||
@@ -150,6 +150,15 @@ pub fn set_avatar(avatar: &adw::Avatar, name: Option<&str>, texture: Option<>k
|
||||
avatar.set_custom_image(texture);
|
||||
}
|
||||
|
||||
/// A flat, start-aligned button for a `GtkPopover` menu (identities, apps).
|
||||
pub fn menu_button(label: &str) -> gtk::Button {
|
||||
gtk::Button::builder()
|
||||
.label(label)
|
||||
.halign(gtk::Align::Start)
|
||||
.css_classes(["flat"])
|
||||
.build()
|
||||
}
|
||||
|
||||
/// An `ExpanderRow` whose title and subtitle are plain text.
|
||||
///
|
||||
/// The enable switch is hidden: on an expander it only hides the child rows,
|
||||
|
||||
Reference in new issue
Block a user