use crate::auth_mechanism::AuthMechanism;
use crate::error::Result;
use crate::service_type::ServiceType;
use std::fmt::{Debug, Display};
#[derive(Clone, PartialEq, Hash)]
#[non_exhaustive]
pub enum Authenticator {
PasswordAuthenticator(PasswordAuthenticator),
CertificateAuthenticator(CertificateAuthenticator),
JwtAuthenticator(JwtAuthenticator),
}
impl Display for Authenticator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Authenticator::PasswordAuthenticator(_) => write!(f, "PasswordAuthenticator"),
Authenticator::CertificateAuthenticator(_) => {
write!(f, "CertificateAuthenticator")
}
Authenticator::JwtAuthenticator(_) => write!(f, "JwtAuthenticator"),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct UserPassPair {
pub username: String,
pub password: String,
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PasswordAuthenticator {
pub username: String,
pub password: String,
}
impl PasswordAuthenticator {
pub fn get_credentials(
&self,
_service_type: &ServiceType,
_host_port: String,
) -> Result<UserPassPair> {
Ok(UserPassPair {
username: self.username.clone(),
password: self.password.clone(),
})
}
pub fn get_auth_mechanisms(&self, tls_enabled: bool) -> Vec<AuthMechanism> {
if tls_enabled {
vec![AuthMechanism::Plain]
} else {
vec![
AuthMechanism::ScramSha512,
AuthMechanism::ScramSha256,
AuthMechanism::ScramSha1,
]
}
}
}
impl From<PasswordAuthenticator> for Authenticator {
fn from(value: PasswordAuthenticator) -> Self {
Authenticator::PasswordAuthenticator(value)
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct CertificateAuthenticator {}
impl CertificateAuthenticator {
pub fn get_credentials(
&self,
_service_type: &ServiceType,
_host_port: String,
) -> Result<UserPassPair> {
Ok(UserPassPair {
username: String::new(), password: String::new(), })
}
}
impl From<CertificateAuthenticator> for Authenticator {
fn from(value: CertificateAuthenticator) -> Self {
Authenticator::CertificateAuthenticator(value)
}
}
#[derive(Clone, PartialEq, Hash)]
pub struct JwtAuthenticator {
pub token: String,
}
impl JwtAuthenticator {
pub fn get_token(&self) -> &str {
&self.token
}
pub fn get_auth_mechanisms(&self) -> Vec<AuthMechanism> {
vec![AuthMechanism::OAuthBearer]
}
}
impl From<JwtAuthenticator> for Authenticator {
fn from(value: JwtAuthenticator) -> Self {
Authenticator::JwtAuthenticator(value)
}
}