One read of a device's link: state::credentials, with the client's seal

Five places read the server address and token straight from sync_state:
sharing, autosync, sync_unlink, update's download token, and the ffi. Reading it
raw is how Android came to send its sealed token to the share routes (#5381).
state::credentials(conn, seal) now holds that read. With a seal it opens the token
(open_token), and without one (the desktop keeps it plain) it returns it as stored.
Every site calls it.

DRY pass #2, batch 1, F1 (#5372).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 14:14:18 -04:00
co-authored by Claude Opus 5.5
parent 17ae8c0863
commit 70274347af
6 changed files with 37 additions and 32 deletions
+8 -13
View File
@@ -6,10 +6,10 @@
//! the server's answer so the card's chip changes at once rather than at the next
//! sync (which brings the same value).
//!
//! The server address and token come from the CALLER, never from the store here.
//! A stored token may be sealed (Android keeps it under a Keystore key, see
//! `state::TokenSeal`), and only the caller holds the seal that opens it. Reading it
//! here sent `sealed:…` as the bearer token, and every share call was refused (#5381).
//! The server address and token come from the CALLER, read with
//! `state::credentials` and the caller's seal. A stored token may be sealed
//! (Android keeps it under a Keystore key), and reading it raw here sent `sealed:…`
//! as the bearer token, so every share call was refused (#5381).
use rusqlite::params;
@@ -22,17 +22,12 @@ use crate::local::Db;
pub const NEEDS_SERVER: &str =
"Sharing is between people on a server. Link this device to one in Sync to share notes.";
/// The server address and token AS STORED, or [`NEEDS_SERVER`].
///
/// Only for a client that stores its token plain, as the desktop does. A client
/// with a seal opens the token itself (`state::open_token`) and passes that.
/// The link to share over, or [`NEEDS_SERVER`]. For a client that keeps its token
/// plain, as the desktop does; one with a seal reads `state::credentials` itself.
pub fn stored_link(db: &Db) -> Result<(String, String), String> {
let conn = db.conn()?;
let link = state::read(&conn).map_err(|e| e.to_string())?;
match (link.server_url, link.device_token) {
(Some(url), Some(token)) => Ok((url, token)),
_ => Err(NEEDS_SERVER.to_string()),
}
let link = state::credentials(&conn, None).map_err(|e| e.to_string())?;
link.ok_or_else(|| NEEDS_SERVER.to_string())
}
/// Set the local note's `shared` flag from the server's list of its shares. Not a