use std::collections::HashMap;
#[derive(Debug, Clone, Default)]
pub enum AuthIdentity {
#[default]
Anonymous,
Authenticated {
owner: String,
claims: Option<serde_json::Value>,
},
}
impl AuthIdentity {
pub fn is_authenticated(&self) -> bool {
matches!(self, Self::Authenticated { .. })
}
pub fn owner(&self) -> &str {
match self {
Self::Anonymous => "anonymous",
Self::Authenticated { owner, .. } => owner,
}
}
pub fn claims(&self) -> Option<&serde_json::Value> {
match self {
Self::Anonymous => None,
Self::Authenticated { claims, .. } => claims.as_ref(),
}
}
}
#[derive(Debug, Clone)]
pub struct RequestContext {
pub bearer_token: Option<String>,
pub headers: http::HeaderMap,
pub identity: AuthIdentity,
pub extensions: HashMap<String, serde_json::Value>,
}
impl RequestContext {
pub fn new() -> Self {
Self {
bearer_token: None,
headers: http::HeaderMap::new(),
identity: AuthIdentity::default(),
extensions: HashMap::new(),
}
}
}
impl Default for RequestContext {
fn default() -> Self {
Self::new()
}
}