deleted unwrap, used &[u8] in fn from_str

This commit is contained in:
JustTestingV 2023-10-08 13:31:57 +03:00
parent ffc383af3b
commit 06691bfc44
2 changed files with 18 additions and 12 deletions

View File

@ -257,9 +257,7 @@ async fn socket_auth(
let auth: proto_xmpp::sasl::Auth = proto_xmpp::sasl::Auth::parse(xml_reader, reader_buf).await?; let auth: proto_xmpp::sasl::Auth = proto_xmpp::sasl::Auth::parse(xml_reader, reader_buf).await?;
proto_xmpp::sasl::Success.write_xml(xml_writer).await?; proto_xmpp::sasl::Success.write_xml(xml_writer).await?;
let auth_body = AuthBody::from_str(auth.body); match AuthBody::from_str(&auth.body) {
match auth_body {
Ok(logopass) => { Ok(logopass) => {
let name = &logopass.login; let name = &logopass.login;
let stored_user = storage.retrieve_user_by_name(name).await?; let stored_user = storage.retrieve_user_by_name(name).await?;
@ -288,8 +286,8 @@ async fn socket_auth(
xmpp_resource: Resource(name.to_string().into()), xmpp_resource: Resource(name.to_string().into()),
xmpp_muc_name: Resource(name.to_string().into()), xmpp_muc_name: Resource(name.to_string().into()),
}) })
} },
Err(e) => Err(e) Err(e) => return Err(e)
} }
} }

View File

@ -35,13 +35,21 @@ pub struct AuthBody {
} }
impl AuthBody { impl AuthBody {
pub fn from_str(input: Vec<u8>) -> Result<AuthBody> { pub fn from_str(input: &[u8]) -> Result<AuthBody> {
let decoded_body = general_purpose::STANDARD.decode(input).unwrap(); match general_purpose::STANDARD.decode(input){
let parsed_to_string = String::from_utf8(decoded_body).unwrap(); Ok(decoded_body) => {
let separated_words: Vec<&str> = parsed_to_string.split("\x00").filter(|&part| !part.is_empty()).collect::<Vec<_>>().clone(); match String::from_utf8(decoded_body) {
if separated_words.len() == 2 { Ok(parsed_to_string) => {
Ok(AuthBody { login: separated_words[0].to_string(), password: separated_words[1].to_string() }) let separated_words: Vec<&str> = parsed_to_string.split("\x00").filter(|&part| !part.is_empty()).collect::<Vec<_>>().clone();
} else { Err(anyhow!("Incorrect auth format")) } if separated_words.len() == 2 {
Ok(AuthBody { login: separated_words[0].to_string(), password: separated_words[1].to_string() })
} else { return Err(anyhow!("Incorrect auth format")) }
},
Err(e) => return Err(anyhow!(e))
}
},
Err(e) => return Err(anyhow!(e))
}
} }
} }