use crate::types::RedisResult;
use futures_util::Stream;
use std::pin::Pin;
#[derive(Clone, Default)]
pub struct BasicAuth {
pub(crate) username: String,
pub(crate) password: String,
}
impl BasicAuth {
pub fn new(username: String, password: String) -> Self {
Self { username, password }
}
pub fn username(&self) -> &str {
&self.username
}
pub fn password(&self) -> &str {
&self.password
}
pub fn set_username(&mut self, username: String) {
self.username = username;
}
pub fn set_password(&mut self, password: String) {
self.password = password;
}
}
impl std::fmt::Debug for BasicAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
username,
password: _,
} = self;
f.debug_struct("BasicAuth")
.field("username", username)
.field("password", &"<redacted>")
.finish()
}
}
pub trait StreamingCredentialsProvider: Send + Sync {
fn subscribe(&self) -> Pin<Box<dyn Stream<Item = RedisResult<BasicAuth>> + Send + 'static>>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_does_not_print_password() {
let auth = BasicAuth::new("user".to_string(), "hunter2".to_string());
let debug = format!("{auth:?}");
assert!(debug.contains("user"));
assert!(!debug.contains("hunter2"));
}
}