cdrs_temp/
authenticators.rs1use crate::types::CBytes;
2
3pub trait Authenticator: Clone + Send + Sync {
4 fn get_auth_token(&self) -> CBytes;
5 fn get_cassandra_name(&self) -> Option<&str>;
6}
7
8#[derive(Debug, Clone)]
9#[deprecated(
10 since = "2.1.0",
11 note = "`PasswordAuthenticator` is deprecated in favour of `StaticPasswordAuthenticator` because the second one doesn't require static lifetime for credentials thus it's easier to use."
12)]
13pub struct PasswordAuthenticator<'a> {
14 username: &'a str,
15 password: &'a str,
16}
17
18#[allow(deprecated)]
19impl<'a> PasswordAuthenticator<'a> {
20 pub fn new<'b>(username: &'b str, password: &'b str) -> PasswordAuthenticator<'b> {
21 PasswordAuthenticator {
22 username: username,
23 password: password,
24 }
25 }
26}
27
28#[allow(deprecated)]
29impl<'a> Authenticator for PasswordAuthenticator<'a> {
30 fn get_auth_token(&self) -> CBytes {
31 let mut token = vec![0];
32 token.extend_from_slice(self.username.as_bytes());
33 token.push(0);
34 token.extend_from_slice(self.password.as_bytes());
35
36 CBytes::new(token)
37 }
38
39 fn get_cassandra_name(&self) -> Option<&str> {
40 Some("org.apache.cassandra.auth.PasswordAuthenticator")
41 }
42}
43
44#[derive(Debug, Clone)]
45pub struct StaticPasswordAuthenticator {
46 username: String,
47 password: String,
48}
49
50impl StaticPasswordAuthenticator {
51 pub fn new<S: ToString>(username: S, password: S) -> StaticPasswordAuthenticator {
52 StaticPasswordAuthenticator {
53 username: username.to_string(),
54 password: password.to_string(),
55 }
56 }
57}
58
59impl Authenticator for StaticPasswordAuthenticator {
60 fn get_auth_token(&self) -> CBytes {
61 let mut token = vec![0];
62 token.extend_from_slice(self.username.as_bytes());
63 token.push(0);
64 token.extend_from_slice(self.password.as_bytes());
65
66 CBytes::new(token)
67 }
68
69 fn get_cassandra_name(&self) -> Option<&str> {
70 Some("org.apache.cassandra.auth.PasswordAuthenticator")
71 }
72}
73
74#[derive(Debug, Clone)]
75pub struct NoneAuthenticator;
76
77impl Authenticator for NoneAuthenticator {
78 fn get_auth_token(&self) -> CBytes {
79 CBytes::new(vec![0])
80 }
81
82 fn get_cassandra_name(&self) -> Option<&str> {
83 None
84 }
85}
86
87#[cfg(test)]
88#[allow(deprecated)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn test_password_authenticator_trait_impl() {
94 let authenticator = PasswordAuthenticator::new("a", "a");
95 let _ = authenticator_tester(Box::new(authenticator));
96 }
97
98 #[test]
99 fn test_password_authenticator_new() {
100 PasswordAuthenticator::new("foo", "bar");
101 }
102
103 #[test]
104 fn test_password_authenticator_get_cassandra_name() {
105 let auth = PasswordAuthenticator::new("foo", "bar");
106 assert_eq!(
107 auth.get_cassandra_name(),
108 Some("org.apache.cassandra.auth.PasswordAuthenticator")
109 );
110 }
111
112 #[test]
113 fn test_password_authenticator_get_auth_token() {
114 let auth = PasswordAuthenticator::new("foo", "bar");
115 let mut expected_token = vec![0];
116 expected_token.extend_from_slice("foo".as_bytes());
117 expected_token.push(0);
118 expected_token.extend_from_slice("bar".as_bytes());
119
120 assert_eq!(auth.get_auth_token().into_plain().unwrap(), expected_token);
121 }
122
123 #[test]
124 fn test_static_password_authenticator_trait_impl() {
125 let authenticator = StaticPasswordAuthenticator::new("a", "a");
126 let _ = authenticator_tester(Box::new(authenticator));
127 }
128
129 #[test]
130 fn test_static_password_authenticator_new() {
131 StaticPasswordAuthenticator::new("foo", "bar");
132 }
133
134 #[test]
135 fn test_static_password_authenticator_get_cassandra_name() {
136 let auth = StaticPasswordAuthenticator::new("foo", "bar");
137 assert_eq!(
138 auth.get_cassandra_name(),
139 Some("org.apache.cassandra.auth.PasswordAuthenticator")
140 );
141 }
142
143 #[test]
144 fn test_static_password_authenticator_get_auth_token() {
145 let auth = PasswordAuthenticator::new("foo", "bar");
146 let mut expected_token = vec![0];
147 expected_token.extend_from_slice("foo".as_bytes());
148 expected_token.push(0);
149 expected_token.extend_from_slice("bar".as_bytes());
150
151 assert_eq!(auth.get_auth_token().into_plain().unwrap(), expected_token);
152 }
153
154 #[test]
155 fn test_authenticator_none_get_cassandra_name() {
156 let auth = NoneAuthenticator;
157 assert_eq!(auth.get_cassandra_name(), None);
158 assert_eq!(auth.get_auth_token().into_plain().unwrap(), vec![0]);
159 }
160
161 fn authenticator_tester<A: Authenticator>(_authenticator: Box<A>) {}
162}