pub struct AuthContext {
pub user_id: String,
pub session_id: String,
pub roles: Vec<String>,
pub metadata: Value,
}Expand description
Per-connection authentication context, populated during WS upgrade.
This context is extracted from HTTP cookies (or other auth mechanisms) during the WebSocket handshake and attached to the connection. Every RPC call on that connection has access to this context.
§Multi-tenancy with Keycloak
When using Keycloak for multi-tenancy, the AuthContext typically contains:
user_id: Keycloak user ID (sub claim from JWT)session_id: Keycloak session IDroles: User roles within the tenant/realmmetadata: Additional JWT claims (realm, tenant ID, custom attributes)
Fields§
§user_id: StringUser identifier (e.g., Keycloak sub claim, user UUID)
session_id: StringSession identifier (e.g., Keycloak session ID)
roles: Vec<String>User roles (e.g., [“user”, “admin”], Keycloak realm roles)
metadata: ValueAdditional metadata (e.g., JWT claims, tenant/realm info, custom attributes) For Keycloak multi-tenancy, this typically includes:
realm: Keycloak realm nametenant_id: Organization/tenant identifier- Any custom claims from the JWT token
Implementations§
Source§impl AuthContext
impl AuthContext
Sourcepub fn new(
user_id: String,
session_id: String,
roles: Vec<String>,
metadata: Value,
) -> Self
pub fn new( user_id: String, session_id: String, roles: Vec<String>, metadata: Value, ) -> Self
Create a new AuthContext
Sourcepub fn anonymous() -> Self
pub fn anonymous() -> Self
Create an anonymous/unauthenticated context
This can be used as a fallback when methods accept Option<&AuthContext>
and no authentication was provided.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Check if this context represents an authenticated user
Sourcepub fn get_metadata_string(&self, key: &str) -> Option<String>
pub fn get_metadata_string(&self, key: &str) -> Option<String>
Get a metadata field as a string
Trait Implementations§
Source§impl Clone for AuthContext
impl Clone for AuthContext
Source§fn clone(&self) -> AuthContext
fn clone(&self) -> AuthContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AuthContext
impl Debug for AuthContext
Source§impl<'de> Deserialize<'de> for AuthContext
impl<'de> Deserialize<'de> for AuthContext
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl JsonSchema for AuthContext
impl JsonSchema for AuthContext
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read more