1use ns;
2use minidom::Element;
3use util::FromElement;
4
5#[derive(Clone, Debug)]
6pub enum Condition {
7 Aborted,
8 AccountDisabled(Option<String>),
9 CredentialsExpired,
10 EncryptionRequired,
11 IncorrectEncoding,
12 InvalidAuthzid,
13 InvalidMechanism,
14 MalformedRequest,
15 MechanismTooWeak,
16 NotAuthorized,
17 TemporaryAuthFailure,
18 Unknown,
19}
20
21#[derive(Clone, Debug)]
22pub struct SaslError {
23 condition: Condition,
24 text: Option<String>,
25}
26
27impl FromElement for SaslError {
28 type Err = ();
29
30 fn from_element(element: &Element) -> Result<SaslError, ()> {
31 if !element.is("failure", ns::SASL) {
32 return Err(());
33 }
34 let mut err = SaslError {
35 condition: Condition::Unknown,
36 text: None,
37 };
38 if let Some(text) = element.get_child("text", ns::SASL) {
39 let desc = text.text();
40 err.text = Some(desc);
41 }
42 if element.has_child("aborted", ns::SASL) {
43 err.condition = Condition::Aborted;
44 }
45 else if let Some(account_disabled) = element.get_child("account-disabled", ns::SASL) {
46 let text = account_disabled.text();
47 err.condition = Condition::AccountDisabled(if text == "" { None } else { Some(text) });
48 }
49 else if element.has_child("credentials-expired", ns::SASL) {
50 err.condition = Condition::CredentialsExpired;
51 }
52 else if element.has_child("encryption-required", ns::SASL) {
53 err.condition = Condition::EncryptionRequired;
54 }
55 else if element.has_child("incorrect-encoding", ns::SASL) {
56 err.condition = Condition::IncorrectEncoding;
57 }
58 else if element.has_child("invalid-authzid", ns::SASL) {
59 err.condition = Condition::InvalidAuthzid;
60 }
61 else if element.has_child("malformed-request", ns::SASL) {
62 err.condition = Condition::MalformedRequest;
63 }
64 else if element.has_child("mechanism-too-weak", ns::SASL) {
65 err.condition = Condition::MechanismTooWeak;
66 }
67 else if element.has_child("not-authorized", ns::SASL) {
68 err.condition = Condition::NotAuthorized;
69 }
70 else if element.has_child("temporary-auth-failure", ns::SASL) {
71 err.condition = Condition::TemporaryAuthFailure;
72 }
73 Ok(err)
74 }
75}