Skip to content

Commit 18b51dd

Browse files
committed
Change authenticator to be an enum
Motivation ---------- Authenticator being a trait makes it more difficult to work with than if it was an enum. It is intended to be a public facing interface but with private-only implementations. As we know all of the implementations we can just use an enum. Changes -------- Change authenticator to be an enum.
1 parent ded8e9c commit 18b51dd

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
lines changed

sdk/couchbase-core/src/authenticator.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use std::fmt::Debug;
33
use crate::error::Result;
44
use crate::service_type::ServiceType;
55

6-
pub trait Authenticator: Debug + Send + Sync {
6+
#[derive(Debug, Clone, PartialEq, Hash)]
7+
#[non_exhaustive]
8+
pub enum Authenticator {
9+
PasswordAuthenticator(PasswordAuthenticator),
710
// TODO: get_client_certificate needs some thought about how to expose the certificate
8-
// fn get_client_certificate(service: ServiceType, host_port: String) ->
9-
fn get_credentials(&self, service_type: ServiceType, host_port: String)
10-
-> Result<UserPassPair>;
1111
}
1212

1313
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -22,8 +22,8 @@ pub struct PasswordAuthenticator {
2222
pub password: String,
2323
}
2424

25-
impl Authenticator for PasswordAuthenticator {
26-
fn get_credentials(
25+
impl PasswordAuthenticator {
26+
pub fn get_credentials(
2727
&self,
2828
_service_type: ServiceType,
2929
_host_port: String,
@@ -34,3 +34,9 @@ impl Authenticator for PasswordAuthenticator {
3434
})
3535
}
3636
}
37+
38+
impl From<PasswordAuthenticator> for Authenticator {
39+
fn from(value: PasswordAuthenticator) -> Self {
40+
Authenticator::PasswordAuthenticator(value)
41+
}
42+
}

sdk/couchbase-core/src/configwatcher.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,13 @@ mod tests {
224224
root_certs: None,
225225
accept_all_certs: None,
226226
client_name: "myclient".to_string(),
227-
authenticator: Some(Arc::new(PasswordAuthenticator {
228-
username: "Administrator".to_string(),
229-
password: "password".to_string(),
230-
})),
227+
authenticator: Some(Arc::new(
228+
PasswordAuthenticator {
229+
username: "Administrator".to_string(),
230+
password: "password".to_string(),
231+
}
232+
.into(),
233+
)),
231234
selected_bucket: Some("default".to_string()),
232235
disable_default_features: false,
233236
disable_error_map: false,

sdk/couchbase-core/src/crudcomponent.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,13 @@ mod tests {
193193
root_certs: None,
194194
accept_all_certs: None,
195195
client_name: "myclient".to_string(),
196-
authenticator: Some(Arc::new(PasswordAuthenticator {
197-
username: "Administrator".to_string(),
198-
password: "password".to_string(),
199-
})),
196+
authenticator: Some(Arc::new(
197+
PasswordAuthenticator {
198+
username: "Administrator".to_string(),
199+
password: "password".to_string(),
200+
}
201+
.into(),
202+
)),
200203
selected_bucket: Some("default".to_string()),
201204
disable_default_features: false,
202205
disable_error_map: false,

sdk/couchbase-core/src/kvclient.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) struct KvClientConfig {
3030
pub root_certs: Option<RootCertStore>,
3131
pub accept_all_certs: Option<bool>,
3232
pub client_name: String,
33-
pub authenticator: Option<Arc<dyn Authenticator>>,
33+
pub authenticator: Option<Arc<Authenticator>>,
3434
pub selected_bucket: Option<String>,
3535
pub disable_default_features: bool,
3636
pub disable_error_map: bool,
@@ -142,8 +142,13 @@ where
142142
None
143143
};
144144

145-
let bootstrap_auth = if let Some(ref auth) = config.authenticator {
146-
let creds = auth.get_credentials(ServiceType::Memd, config.address.to_string())?;
145+
let bootstrap_auth = if let Some(auth) = &config.authenticator {
146+
let creds = match auth.as_ref() {
147+
// PasswordAuthenticator(auth) => get_credentials(ServiceType::Memd, config.address.to_string())
148+
Authenticator::PasswordAuthenticator(a) => {
149+
a.get_credentials(ServiceType::Memd, config.address.to_string())?
150+
}
151+
};
147152

148153
Some(SASLAuthAutoOptions {
149154
username: creds.username.clone(),
@@ -395,10 +400,13 @@ mod tests {
395400
root_certs: None,
396401
accept_all_certs: None,
397402
client_name: "myclient".to_string(),
398-
authenticator: Some(Arc::new(PasswordAuthenticator {
399-
username: "Administrator".to_string(),
400-
password: "password".to_string(),
401-
})),
403+
authenticator: Some(Arc::new(
404+
PasswordAuthenticator {
405+
username: "Administrator".to_string(),
406+
password: "password".to_string(),
407+
}
408+
.into(),
409+
)),
402410
selected_bucket: Some("default".to_string()),
403411
disable_default_features: false,
404412
disable_error_map: false,

sdk/couchbase-core/src/kvclientpool.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,13 @@ mod tests {
357357
root_certs: None,
358358
accept_all_certs: None,
359359
client_name: "myclient".to_string(),
360-
authenticator: Some(Arc::new(PasswordAuthenticator {
361-
username: "Administrator".to_string(),
362-
password: "password".to_string(),
363-
})),
360+
authenticator: Some(Arc::new(
361+
PasswordAuthenticator {
362+
username: "Administrator".to_string(),
363+
password: "password".to_string(),
364+
}
365+
.into(),
366+
)),
364367
selected_bucket: Some("default".to_string()),
365368
disable_default_features: false,
366369
disable_error_map: false,
@@ -449,10 +452,13 @@ mod tests {
449452
root_certs: None,
450453
accept_all_certs: None,
451454
client_name: "myclient".to_string(),
452-
authenticator: Some(Arc::new(PasswordAuthenticator {
453-
username: "Administrator".to_string(),
454-
password: "password".to_string(),
455-
})),
455+
authenticator: Some(Arc::new(
456+
PasswordAuthenticator {
457+
username: "Administrator".to_string(),
458+
password: "password".to_string(),
459+
}
460+
.into(),
461+
)),
456462
selected_bucket: None,
457463
disable_default_features: false,
458464
disable_error_map: false,
@@ -481,10 +487,13 @@ mod tests {
481487
root_certs: None,
482488
accept_all_certs: None,
483489
client_name: "myclient".to_string(),
484-
authenticator: Some(Arc::new(PasswordAuthenticator {
485-
username: "Administrator".to_string(),
486-
password: "password".to_string(),
487-
})),
490+
authenticator: Some(Arc::new(
491+
PasswordAuthenticator {
492+
username: "Administrator".to_string(),
493+
password: "password".to_string(),
494+
}
495+
.into(),
496+
)),
488497
selected_bucket: Some("default".to_string()),
489498
disable_default_features: false,
490499
disable_error_map: false,

0 commit comments

Comments
 (0)