1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Provides the SASL "PLAIN" mechanism.

use crate::client::{Mechanism, MechanismError};
use crate::common::{Credentials, Identity, Password, Secret};

/// A struct for the SASL PLAIN mechanism.
pub struct Plain {
    username: String,
    password: String,
}

impl Plain {
    /// Constructs a new struct for authenticating using the SASL PLAIN mechanism.
    ///
    /// It is recommended that instead you use a `Credentials` struct and turn it into the
    /// requested mechanism using `from_credentials`.
    pub fn new<N: Into<String>, P: Into<String>>(username: N, password: P) -> Plain {
        Plain {
            username: username.into(),
            password: password.into(),
        }
    }
}

impl Mechanism for Plain {
    fn name(&self) -> &str {
        "PLAIN"
    }

    fn from_credentials(credentials: Credentials) -> Result<Plain, MechanismError> {
        if let Secret::Password(Password::Plain(password)) = credentials.secret {
            if let Identity::Username(username) = credentials.identity {
                Ok(Plain::new(username, password))
            } else {
                Err(MechanismError::PlainRequiresUsername)
            }
        } else {
            Err(MechanismError::PlainRequiresPlaintextPassword)
        }
    }

    fn initial(&mut self) -> Vec<u8> {
        let mut auth = Vec::new();
        auth.push(0);
        auth.extend(self.username.bytes());
        auth.push(0);
        auth.extend(self.password.bytes());
        auth
    }
}