DNS/TLS deps are now optional, component now also uses ServerConnector

This commit is contained in:
moparisthebest 2023-12-30 22:08:37 -05:00
commit 733d005f51
No known key found for this signature in database
GPG key ID: 88C93BFE27BC8229
17 changed files with 440 additions and 337 deletions

View file

@ -1,32 +1,11 @@
use sasl::common::{ChannelBinding, Credentials};
use tokio::io::{AsyncRead, AsyncWrite};
use sasl::common::Credentials;
use xmpp_parsers::{ns, Jid};
use super::{auth::auth, bind::bind};
use crate::client::auth::auth;
use crate::client::bind::bind;
use crate::connect::ServerConnector;
use crate::{xmpp_stream::XMPPStream, Error};
/// trait returned wrapped in XMPPStream by ServerConnector
pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Unpin + Send {}
impl<T: AsyncRead + AsyncWrite + Unpin + Send> AsyncReadAndWrite for T {}
/// Trait called to connect to an XMPP server, perhaps called multiple times
pub trait ServerConnector: Clone + core::fmt::Debug + Send + Unpin + 'static {
/// The type of Stream this ServerConnector produces
type Stream: AsyncReadAndWrite;
/// This must return the connection ready to login, ie if starttls is involved, after TLS has been started, and then after the <stream headers are exchanged
fn connect(
&self,
jid: &Jid,
) -> impl std::future::Future<Output = Result<XMPPStream<Self::Stream>, Error>> + Send;
/// Return channel binding data if available
/// do not fail if channel binding is simply unavailable, just return Ok(None)
/// this should only be called after the TLS handshake is finished
fn channel_binding(_stream: &Self::Stream) -> Result<ChannelBinding, Error> {
Ok(ChannelBinding::None)
}
}
/// Log into an XMPP server as a client with a jid+pass
/// does channel binding if supported
pub async fn client_login<C: ServerConnector>(
@ -37,7 +16,7 @@ pub async fn client_login<C: ServerConnector>(
let username = jid.node_str().unwrap();
let password = password;
let xmpp_stream = server.connect(&jid).await?;
let xmpp_stream = server.connect(&jid, ns::JABBER_CLIENT).await?;
let channel_binding = C::channel_binding(xmpp_stream.stream.get_ref())?;