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,
) -> AuthContext
pub fn new( user_id: String, session_id: String, roles: Vec<String>, metadata: Value, ) -> AuthContext
Create a new AuthContext
Sourcepub fn anonymous() -> AuthContext
pub fn anonymous() -> AuthContext
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<AuthContext, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<AuthContext, <__D as Deserializer<'de>>::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 moreSource§impl Serialize for AuthContext
impl Serialize for AuthContext
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for AuthContext
impl RefUnwindSafe for AuthContext
impl Send for AuthContext
impl Sync for AuthContext
impl Unpin for AuthContext
impl UnsafeUnpin for AuthContext
impl UnwindSafe for AuthContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more