pub struct Ctx<D, A = Public> { /* private fields */ }Expand description
The per-request context. Cheap to clone; hooks and event handlers get their own copy.
Implementations§
Source§impl<D: Adapter, A> Ctx<D, A>
impl<D: Adapter, A> Ctx<D, A>
Sourcepub fn db_on<'t, Q: Queries>(&'t self, queries: &'t Q) -> Db<'t, Q>
pub fn db_on<'t, Q: Queries>(&'t self, queries: &'t Q) -> Db<'t, Q>
Typed queries inside a transaction: let tx = ctx.adapter().begin().await?; ctx.db_on(&tx).
Sourcepub fn adapter(&self) -> &D
pub fn adapter(&self) -> &D
The adapter this call runs on: the configured one, or the one a server API call
passed to Api::within (for sqlx, one bound to the app’s transaction).
pub fn schema(&self) -> &Schema
pub fn keys(&self) -> &Keys
Sourcepub fn oauth_token_bytes(&self, token: &str) -> Vec<u8> ⓘ
pub fn oauth_token_bytes(&self, token: &str) -> Vec<u8> ⓘ
The bytes an OAuth access or refresh token is stored as in account: encrypted with
the secret, or the token’s own bytes with
store_oauth_tokens_in_plaintext.
Account::read_access_token reads either.
pub fn config(&self) -> &CoreConfig
pub fn passwords(&self) -> &Passwords
pub fn now(&self) -> OffsetDateTime
Sourcepub fn new_id(&self) -> Result<Id, AuthError>
pub fn new_id(&self) -> Result<Id, AuthError>
A new id from the configured IdStrategy. Fails only
when a custom generator returns an invalid id: a 500 whose detail is logged.
pub fn request(&self) -> &RequestInfo
pub fn headers(&self) -> &HeaderMap
pub fn ip(&self) -> Option<&str>
pub fn user_agent(&self) -> Option<&str>
Sourcepub fn param(&self, name: &str) -> Option<&str>
pub fn param(&self, name: &str) -> Option<&str>
A path parameter, e.g. {token} in /reset-password/{token}.
Sourcepub fn plugin<P: Plugin>(&self) -> &P
pub fn plugin<P: Plugin>(&self) -> &P
A plugin’s config. Panics if it isn’t registered, which Plugin::Requires rules out
for dependencies. Use try_plugin for optional ones.
pub fn try_plugin<P: Plugin>(&self) -> Option<&P>
Sourcepub fn emit<E: Event>(&self, event: E)
pub fn emit<E: Event>(&self, event: E)
Queues an event; handlers run after the endpoint succeeds.
Sets {prefix}.{name}, signed with the cookie key.
Reads {prefix}.{name} and checks that the current secret signed it.
Reads {prefix}.{name} holding a session token or a marker that only restricts a
session, signed by any configured secret (see Keys::verify_token), so the cookie
survives a secret rotation.
Sourcepub fn redirect(&self, url: impl Into<String>)
pub fn redirect(&self, url: impl Into<String>)
Answer with a 302 to url instead of JSON. url must pass
CoreConfig::is_safe_redirect; Location is the URL it resolves to, and an unsafe
url answers 403 INVALID_CALLBACK_URL instead.
pub fn set_header(&self, name: HeaderName, value: HeaderValue)
Sourcepub fn internal(&self) -> Internal<'_, D, A>
pub fn internal(&self) -> Internal<'_, D, A>
Core operations plugins build on: users, sessions, sign-in, verification values, email.
Sourcepub async fn sensitive_session(&self) -> Result<SessionView, AuthError>
pub async fn sensitive_session(&self) -> Result<SessionView, AuthError>
The request’s session read again from the database, for endpoints that create or
change a credential (API keys, passkeys, linked accounts, two-factor). Skips the
cookie cache, so a revoked session is refused at once, and runs the session gates
with SessionUse::Sensitive, where plugins refuse sessions that shouldn’t mint
credentials (admin: impersonation sessions). 401 without a stored session, including
ephemeral ones such as an API key’s.
The request’s session as the database holds it now, for authorization decisions:
admin and organization roles and permissions, and endpoints that revoke sessions. A
session the cookie cache answered is read again and the session gates run on the
stored row, so a revoked session is refused and a changed role or a ban counts at once.
Unlike sensitive_session it runs the gates with
SessionUse::Request, so impersonation and resolver sessions (API keys) pass as
they do on any request. 401 without a session.
Sourcepub fn dont_remember(&self) -> bool
pub fn dont_remember(&self) -> bool
Whether the browser’s session cookie is meant to last only for the browser session:
the session this response sets came from a rememberMe: false sign-in or, when the
response sets none, the request carries the cookie of one.