Skip to main content

AuthContext

Struct AuthContext 

Source
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: bool

Whether this is an admin context (bypasses policies).

§is_guest: bool

True 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

Source

pub fn anonymous() -> Self

Create an anonymous/public auth context.

Source

pub fn authenticated(user_id: String) -> Self

Create an authenticated auth context.

Source

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().

Source

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).

Source

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.

Source

pub fn admin() -> Self

Create an admin auth context that bypasses all policies.

Source

pub fn user(user_id: String) -> Self

Convenience: build a user context from a user id.

Source

pub fn tenant_id(&self) -> Option<&str>

Active tenant id (None when the user hasn’t selected an org).

Source

pub fn with_tenant(self, tenant_id: String) -> Self

Attach a tenant id to the context (chainable).

Source

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.

Source

pub fn has_role(&self, role: &str) -> bool

Check if the user has a specific role. Admins have every role implicitly.

Source

pub fn has_any_role(&self, roles: &[&str]) -> bool

Check if the user has ANY of the given roles.

Source

pub fn with_roles(self, roles: Vec<String>) -> Self

Attach roles to the context (chainable).

Trait Implementations§

Source§

impl Clone for AuthContext

Source§

fn clone(&self) -> AuthContext

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AuthContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for AuthContext

Source§

fn eq(&self, other: &AuthContext) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for AuthContext

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for AuthContext

Source§

impl StructuralPartialEq for AuthContext

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more