|
@@ -1,6 +1,7 @@
|
|
|
use std::str::FromStr;
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
+use aspect_proto::auth::AspAuthConfig;
|
|
|
use ecksport_rpc::client::RpcClient;
|
|
|
use ecksport_rpc::errors::ClientError;
|
|
|
use thiserror::Error;
|
|
@@ -10,7 +11,7 @@ use tracing::*;
|
|
|
use aspect_core::addrfmt;
|
|
|
use aspect_core::message_queue::MessageQueueId;
|
|
|
use aspect_db::state_prop::StatePropDatastore;
|
|
|
-use aspect_ident::{DeviceIdent, DirPackage};
|
|
|
+use aspect_ident::{DeviceIdent, DirPackage, IdentSigner};
|
|
|
use aspect_proto::identity::AnnResponse;
|
|
|
use aspect_proto::msg_queue::{MqListRequest, QueueViewState};
|
|
|
use aspect_proto::user::UserCreds;
|
|
@@ -47,18 +48,37 @@ pub enum Error {
|
|
|
Datastore(#[from] aspect_db::errors::Error),
|
|
|
}
|
|
|
|
|
|
+/// Used to indicate how we decide to authenticate with a server.
|
|
|
+#[derive(Clone)]
|
|
|
+pub enum PeerAuthConfig {
|
|
|
+ Anon,
|
|
|
+ Ident(Arc<IdentSigner>),
|
|
|
+}
|
|
|
+
|
|
|
+impl PeerAuthConfig {
|
|
|
+ /// Constructs an [``AspAuthConfig``] from the auth config.
|
|
|
+ pub fn to_ecks_auth_config(&self) -> AspAuthConfig {
|
|
|
+ match self {
|
|
|
+ Self::Anon => AspAuthConfig::new_unauth(),
|
|
|
+ Self::Ident(signer) => AspAuthConfig::new_auth(signer.clone()),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/// Manages a client's peers, in the context-dependent forms they might take.
|
|
|
pub struct PeerManager {
|
|
|
stateprop_ds: Arc<StatePropDatastore>,
|
|
|
creds: RwLock<Option<HomeserverCreds>>,
|
|
|
homeserver_client: RwLock<Option<Arc<HomeserverClient>>>,
|
|
|
signer_man: Arc<RwLock<SignerManager>>,
|
|
|
+ peer_auth_config: PeerAuthConfig,
|
|
|
}
|
|
|
|
|
|
impl PeerManager {
|
|
|
pub async fn new(
|
|
|
stateprop_ds: Arc<StatePropDatastore>,
|
|
|
signer_man: Arc<RwLock<SignerManager>>,
|
|
|
+ peer_auth_config: PeerAuthConfig,
|
|
|
) -> Result<Self, Error> {
|
|
|
let creds = stateprop_ds
|
|
|
.get_prop::<HomeserverCreds>(DSK_HOMESERVER_CREDS)
|
|
@@ -69,6 +89,7 @@ impl PeerManager {
|
|
|
creds: RwLock::new(creds),
|
|
|
homeserver_client: RwLock::new(None),
|
|
|
signer_man,
|
|
|
+ peer_auth_config,
|
|
|
})
|
|
|
}
|
|
|
|
|
@@ -117,6 +138,12 @@ impl PeerManager {
|
|
|
self.get_creds().await.ok_or(Error::NoHomeserverCreds)
|
|
|
}
|
|
|
|
|
|
+ /// Creates an instance of an auth config to use based on the peer auth
|
|
|
+ /// config.
|
|
|
+ pub fn create_ecks_auth_config(&self) -> AspAuthConfig {
|
|
|
+ self.peer_auth_config.to_ecks_auth_config()
|
|
|
+ }
|
|
|
+
|
|
|
/// Connects to a homeserver and registers an account on it.
|
|
|
pub async fn connect_and_register(
|
|
|
&self,
|
|
@@ -128,7 +155,8 @@ impl PeerManager {
|
|
|
info!(%homeserver, "connecting to homeserver...");
|
|
|
let addr = addrfmt::Address::from_str(&homeserver)?;
|
|
|
|
|
|
- let rpc_client = connect_to_server(addr).await?;
|
|
|
+ let acfg = self.create_ecks_auth_config();
|
|
|
+ let rpc_client = connect_to_server(addr, acfg).await?;
|
|
|
let client = HomeserverClient::from_client(rpc_client);
|
|
|
let client = Arc::new(client);
|
|
|
client
|
|
@@ -168,7 +196,8 @@ impl PeerManager {
|
|
|
info!(%homeserver, "connecting to homeserver...");
|
|
|
let addr = addrfmt::Address::from_str(&homeserver)?;
|
|
|
|
|
|
- let rpc_client = connect_to_server(addr).await?;
|
|
|
+ let acfg = self.create_ecks_auth_config();
|
|
|
+ let rpc_client = connect_to_server(addr, acfg).await?;
|
|
|
let client = HomeserverClient::from_client(rpc_client);
|
|
|
let client = Arc::new(client);
|
|
|
|
|
@@ -205,7 +234,8 @@ impl PeerManager {
|
|
|
|
|
|
info!(%homeserver, "connecting to homeserver...");
|
|
|
let addr = addrfmt::Address::from_str(&homeserver)?;
|
|
|
- let rpc_client = connect_to_server(addr).await?;
|
|
|
+ let acfg = self.create_ecks_auth_config();
|
|
|
+ let rpc_client = connect_to_server(addr, acfg).await?;
|
|
|
let client = HomeserverClient::from_client(rpc_client);
|
|
|
let mut hsc = self.homeserver_client.write().await;
|
|
|
*hsc = Some(Arc::new(client));
|
|
@@ -315,11 +345,14 @@ impl PeerManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-async fn connect_to_server(addr: addrfmt::Address) -> Result<RpcClient, Error> {
|
|
|
+async fn connect_to_server(
|
|
|
+ addr: addrfmt::Address,
|
|
|
+ auth: AspAuthConfig,
|
|
|
+) -> Result<RpcClient, Error> {
|
|
|
match addr {
|
|
|
addrfmt::Address::Sock(addr) => {
|
|
|
let conn = ecksport_net::builder::ClientBuilder::new(aspect_proto::protocol::PROTO_V0)
|
|
|
- .connect_tcp(addr)
|
|
|
+ .connect_tcp_authed(addr, auth)
|
|
|
.await?;
|
|
|
|
|
|
Ok(ecksport_rpc::client::RpcClient::new(conn))
|