use async_trait::async_trait;
use kyma_core::tenant::TenantId;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum Role {
Read = 0,
Write = 1,
Admin = 2,
}
impl Role {
pub fn parse(s: &str) -> Option<Self> {
match s.trim() {
"read" => Some(Role::Read),
"write" => Some(Role::Write),
"admin" => Some(Role::Admin),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct Principal {
pub tenant: TenantId,
pub role: Role,
pub subject: Option<String>,
}
#[derive(Debug, thiserror::Error)]
pub enum AuthError {
#[error("missing Authorization: Bearer <token>")]
MissingToken,
#[error("unknown token")]
UnknownToken,
#[error("auth backend error: {0}")]
Backend(String),
}
#[async_trait]
pub trait AuthBackend: Send + Sync + 'static {
fn enabled(&self) -> bool;
async fn authenticate(&self, token: &str) -> Result<Principal, AuthError>;
}