pub struct AuthContext {
pub user_id: Option<String>,
pub is_admin: bool,
pub is_guest: bool,
pub roles: Vec<String>,
pub tenant_id: Option<String>,
pub api_key_id: Option<String>,
pub api_key_scopes: Option<String>,
}Expand description
The auth context for a request. Represents who is making the request.
Do NOT derive Deserialize on this type. If the server ever parses an
AuthContext from client-supplied JSON, a client can set is_admin=true
or add roles and bypass every policy. Identity must come from
server-minted sessions (Session::to_auth_context) or explicit
constructors, never from deserialization.
Serialize is safe because sending the resolved context BACK to the
client exposes nothing the server didn’t already decide.
Fields§
§user_id: Option<String>The authenticated user ID, or None for public/anonymous access.
For guest contexts this is Some(guest_id) — a stable
anonymous identifier, NOT a real user.
is_admin: boolWhether this is an admin context (bypasses policies).
is_guest: boolTrue for AuthContext::guest() — anonymous-with-stable-id, used
for cart state and similar pre-login persistence. Routes guarded
by AuthMode::User reject guests; only is_authenticated() ==
“real signed-in user” should pass auth-required gates.
roles: Vec<String>Roles granted to this user. Empty for anonymous.
tenant_id: Option<String>Active tenant id (for multi-tenant apps). Set when the user has selected an organization for the current session.
api_key_id: Option<String>API key id when the request was authenticated via a pk.…
bearer token. Set so policies + management endpoints can
distinguish “user-via-session” from “user-via-key” — e.g.
password change is forbidden via API key.
api_key_scopes: Option<String>Comma-separated scope string from the API key. Application policies decide what scopes mean — pylon only carries them.
Implementations§
Source§impl AuthContext
impl AuthContext
Sourcepub fn authenticated(user_id: String) -> Self
pub fn authenticated(user_id: String) -> Self
Create an authenticated auth context.
Sourcepub fn from_api_key(
user_id: String,
key_id: String,
scopes: Option<String>,
) -> Self
pub fn from_api_key( user_id: String, key_id: String, scopes: Option<String>, ) -> Self
Create an authenticated context backed by an API key. Policies +
auth-management endpoints can detect this via is_api_key_auth().
Sourcepub fn is_api_key_auth(&self) -> bool
pub fn is_api_key_auth(&self) -> bool
True iff this request was authenticated by an API key (not a session cookie / bearer session token).
Sourcepub fn guest(guest_id: String) -> Self
pub fn guest(guest_id: String) -> Self
Create a guest auth context with a persistent anonymous ID.
Guests carry an opaque stable id (cart/session continuity) but
are NOT considered authenticated — is_authenticated() returns
false and AuthMode::User rejects them.
Sourcepub fn tenant_id(&self) -> Option<&str>
pub fn tenant_id(&self) -> Option<&str>
Active tenant id (None when the user hasn’t selected an org).
Sourcepub fn with_tenant(self, tenant_id: String) -> Self
pub fn with_tenant(self, tenant_id: String) -> Self
Attach a tenant id to the context (chainable).
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Check if this context represents an authenticated user.
Guests intentionally return false — they have a stable anonymous
id but never gain user-level access.
Sourcepub fn has_role(&self, role: &str) -> bool
pub fn has_role(&self, role: &str) -> bool
Check if the user has a specific role. Admins have every role implicitly.
Sourcepub fn has_any_role(&self, roles: &[&str]) -> bool
pub fn has_any_role(&self, roles: &[&str]) -> bool
Check if the user has ANY of the given roles.
Sourcepub fn with_roles(self, roles: Vec<String>) -> Self
pub fn with_roles(self, roles: Vec<String>) -> Self
Attach roles to the context (chainable).
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 more