use reqwest::RequestBuilder;
#[derive(Clone)]
pub enum Auth {
Token(String),
Bearer(String),
Basic {
username: String,
password: Option<String>,
},
}
impl Auth {
pub fn token(t: impl Into<String>) -> Self {
Self::Token(t.into())
}
pub fn bearer(t: impl Into<String>) -> Self {
Self::Bearer(t.into())
}
pub fn basic(username: impl Into<String>, password: impl Into<String>) -> Self {
Self::Basic {
username: username.into(),
password: Some(password.into()),
}
}
pub fn basic_no_password(username: impl Into<String>) -> Self {
Self::Basic {
username: username.into(),
password: None,
}
}
pub(crate) fn apply(&self, req: RequestBuilder) -> RequestBuilder {
match self {
Self::Token(t) => req.header("Authorization", format!("Token token={t}")),
Self::Bearer(t) => req.bearer_auth(t),
Self::Basic { username, password } => req.basic_auth(username, password.as_deref()),
}
}
}
impl std::fmt::Debug for Auth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Token(_) => f.debug_tuple("Token").field(&"<redacted>").finish(),
Self::Bearer(_) => f.debug_tuple("Bearer").field(&"<redacted>").finish(),
Self::Basic { username, .. } => f
.debug_struct("Basic")
.field("username", username)
.field("password", &"<redacted>")
.finish(),
}
}
}