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
use types::CBytes;

pub trait Authenticator: Clone {
    fn get_auth_token(&self) -> CBytes;
    fn get_cassandra_name(&self) -> &str;
}

#[derive(Clone)]
pub struct PasswordAuthenticator<'a> {
    username: &'a str,
    password: &'a str
}

impl<'a> PasswordAuthenticator<'a> {
    pub fn new<'b>(username: &'b str, password: &'b str) -> PasswordAuthenticator<'b> {
        return PasswordAuthenticator {
            username: username,
            password: password
        };
    }
}

impl<'a> Authenticator for PasswordAuthenticator<'a> {
    fn get_auth_token(&self) -> CBytes {
        let mut token = vec![0];
        token.extend_from_slice(self.username.as_bytes());
        token.push(0);
        token.extend_from_slice(self.password.as_bytes());

        return CBytes::new(token);
    }

    fn get_cassandra_name(&self) -> &str {
        return "org.apache.cassandra.auth.PasswordAuthenticator";
    }
}