pub mod api_key;
pub mod basic;
pub mod bearer;
pub mod custom;
pub mod oauth2;
use crate::error::FaucetError;
use reqwest::header::HeaderMap;
#[derive(Debug, Clone)]
pub enum Auth {
None,
Bearer(String),
Basic {
username: String,
password: String,
},
ApiKey {
header: String,
value: String,
},
ApiKeyQuery {
param: String,
value: String,
},
OAuth2 {
token_url: String,
client_id: String,
client_secret: String,
scopes: Vec<String>,
expiry_ratio: f64,
},
Custom(HeaderMap),
}
impl Auth {
pub fn apply(&self, headers: &mut HeaderMap) -> Result<(), FaucetError> {
match self {
Auth::None | Auth::ApiKeyQuery { .. } => Ok(()),
Auth::Bearer(token) => bearer::apply(headers, token),
Auth::Basic { username, password } => basic::apply(headers, username, password),
Auth::ApiKey { header, value } => api_key::apply(headers, header, value),
Auth::OAuth2 { .. } => Err(FaucetError::Auth(
"OAuth2 auth must be resolved to a bearer token before applying; \
use RestStream (which resolves it automatically) or call \
fetch_oauth2_token() and use Auth::Bearer"
.into(),
)),
Auth::Custom(h) => {
custom::apply(headers, h);
Ok(())
}
}
}
}
pub use oauth2::fetch_oauth2_token;