use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const PRINCIPAL_CLAIM: &str = "principal";
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct AuthContext {
pub user_id: String,
pub session_id: String,
pub roles: Vec<String>,
pub metadata: Value,
}
impl AuthContext {
pub fn new(user_id: String, session_id: String, roles: Vec<String>, metadata: Value) -> Self {
Self {
user_id,
session_id,
roles,
metadata,
}
}
pub fn anonymous() -> Self {
Self {
user_id: "anonymous".to_string(),
session_id: String::new(),
roles: vec![],
metadata: Value::Null,
}
}
pub fn for_principal(
principal: &crate::identity::Principal,
session_id: impl Into<String>,
roles: Vec<String>,
metadata: Value,
) -> Self {
let mut map = match metadata {
Value::Object(m) => m,
Value::Null => serde_json::Map::new(),
other => {
let mut m = serde_json::Map::new();
m.insert("claims".to_string(), other);
m
}
};
map.insert(
PRINCIPAL_CLAIM.to_string(),
Value::String(principal.to_string()),
);
Self {
user_id: principal.as_legacy_user_id(),
session_id: session_id.into(),
roles,
metadata: Value::Object(map),
}
}
pub fn principal(&self) -> Option<crate::identity::Principal> {
if let Some(raw) = self.get_metadata_string(PRINCIPAL_CLAIM) {
if let Ok(p) = raw.parse::<crate::identity::Principal>() {
return Some(p);
}
return None;
}
crate::identity::Principal::from_legacy_user_id(&self.user_id)
}
pub fn is_authenticated(&self) -> bool {
self.user_id != "anonymous" && !self.session_id.is_empty()
}
pub fn expired(&self) -> bool {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(i64::MAX);
self.metadata.get(EXPIRES_AT_KEY).and_then(Value::as_i64).is_some_and(|exp| exp <= now)
}
pub fn has_role(&self, role: &str) -> bool {
self.roles.iter().any(|r| r == role)
}
pub fn get_metadata_string(&self, key: &str) -> Option<String> {
self.metadata
.get(key)
.and_then(|v| v.as_str())
.map(String::from)
}
pub fn tenant(&self) -> Option<String> {
self.get_metadata_string("tenant_id")
.or_else(|| self.get_metadata_string("realm"))
}
pub fn tenant_id(&self) -> Option<crate::tenant::types::TenantId> {
self.get_metadata_string("org_id")
.or_else(|| self.get_metadata_string("tenant_id"))
.and_then(|s| crate::tenant::types::TenantId::try_new(s).ok())
}
pub(crate) fn derive_callee_context(
caller_ctx: &AuthContext,
derivation: &crate::forward::ForwardDerivation,
_immediate_caller_stamp: &crate::principal::Principal,
) -> AuthContext {
let (user_id, session_id) = if derivation.keep_verified_user {
(caller_ctx.user_id.clone(), caller_ctx.session_id.clone())
} else {
("anonymous".to_string(), String::new())
};
let roles = if derivation.keep_roles {
caller_ctx.roles.clone()
} else {
Vec::new()
};
let mut metadata = if derivation.keep_metadata {
caller_ctx.metadata.clone()
} else {
Value::Null
};
if !derivation.keep_verified_user {
if let Value::Object(ref mut map) = metadata {
map.remove(PRINCIPAL_CLAIM);
}
} else if !derivation.keep_metadata {
if let Some(stamp) = caller_ctx.metadata.get(PRINCIPAL_CLAIM) {
let mut map = serde_json::Map::new();
map.insert(PRINCIPAL_CLAIM.to_string(), stamp.clone());
metadata = Value::Object(map);
}
}
AuthContext {
user_id,
session_id,
roles,
metadata,
}
}
pub fn with_callee_context<F, R>(
&self,
derivation: &crate::forward::ForwardDerivation,
immediate_caller_stamp: &crate::principal::Principal,
f: F,
) -> R
where
F: FnOnce(AuthContext) -> R,
{
f(Self::derive_callee_context(self, derivation, immediate_caller_stamp))
}
}
pub const EXPIRES_AT_KEY: &str = "expires_at";
#[async_trait]
pub trait SessionValidator: Send + Sync + 'static {
async fn validate(&self, cookie_value: &str) -> Option<AuthContext>;
fn alive(&self, ctx: &AuthContext) -> Result<(), String> {
if ctx.expired() {
return Err("your sign-in has expired: sign in again".into());
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_context_creation() {
let ctx = AuthContext::new(
"user-123".to_string(),
"sess-456".to_string(),
vec!["admin".to_string()],
serde_json::json!({"tenant_id": "acme"}),
);
assert_eq!(ctx.user_id, "user-123");
assert_eq!(ctx.session_id, "sess-456");
assert!(ctx.has_role("admin"));
assert!(!ctx.has_role("user"));
assert_eq!(ctx.tenant(), Some("acme".to_string()));
assert!(ctx.is_authenticated());
}
#[test]
fn test_auth_context_clone() {
let ctx = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec!["admin".to_string()],
serde_json::json!({"org": "acme"}),
);
let cloned = ctx.clone();
assert_eq!(ctx.user_id, cloned.user_id);
assert_eq!(ctx.session_id, cloned.session_id);
assert_eq!(ctx.roles, cloned.roles);
}
#[test]
fn test_anonymous_context() {
let ctx = AuthContext::anonymous();
assert_eq!(ctx.user_id, "anonymous");
assert!(!ctx.is_authenticated());
assert!(ctx.roles.is_empty());
}
#[test]
fn test_role_checking() {
let ctx = AuthContext::new(
"user-1".to_string(),
"sess-1".to_string(),
vec!["user".to_string(), "editor".to_string()],
Value::Null,
);
assert!(ctx.has_role("user"));
assert!(ctx.has_role("editor"));
assert!(!ctx.has_role("admin"));
}
#[test]
fn test_metadata_access() {
let ctx = AuthContext::new(
"user-1".to_string(),
"sess-1".to_string(),
vec![],
serde_json::json!({
"tenant_id": "org-123",
"realm": "production",
"email": "user@example.com"
}),
);
assert_eq!(
ctx.get_metadata_string("tenant_id"),
Some("org-123".to_string())
);
assert_eq!(
ctx.get_metadata_string("realm"),
Some("production".to_string())
);
assert_eq!(
ctx.get_metadata_string("email"),
Some("user@example.com".to_string())
);
assert_eq!(ctx.get_metadata_string("nonexistent"), None);
}
#[test]
fn derive_callee_context_identity_only_strips_roles_and_metadata() {
use crate::forward::ForwardDerivation;
use crate::principal::Principal;
let caller = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec!["admin".to_string(), "editor".to_string()],
serde_json::json!({"tenant_id": "acme"}),
);
let stamp = Principal::anonymous_sealed();
let callee = AuthContext::derive_callee_context(
&caller,
&ForwardDerivation::IDENTITY_ONLY,
&stamp,
);
assert_eq!(callee.user_id, "alice");
assert_eq!(callee.session_id, "sess-1");
assert!(callee.roles.is_empty());
assert_eq!(callee.metadata, Value::Null);
}
#[test]
fn derive_callee_context_pass_through_retains_all_fields() {
use crate::forward::ForwardDerivation;
use crate::principal::Principal;
let caller = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec!["admin".to_string()],
serde_json::json!({"tenant_id": "acme", "k": "v"}),
);
let stamp = Principal::anonymous_sealed();
let callee = AuthContext::derive_callee_context(
&caller,
&ForwardDerivation::PASS_THROUGH,
&stamp,
);
assert_eq!(callee.user_id, caller.user_id);
assert_eq!(callee.session_id, caller.session_id);
assert_eq!(callee.roles, caller.roles);
assert_eq!(callee.metadata, caller.metadata);
}
#[test]
fn derive_callee_context_anonymous_drops_everything() {
use crate::forward::ForwardDerivation;
use crate::principal::Principal;
let caller = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec!["admin".to_string()],
serde_json::json!({"tenant_id": "acme"}),
);
let stamp = Principal::anonymous_sealed();
let callee = AuthContext::derive_callee_context(
&caller,
&ForwardDerivation::ANONYMOUS,
&stamp,
);
assert_eq!(callee.user_id, "anonymous");
assert_eq!(callee.session_id, "");
assert!(callee.roles.is_empty());
assert_eq!(callee.metadata, Value::Null);
assert!(!callee.is_authenticated());
}
#[test]
fn with_callee_context_invokes_closure_with_derived_callee() {
use crate::forward::ForwardDerivation;
use crate::principal::Principal;
let caller = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec!["admin".to_string()],
serde_json::json!({"tenant_id": "org-1"}),
);
let stamp = Principal::anonymous_sealed();
let observed_user_id =
caller.with_callee_context(&ForwardDerivation::IDENTITY_ONLY, &stamp, |callee| {
assert!(callee.roles.is_empty());
assert_eq!(callee.metadata, Value::Null);
callee.user_id
});
assert_eq!(observed_user_id, "alice");
}
#[test]
fn with_callee_context_returns_closure_value() {
use crate::forward::ForwardDerivation;
use crate::principal::Principal;
let caller = AuthContext::anonymous();
let stamp = Principal::anonymous_sealed();
let answer =
caller.with_callee_context(&ForwardDerivation::PASS_THROUGH, &stamp, |_| 42_u32);
assert_eq!(answer, 42);
}
#[test]
fn identity_only_keeps_the_callers_principal_stamp() {
use crate::forward::ForwardDerivation;
use crate::principal::Principal;
let subject: crate::identity::Principal =
"key:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".parse().unwrap();
let caller = AuthContext::for_principal(&subject, "s1", vec!["viewer".into()], serde_json::json!({ "k": "v" }));
let callee = AuthContext::derive_callee_context(&caller, &ForwardDerivation::IDENTITY_ONLY, &Principal::anonymous_sealed());
assert_eq!(callee.principal(), Some(subject), "the identity, whole");
assert!(callee.roles.is_empty());
assert_eq!(callee.get_metadata_string("k"), None, "and nothing else of the metadata");
assert!(callee.is_authenticated());
}
#[test]
fn derive_callee_context_never_grows_context() {
use crate::forward::ForwardDerivation;
use crate::principal::Principal;
let caller = AuthContext::anonymous();
let stamp = Principal::anonymous_sealed();
let callee = AuthContext::derive_callee_context(
&caller,
&ForwardDerivation::PASS_THROUGH,
&stamp,
);
assert_eq!(callee.user_id, "anonymous");
assert!(callee.roles.is_empty());
assert_eq!(callee.metadata, Value::Null);
assert!(!callee.is_authenticated());
}
#[test]
fn dropping_the_verified_user_also_drops_the_principal_stamp() {
use crate::forward::ForwardDerivation;
use crate::identity::Principal as Subject;
use crate::principal::Principal;
let subject: Subject = "idp:6ba7b810-9dad-11d1-80b4-00c04fd430c8".parse().unwrap();
let caller = AuthContext::for_principal(
&subject,
"sess-1",
vec!["admin".to_string()],
serde_json::json!({"org_id": "acme"}),
);
assert_eq!(caller.principal(), Some(subject.clone()));
let stamp = Principal::anonymous_sealed();
let kept =
AuthContext::derive_callee_context(&caller, &ForwardDerivation::PASS_THROUGH, &stamp);
assert_eq!(kept.principal(), Some(subject));
assert_eq!(kept.user_id, "6ba7b810-9dad-11d1-80b4-00c04fd430c8");
let dropped =
AuthContext::derive_callee_context(&caller, &ForwardDerivation::ANONYMOUS, &stamp);
assert_eq!(dropped.user_id, "anonymous");
assert_eq!(dropped.principal(), None);
let mixed = ForwardDerivation {
keep_verified_user: false,
keep_roles: false,
keep_capabilities: false,
keep_metadata: true,
};
let mixed = AuthContext::derive_callee_context(&caller, &mixed, &stamp);
assert_eq!(mixed.user_id, "anonymous");
assert_eq!(
mixed.principal(),
None,
"keep_metadata must not smuggle the caller's principal through"
);
assert_eq!(mixed.get_metadata_string("org_id").as_deref(), Some("acme"));
}
#[test]
fn for_principal_and_new_agree_on_an_idp_identity() {
use crate::identity::Principal as Subject;
let uuid = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
let subject: Subject = format!("idp:{uuid}").parse().unwrap();
let stamped = AuthContext::for_principal(&subject, "s", vec![], Value::Null);
let legacy = AuthContext::new(uuid.to_string(), "s".to_string(), vec![], Value::Null);
assert_eq!(stamped.user_id, legacy.user_id);
assert_eq!(stamped.principal(), legacy.principal());
}
#[test]
fn tenant_id_prefers_org_id_claim() {
let ctx = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec![],
serde_json::json!({"org_id": "org_acme", "tenant_id": "legacy"}),
);
assert_eq!(
ctx.tenant_id().map(|t| t.as_str().to_string()),
Some("org_acme".to_string())
);
}
#[test]
fn tenant_id_falls_back_to_legacy_tenant_id_claim() {
let ctx = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec![],
serde_json::json!({"tenant_id": "acme"}),
);
assert_eq!(
ctx.tenant_id().map(|t| t.as_str().to_string()),
Some("acme".to_string())
);
}
#[test]
fn tenant_id_is_none_when_absent_or_malformed() {
let absent = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec![],
serde_json::json!({"realm": "prod"}),
);
assert!(absent.tenant_id().is_none());
let malformed = AuthContext::new(
"alice".to_string(),
"sess-1".to_string(),
vec![],
serde_json::json!({"org_id": "evil\u{0000}org"}),
);
assert!(malformed.tenant_id().is_none());
}
#[test]
fn test_tenant_from_metadata() {
let ctx1 = AuthContext::new(
"user-1".to_string(),
"sess-1".to_string(),
vec![],
serde_json::json!({"tenant_id": "org-123", "realm": "prod"}),
);
assert_eq!(ctx1.tenant(), Some("org-123".to_string()));
let ctx2 = AuthContext::new(
"user-1".to_string(),
"sess-1".to_string(),
vec![],
serde_json::json!({"realm": "prod"}),
);
assert_eq!(ctx2.tenant(), Some("prod".to_string()));
let ctx3 = AuthContext::new(
"user-1".to_string(),
"sess-1".to_string(),
vec![],
Value::Null,
);
assert_eq!(ctx3.tenant(), None);
}
}