aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/federation/party.rs84
1 files changed, 74 insertions, 10 deletions
diff --git a/src/federation/party.rs b/src/federation/party.rs
index 8f69e49..72c18e9 100644
--- a/src/federation/party.rs
+++ b/src/federation/party.rs
@@ -1,8 +1,9 @@
//! `continuum-party`: a party's client of a federation (`docs/federation.md`). It signs its
//! party's events with one of its keys (`--key`: an OpenSSH private key file, or a public
//! key the ssh-agent at `SSH_AUTH_SOCK` holds, given itself or in a file), puts them
-//! through a node's API (`--node`, its user and password in the URL, or `--token`), and
-//! keeps its party's record (`--record`, `client::Record`), by which it votes, written
+//! through a node's API (`--node`, its user and password in the URL, or `--token`; with
+//! neither, as its party, logging in there by that key: `Node::log_in`), and keeps its
+//! party's record (`--record`, `client::Record`), by which it votes, written
//! before a vote is sent. Its options may be set in configuration files as the node's are
//! (`--config`, `config::PARTY_OPTIONS`): the same format, imports and all.
//!
@@ -616,7 +617,23 @@ pub(crate) async fn main() -> Result<()> {
print!("{}", judge::judge(&histories));
return Ok(());
}
- let node = Node::new(&options)?;
+ let mut node = Node::new(&options)?;
+ // Without a user of the node or a token: as the party, by its key, where the record
+ // (or `init`) names the party.
+ if node.password.is_none() && node.token.is_none() && options.key.is_some() {
+ let party = match (name, &options.record) {
+ ("init", _) => sub.get_one::<String>("party").cloned(),
+ (_, Some(path)) if Path::new(path).exists() => Some(
+ Record::load(Path::new(path))
+ .map_err(|why| anyhow!(why))?
+ .party,
+ ),
+ _ => None,
+ };
+ if let Some(party) = party {
+ node.log_in(&party, &signing_key(&options)?).await?;
+ }
+ }
if name == "propose" && proposing_flags(sub).get_flag("print") {
let current = node.current().await?;
let version = to_propose(&node, &current, sub).await?;
@@ -2161,18 +2178,65 @@ impl Node {
})
}
- async fn answer(response: reqwest::Response) -> Result<reqwest::Response> {
+ async fn answer(&self, response: reqwest::Response) -> Result<reqwest::Response> {
let status = response.status();
if status.is_success() {
return Ok(response);
}
let body = response.text().await.unwrap_or_default();
+ if status == reqwest::StatusCode::UNAUTHORIZED
+ && self.password.is_none()
+ && self.token.is_none()
+ {
+ bail!(
+ "the node answers {status}: {body}. Name a user of it in --node, give --token, \
+ or --key and --record to log in as the party"
+ );
+ }
bail!("the node answers {status}: {body}")
}
+ /// Logs in as `party` by its key `key` (`POST /auth/login/party`): a party's session,
+ /// which does federation work alone, its token going with every request then.
+ async fn log_in(&mut self, party: &str, key: &Signing) -> Result<()> {
+ let namespace = crate::access::challenge::NAMESPACE;
+ let asked = async {
+ let response = self
+ .request(reqwest::Method::POST, "/auth/challenge")?
+ .send()
+ .await?;
+ let challenge: JsonValue = self.answer(response).await?.json().await?;
+ let challenge = challenge["challenge"]
+ .as_str()
+ .context("the node gives no challenge")?
+ .to_owned();
+ let signature = match key {
+ Signing::File(key) => keys::sign_in(namespace, key, challenge.as_bytes()),
+ Signing::Agent(key, socket) => {
+ keys::sign_through_in(namespace, socket, key, challenge.as_bytes())
+ .map_err(|why| anyhow!(why))?
+ }
+ };
+ let body = serde_json::json!({
+ "party": party,
+ "challenge": challenge,
+ "signature": signature,
+ });
+ self.post("/auth/login/party", &body).await
+ };
+ let answer = asked.await.with_context(|| {
+ format!("Logging in as {party} by its key, with no user of the node")
+ })?;
+ let token = answer["token"]
+ .as_str()
+ .context("the node gives no token")?;
+ self.token = Some(token.to_owned());
+ Ok(())
+ }
+
async fn get(&self, path: &str) -> Result<JsonValue> {
let response = self.request(reqwest::Method::GET, path)?.send().await?;
- Ok(Self::answer(response).await?.json().await?)
+ Ok(self.answer(response).await?.json().await?)
}
/// The store's ID, and its heads.
@@ -2217,7 +2281,7 @@ impl Node {
.request(reqwest::Method::GET, &format!("/events/{id}"))?
.send()
.await?;
- let bytes = Self::answer(response).await?.bytes().await?.to_vec();
+ let bytes = self.answer(response).await?.bytes().await?.to_vec();
if Id::of(&bytes) != *id {
bail!("the node's event {id} isn't of that ID");
}
@@ -2230,7 +2294,7 @@ impl Node {
.json(body)
.send()
.await?;
- Ok(Self::answer(response).await?.json().await?)
+ Ok(self.answer(response).await?.json().await?)
}
/// What the node answers to `body`, as bytes.
@@ -2240,7 +2304,7 @@ impl Node {
.json(body)
.send()
.await?;
- Ok(Self::answer(response).await?.bytes().await?.to_vec())
+ Ok(self.answer(response).await?.bytes().await?.to_vec())
}
/// What the node answers to the bytes `body`.
@@ -2251,7 +2315,7 @@ impl Node {
.body(body)
.send()
.await?;
- Ok(Self::answer(response).await?.json().await?)
+ Ok(self.answer(response).await?.json().await?)
}
async fn put(&self, event: &[u8]) -> Result<()> {
@@ -2261,7 +2325,7 @@ impl Node {
.body(event.to_vec())
.send()
.await?;
- let answer: JsonValue = Self::answer(response).await?.json().await?;
+ let answer: JsonValue = self.answer(response).await?.json().await?;
println!(
"{} {}",
answer["id"].as_str().unwrap_or_default(),