mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-29 08:21:47 +00:00
minion: move authenticate() to it's own function
This commit is contained in:
parent
2b96ddf27a
commit
406dd37cb2
@ -1,5 +1,6 @@
|
||||
use crate::comms::{ToMinionMessage, ToOverlordMessage};
|
||||
use crate::people::PersonList;
|
||||
use nostr_types::RelayUrl;
|
||||
|
||||
/// Error kinds that can occur in gossip-lib
|
||||
#[derive(Debug)]
|
||||
@ -20,6 +21,7 @@ pub enum ErrorKind {
|
||||
Nostr(nostr_types::Error),
|
||||
NoPublicKey,
|
||||
NoPrivateKey,
|
||||
NoPrivateKeyForAuth(RelayUrl),
|
||||
NoRelay,
|
||||
NotAPersonListEvent,
|
||||
NoSlotsRemaining,
|
||||
@ -96,6 +98,9 @@ impl std::fmt::Display for Error {
|
||||
Nostr(e) => write!(f, "Nostr: {e}"),
|
||||
NoPublicKey => write!(f, "No public key identity available."),
|
||||
NoPrivateKey => write!(f, "No private key available."),
|
||||
NoPrivateKeyForAuth(u) => {
|
||||
write!(f, "No private key available, cannot AUTH to relay: {}", u)
|
||||
}
|
||||
NoRelay => write!(f, "Could not determine a relay to use."),
|
||||
NotAPersonListEvent => write!(f, "Not a person list event"),
|
||||
NoSlotsRemaining => write!(f, "No custom list slots remaining."),
|
||||
|
@ -2,9 +2,7 @@ use super::Minion;
|
||||
use crate::comms::ToOverlordMessage;
|
||||
use crate::error::Error;
|
||||
use crate::globals::GLOBALS;
|
||||
use futures_util::sink::SinkExt;
|
||||
use nostr_types::{ClientMessage, EventKind, PreEvent, RelayMessage, Tag, Unixtime};
|
||||
use tungstenite::protocol::Message as WsMessage;
|
||||
use nostr_types::{RelayMessage, Unixtime};
|
||||
|
||||
impl Minion {
|
||||
pub(super) async fn handle_nostr_message(&mut self, ws_message: String) -> Result<(), Error> {
|
||||
@ -159,37 +157,7 @@ impl Minion {
|
||||
}
|
||||
}
|
||||
RelayMessage::Auth(challenge) => {
|
||||
if !GLOBALS.signer.is_ready() {
|
||||
tracing::warn!("AUTH required on {}, but we have no key", &self.url);
|
||||
return Ok(());
|
||||
}
|
||||
let pubkey = match GLOBALS.signer.public_key() {
|
||||
Some(pk) => pk,
|
||||
None => return Ok(()),
|
||||
};
|
||||
let pre_event = PreEvent {
|
||||
pubkey,
|
||||
created_at: Unixtime::now().unwrap(),
|
||||
kind: EventKind::Auth,
|
||||
tags: vec![
|
||||
Tag::Other {
|
||||
tag: "relay".to_string(),
|
||||
data: vec![self.url.as_str().to_owned()],
|
||||
},
|
||||
Tag::Other {
|
||||
tag: "challenge".to_string(),
|
||||
data: vec![challenge],
|
||||
},
|
||||
],
|
||||
content: "".to_string(),
|
||||
};
|
||||
let event = GLOBALS.signer.sign_preevent(pre_event, None, None)?;
|
||||
let msg = ClientMessage::Auth(Box::new(event));
|
||||
let wire = serde_json::to_string(&msg)?;
|
||||
self.last_message_sent = wire.clone();
|
||||
let ws_stream = self.stream.as_mut().unwrap();
|
||||
ws_stream.send(WsMessage::Text(wire)).await?;
|
||||
tracing::info!("Authenticated to {}", &self.url);
|
||||
let _ = self.authenticate(challenge).await?;
|
||||
}
|
||||
RelayMessage::Closed(subid, message) => {
|
||||
let handle = self
|
||||
|
@ -16,8 +16,8 @@ use http::uri::{Parts, Scheme};
|
||||
use http::Uri;
|
||||
use mime::Mime;
|
||||
use nostr_types::{
|
||||
ClientMessage, EventAddr, EventKind, Filter, Id, IdHex, PublicKey, PublicKeyHex,
|
||||
RelayInformationDocument, RelayUrl, Unixtime,
|
||||
ClientMessage, EventAddr, EventKind, Filter, Id, IdHex, PreEvent, PublicKey, PublicKeyHex,
|
||||
RelayInformationDocument, RelayUrl, Tag, Unixtime,
|
||||
};
|
||||
use reqwest::Response;
|
||||
use std::borrow::Cow;
|
||||
@ -955,6 +955,43 @@ impl Minion {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn authenticate(&mut self, challenge: String) -> Result<Id, Error> {
|
||||
if !GLOBALS.signer.is_ready() {
|
||||
return Err(ErrorKind::NoPrivateKeyForAuth(self.url.clone()).into());
|
||||
}
|
||||
let pubkey = match GLOBALS.signer.public_key() {
|
||||
Some(pk) => pk,
|
||||
None => {
|
||||
return Err(ErrorKind::NoPrivateKeyForAuth(self.url.clone()).into());
|
||||
}
|
||||
};
|
||||
let pre_event = PreEvent {
|
||||
pubkey,
|
||||
created_at: Unixtime::now().unwrap(),
|
||||
kind: EventKind::Auth,
|
||||
tags: vec![
|
||||
Tag::Other {
|
||||
tag: "relay".to_string(),
|
||||
data: vec![self.url.as_str().to_owned()],
|
||||
},
|
||||
Tag::Other {
|
||||
tag: "challenge".to_string(),
|
||||
data: vec![challenge],
|
||||
},
|
||||
],
|
||||
content: "".to_string(),
|
||||
};
|
||||
let event = GLOBALS.signer.sign_preevent(pre_event, None, None)?;
|
||||
let id = event.id;
|
||||
let msg = ClientMessage::Auth(Box::new(event));
|
||||
let wire = serde_json::to_string(&msg)?;
|
||||
self.last_message_sent = wire.clone();
|
||||
let ws_stream = self.stream.as_mut().unwrap();
|
||||
ws_stream.send(WsMessage::Text(wire)).await?;
|
||||
tracing::info!("Authenticated to {}", &self.url);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
// This replictes reqwest Response text_with_charset to handle decoding
|
||||
// whatever charset they used into UTF-8, as well as counting the bytes.
|
||||
async fn text_with_charset(
|
||||
|
Loading…
Reference in New Issue
Block a user