use std::fmt;
use base64::engine::{Engine, general_purpose};
#[derive(Clone)]
pub enum Auth {
Credentials {
username: String,
password: String,
},
Token(String),
}
impl fmt::Debug for Auth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Auth(<sensitive>)")
}
}
impl Auth {
#[deprecated(since = "0.6.0", note = "Use `Auth::credentials` instead.")]
pub fn new<S>(username: S, password: S) -> Self
where
S: Into<String>,
{
Self::credentials(username, password)
}
#[inline]
pub fn credentials<U, P>(username: U, password: P) -> Self
where
U: Into<String>,
P: Into<String>,
{
Self::Credentials {
username: username.into(),
password: password.into(),
}
}
#[inline]
pub fn token<S>(token: S) -> Self
where
S: Into<String>,
{
Self::Token(token.into())
}
pub(crate) fn header_value(&self) -> String {
match self {
Self::Credentials { username, password } => {
let b64: String =
general_purpose::STANDARD.encode(format!("{username}:{password}"));
format!("Basic {b64}")
}
Self::Token(token) => {
format!("Bearer {token}")
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_credentials() {
let auth = Auth::credentials("username", "password");
assert_eq!(auth.header_value(), "Basic dXNlcm5hbWU6cGFzc3dvcmQ=");
}
#[test]
fn test_auth_token() {
let auth = Auth::token("tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2");
assert_eq!(
auth.header_value(),
"Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2"
);
}
}