2020-03-05 01:25:24 +01:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
2024-08-11 12:21:06 +02:00
|
|
|
use xmpp_parsers::{component::Handshake, jid::Jid, ns};
|
2017-07-22 01:59:51 +01:00
|
|
|
|
2024-08-11 12:21:06 +02:00
|
|
|
use crate::{
|
|
|
|
|
connect::ServerConnector,
|
|
|
|
|
error::{AuthError, Error},
|
|
|
|
|
proto::{Packet, XmppStream},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Log into an XMPP server as a client with a jid+pass
|
|
|
|
|
pub async fn component_login<C: ServerConnector>(
|
|
|
|
|
connector: C,
|
|
|
|
|
jid: Jid,
|
|
|
|
|
password: String,
|
|
|
|
|
) -> Result<XmppStream<C::Stream>, Error> {
|
|
|
|
|
let password = password;
|
|
|
|
|
let mut xmpp_stream = connector.connect(&jid, ns::COMPONENT).await?;
|
|
|
|
|
auth(&mut xmpp_stream, password).await?;
|
|
|
|
|
Ok(xmpp_stream)
|
|
|
|
|
}
|
2017-07-22 01:59:51 +01:00
|
|
|
|
2020-03-05 01:25:24 +01:00
|
|
|
pub async fn auth<S: AsyncRead + AsyncWrite + Unpin>(
|
2024-08-06 17:00:53 +02:00
|
|
|
stream: &mut XmppStream<S>,
|
2020-03-05 01:25:24 +01:00
|
|
|
password: String,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
let nonza = Handshake::from_password_and_stream_id(&password, &stream.id);
|
|
|
|
|
stream.send_stanza(nonza).await?;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
match stream.next().await {
|
|
|
|
|
Some(Ok(Packet::Stanza(ref stanza)))
|
2020-05-30 01:19:06 +02:00
|
|
|
if stanza.is("handshake", ns::COMPONENT_ACCEPT) =>
|
2020-03-05 01:25:24 +01:00
|
|
|
{
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
Some(Ok(Packet::Stanza(ref stanza)))
|
|
|
|
|
if stanza.is("error", "http://etherx.jabber.org/streams") =>
|
|
|
|
|
{
|
|
|
|
|
return Err(AuthError::ComponentFail.into());
|
|
|
|
|
}
|
|
|
|
|
Some(_) => {}
|
|
|
|
|
None => return Err(Error::Disconnected),
|
2017-07-22 01:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|