pub mod commands;
mod credential;
mod dispatcher;
pub mod exec;
#[cfg(feature = "pkce-auth")]
pub mod pkce;
use async_trait::async_trait;
pub use commands::{
AuthLoginResult, AuthStatusEntry, auth_command_group, login_and_build,
login_and_build_with_scopes, logout_result, status_result, to_status_entry,
};
pub use credential::{CACHE_TTL, Credential};
pub use dispatcher::{Dispatcher, SingleProvider, StatusEntry};
pub use exec::{
ACTION_AUTHENTICATE, ACTION_LIST_ENVIRONMENTS, ACTION_LIST_REALMS, ACTION_LOGOUT,
ACTION_STATUS, AuthnRequest, EnvironmentsResponse, ExecProvider,
};
use crate::Result;
use crate::middleware::CommandMeta;
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct CredentialRequest<'req> {
pub env: &'req str,
pub command: &'req str,
pub tier: &'req str,
pub meta: &'req CommandMeta,
}
impl<'req> CredentialRequest<'req> {
#[must_use]
pub fn new(
env: &'req str,
command: &'req str,
tier: &'req str,
meta: &'req CommandMeta,
) -> Self {
Self {
env,
command,
tier,
meta,
}
}
}
#[async_trait]
pub trait AuthProvider: Send + Sync + std::fmt::Debug {
fn name(&self) -> &str;
async fn get_credential(&self, env: &str, command: &str, tier: &str) -> Result<Credential>;
async fn get_credential_for(&self, req: &CredentialRequest<'_>) -> Result<Credential> {
self.get_credential(req.env, req.command, req.tier).await
}
async fn status(&self, env: &str) -> Result<Credential>;
async fn logout(&self, env: &str) -> Result<()>;
async fn list_environments(&self) -> Result<Vec<String>>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn credential_request_new_sets_all_fields() {
let meta = CommandMeta::default();
let req = CredentialRequest::new("dev", "app:list", "read", &meta);
assert_eq!(req.env, "dev");
assert_eq!(req.command, "app:list");
assert_eq!(req.tier, "read");
let copy = req;
assert_eq!(copy.env, req.env);
}
}