use super::AuthContext;
use crate::{DjogiContext, DjogiError};
#[track_caller]
fn warn_auth_bypass(auth: &AuthContext, method: &'static str) {
tracing::warn!(
user_id = ?auth.user_id,
caller = %std::panic::Location::caller(),
"auth guard bypassed via {method}",
);
}
impl DjogiContext {
pub fn with_auth(mut self, auth: AuthContext) -> Self {
self.set_auth(auth);
self
}
pub fn auth(&self) -> Option<&AuthContext> {
self.auth.as_ref()
}
#[track_caller]
pub fn with_auth_insecurely(mut self, auth: AuthContext) -> Self {
warn_auth_bypass(&auth, "with_auth_insecurely");
self.auth = Some(auth);
self
}
pub fn set_auth(&mut self, auth: AuthContext) {
self.auth = Some(auth);
}
#[track_caller]
pub fn set_auth_insecurely(&mut self, auth: AuthContext) {
warn_auth_bypass(&auth, "set_auth_insecurely");
self.auth = Some(auth);
}
pub fn with_no_tenant_scope(mut self) -> Self {
self.set_no_tenant_scope();
self
}
pub fn set_no_tenant_scope(&mut self) {
self.tenant_scope_suppressed = true;
}
pub(crate) async fn ensure_tenant_set(&mut self, tenant_id: &str) -> Result<(), DjogiError> {
if self.applied_tenant_id.as_deref() == Some(tenant_id) {
return Ok(());
}
self.set_tenant(tenant_id).await?;
Ok(())
}
}