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?;
proto_xmpp::sasl::Success.write_xml(xml_writer).await?;
let auth_body = AuthBody::from_str(auth.body);
match auth_body {
match AuthBody::from_str(&auth.body) {
Ok(logopass) => {
let name = &logopass.login;
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_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 {
pub fn from_str(input: Vec<u8>) -> Result<AuthBody> {
let decoded_body = general_purpose::STANDARD.decode(input).unwrap();
let parsed_to_string = String::from_utf8(decoded_body).unwrap();
let separated_words: Vec<&str> = parsed_to_string.split("\x00").filter(|&part| !part.is_empty()).collect::<Vec<_>>().clone();
if separated_words.len() == 2 {
Ok(AuthBody { login: separated_words[0].to_string(), password: separated_words[1].to_string() })
} else { Err(anyhow!("Incorrect auth format")) }
pub fn from_str(input: &[u8]) -> Result<AuthBody> {
match general_purpose::STANDARD.decode(input){
Ok(decoded_body) => {
match String::from_utf8(decoded_body) {
Ok(parsed_to_string) => {
let separated_words: Vec<&str> = parsed_to_string.split("\x00").filter(|&part| !part.is_empty()).collect::<Vec<_>>().clone();
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))
}
}
}