wsprism_gateway/context/
tenant.rs

1use std::sync::Arc;
2
3use wsprism_core::error::{Result, WsPrismError};
4
5use crate::{app_state::AppState, policy};
6
7/// Immutable metadata for a connected session.
8/// Immutable metadata for a connected session (tenant/user/sid).
9#[derive(Debug, Clone)]
10pub struct SessionMeta {
11    /// Tenant identifier.
12    pub tenant_id: String,
13    /// User identifier within the tenant.
14    pub user_id: String,
15    /// Session identifier (per-connection).
16    pub session_id: String,
17}
18
19/// Resolved tenant runtime (compiled policy + limits) for a given session.
20#[derive(Clone)]
21pub struct TenantContext {
22    pub meta: SessionMeta,
23    pub policy: Arc<policy::TenantPolicyRuntime>,
24}
25
26impl TenantContext {
27    pub fn tenant_id(&self) -> &str {
28        &self.meta.tenant_id
29    }
30    pub fn user_id(&self) -> &str {
31        &self.meta.user_id
32    }
33    pub fn session_id(&self) -> &str {
34        &self.meta.session_id
35    }
36}
37
38/// Resolve tenant runtime or return a client-visible error.
39pub fn resolve_tenant(state: &AppState, tenant_id: &str) -> Result<Arc<policy::TenantPolicyRuntime>> {
40    state
41        .tenant_policy(tenant_id)
42        .ok_or_else(|| WsPrismError::BadRequest(format!("unknown tenant: {tenant_id}")))
43}