pub mod handshake;
pub mod md5;
pub mod scram;
#[cfg(feature = "postgres")]
pub mod postgres;
#[cfg(feature = "gel")]
pub mod gel;
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub enum AuthType {
#[default]
Deny,
Trust,
Plain,
Md5,
ScramSha256,
}
#[derive(derive_more::Debug, Clone)]
pub enum CredentialData {
Trust,
Deny,
#[debug("Plain(...)")]
Plain(String),
#[debug("Md5(...)")]
Md5(md5::StoredHash),
#[debug("Scram(...)")]
Scram(scram::StoredKey),
}
impl CredentialData {
pub fn new(ty: AuthType, username: String, password: String) -> Self {
match ty {
AuthType::Deny => Self::Deny,
AuthType::Trust => Self::Trust,
AuthType::Plain => Self::Plain(password),
AuthType::Md5 => Self::Md5(md5::StoredHash::generate(password.as_bytes(), &username)),
AuthType::ScramSha256 => {
let salt: [u8; 32] = rand::random();
Self::Scram(scram::StoredKey::generate(password.as_bytes(), &salt, 4096))
}
}
}
pub fn auth_type(&self) -> AuthType {
match self {
CredentialData::Trust => AuthType::Trust,
CredentialData::Deny => AuthType::Deny,
CredentialData::Plain(..) => AuthType::Plain,
CredentialData::Md5(..) => AuthType::Md5,
CredentialData::Scram(..) => AuthType::ScramSha256,
}
}
}