Skip to main content

Internal

Struct Internal 

Source
pub struct Internal<'a, D, A> { /* private fields */ }

Implementations§

Source§

impl<'a, D: Adapter, A> Internal<'a, D, A>

Source

pub async fn find_user_by_email( &self, email: &str, ) -> Result<Option<UserView>, AuthError>

Source

pub async fn find_user_by_id( &self, id: &Id, ) -> Result<Option<UserView>, AuthError>

Source

pub async fn create_user( &self, user: NewUser, account: Option<NewAccount>, ) -> Result<UserView, AuthError>

Creates a user (and optionally its first account) in one transaction. Answers USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL (422) when the email is taken.

Links an account like link_account, unless proof of the email claimed the user after user was read: then nothing is linked and the call answers 401. For links that a session or an earlier request started, such as linking a social account, so a link the previous holder started can’t land on a claimed user. The insert runs under lock_user_unless_claimed.

Source

pub async fn lock_user_unless_claimed( &self, user: &UserView, ) -> Result<D::Tx, AuthError>

Begins a transaction that holds user’s row locked, for a write that a session or an earlier read of user authorised: a password, a passkey, an API key, a two-factor enrollment, a linked account, a new email or profile fields. When proof of the email claimed the user since user was read (its security stamp moved), the transaction is rolled back and the call answers 401, so the write can’t land on the claimed user.

Make the write through Ctx::db_on on the transaction and commit it; dropping it rolls back. The lock is the transaction’s first statement, as it is in a claim, which locks the user row before it deletes anything: the two serialise, and with the user row always first they can’t deadlock. Hash passwords and send mail before calling this, not while holding the lock.

Source

pub fn accounts_of(&self, user_id: &Id) -> Where<Account>

The account rows of user_id that fastauth manages: all of them, or with only_owned_accounts on, only those of providers it owns (CoreConfig::owns_account_provider). Every query that lists, changes or deletes a user’s accounts goes through it.

Source

pub async fn refuse_foreign_accounts( &self, user_id: &Id, ) -> Result<(), AuthError>

Fails with 409 USER_HAS_FOREIGN_ACCOUNTS when user_id has account rows of a provider fastauth doesn’t own (with only_owned_accounts on). Deleting the user would delete them with it (the foreign key cascades), so paths that delete users check this first and leave the user alone.

Source

pub async fn find_account( &self, provider_id: &str, account_id: &str, ) -> Result<Option<Account>, AuthError>

Source

pub async fn credential_account( &self, user_id: &Id, ) -> Result<Option<Account>, AuthError>

Source

pub async fn user_fields( &self, extra: &Map<String, Value>, input: UserInput, ) -> Result<Vec<(&'static str, Value)>, AuthError>

The app and plugin columns in extra (a body’s extra camelCase keys), after every prepare_user_input hook, parsed like user_input_fields. Creating a user needs every required column; an update doesn’t.

Source

pub fn validate_new_password(&self, password: &str) -> Result<(), AuthError>

Checks a new password against the configured length limits and policy.

Source

pub async fn set_password( &self, user: &UserView, password: &str, ) -> Result<(), AuthError>

Validates, hashes and stores user’s password, creating the credential account if needed. Pass the user as the request authorised it (the session’s user, or the user a sign-in or a token was read for): the write runs under lock_user_unless_claimed, so when proof of the email claimed the user since, nothing is stored and the call answers 401.

Source

pub async fn create_session( &self, user_id: &Id, remember_me: bool, ) -> Result<SessionView, AuthError>

Creates a session and sets its cookies, running no sign-in gate at all. This is the low-level primitive: sign a user in with sign_in, and finish a challenged sign-in with complete_sign_in. Finishing one with this would skip every other plugin’s gates, so a banned user would get a session.

A remember_me: false session lives one day at most and is never extended. The session hooks run before the response gets the session: when one fails, the session is deleted and the response sets none of its cookies.

Source

pub async fn create_session_unless_claimed( &self, user: &UserView, remember_me: bool, ) -> Result<SessionView, AuthError>

create_session for a user that a session or an earlier read authorised, such as a passkey registered from a session: when proof of the email claimed the user since user was read, the session is deleted again before anyone sees its token and the call answers 401. Runs no sign-in gate.

Source

pub async fn create_session_with( &self, user_id: &Id, remember_me: bool, options: NewSession, ) -> Result<SessionView, AuthError>

create_session with an expiry or plugin columns of its own, stored in the one insert, so the session hooks and every later request see them.

Source

pub async fn find_session_by_token( &self, token: &str, ) -> Result<Option<SessionView>, AuthError>

The unexpired session token belongs to, with its user, without refreshing it. For plugins that hold session tokens of their own, such as one per signed-in account.

Source

pub async fn ephemeral_session( &self, user_id: &Id, ) -> Result<Option<SessionView>, AuthError>

The request-only session a resolver’s Resolved::User gets for this user, after the session gates. None when the user doesn’t exist; the gate’s error when one refuses the user (a banned user, with the admin plugin). For plugins that act for a user without a session, such as an API key’s owner.

Source

pub fn set_session_cookies(&self, token: &str, remember_me: bool)

Makes token the request’s session: sets the session cookie (for the configured lifetime, or for the browser session when remember_me is false) and drops the cookie cache, which belonged to the previous session.

Source

pub async fn sign_in( &self, user: UserView, method: impl Into<Cow<'static, str>>, remember_me: bool, ) -> Result<SignInResult, AuthError>

Runs every sign-in gate in registration order, then creates the session. The first gate that denies or challenges ends the sign-in without a session.

Read user before checking the credential it signs in with: when proof of the email claims the user after user was read, the sign-in answers 401 instead of a session (the security stamp changed), so a password check in flight during a claim can’t keep the previous holder signed in.

Source

pub async fn complete_sign_in( &self, challenger: &str, user: UserView, method: impl Into<Cow<'static, str>>, remember_me: bool, ) -> Result<SignInResult, AuthError>

Finishes a sign-in that a gate of plugin challenger answered with Gate::Challenge, once the user has met the challenge (e.g. a correct TOTP code). Pass the plugin’s own id (TwoFactor::ID, or "app" for a gate the app registered) and the method the sign-in started with.

Runs every gate except challenger’s, in registration order, whether they come before or after it, then creates the session. A remaining gate can still deny, which returns its error with no session. It can also challenge, which returns SignInResult::Challenge with no session; answer with its body. That gate’s plugin finishes the sign-in with its own complete_sign_in, which runs challenger’s gates again, so two gates that both challenge the same user keep challenging each other.

A 500 when no gate belongs to challenger: a wrong id would run the challenger’s own gate again and challenge forever.

Source

pub async fn claim_user( &self, user: UserView, link: Option<NewAccount>, ) -> Result<UserView, AuthError>

Makes whoever just proved control of user’s email its only owner. Every path that proves an email calls this: a magic link, an email code, a password-reset link, a verified social profile, and a verification link that came without the password (see prove_email).

For a user whose email isn’t verified, one transaction changes the security stamp, marks the email verified, deletes every account (password and OAuth links), session and core token (verification and reset links) the user collected before the proof, runs every plugin’s on_claim cleanup (passkeys, API keys, two-factor), and links link, the account the proof came with, if any. A failing cleanup rolls the whole claim back and fails the call. Sign-ins and links that started before the claim can’t finish after it, because the stamp changed. UserClaimed and EmailVerified are emitted after the commit, for notifications only.

A verified user comes back unchanged, with link linked: whoever proved the email before already owns it.

Source

pub async fn claim_unverified_user( &self, user: UserView, ) -> Result<UserView, AuthError>

👎Deprecated since 0.6.0:

use claim_user(user, None)

claim_user without an account to link, the 0.5 name.

Source

pub async fn prove_email(&self, user: UserView) -> Result<UserView, AuthError>

The request proved control of user’s email with a verification link or code. When it also proves it holds the user’s password (it carries the user’s session, or the signed cookie that sign-up or a refused unverified sign-in left in the browser that knew the password), the email is marked verified and nothing else changes. Otherwise whoever set the password may not own the mailbox, so the user is claimed (claim_user): the password and everything else from before the proof is gone, and the owner signs in by link or sets a password with a reset.

Source

pub fn remember_password_holder(&self, user: &UserView)

Leaves the signed cookie prove_email accepts as proof that this browser knows user’s password, for as long as a verification link lives. Sign-up and a sign-in refused for an unverified email call this.

Source

pub async fn create_user_verification( &self, purpose: &str, user: &UserView, ttl: Duration, ) -> Result<String, AuthError>

Stores a single-use token for user under {purpose}:{user_id}, bound to the user’s security stamp and email: consume_user_verification refuses it once a claim or a change of email happened since. For links mailed to the user, such as email verification and password reset.

Source

pub async fn consume_user_verification( &self, purpose: &str, token: &str, ) -> Result<Option<UserView>, AuthError>

Consumes a token from create_user_verification for purpose and answers its user. None when the token is unknown, expired, for another purpose, or minted before the user was claimed or changed email.

Source

pub async fn revoke_session( &self, user_id: &Id, session_id: &Id, ) -> Result<bool, AuthError>

Source

pub async fn revoke_user_sessions( &self, user_id: &Id, keep: Option<&Id>, ) -> Result<u64, AuthError>

Deletes every session of a user, except keep when given.

Source

pub fn clear_session_cookies(&self)

Source

pub async fn create_verification( &self, identifier: &str, ttl: Duration, data: Option<String>, ) -> Result<String, AuthError>

Stores a single-use value and returns it. Only its hash is kept.

Source

pub async fn store_verification_value( &self, identifier: &str, value: &str, ttl: Duration, data: Option<String>, ) -> Result<(), AuthError>

Stores a caller-chosen value (e.g. a 6-digit OTP) under identifier, replacing older ones.

Source

pub async fn consume_verification_value( &self, identifier: &str, value: &str, ) -> Result<Option<Verification>, AuthError>

Consumes a value stored with store_verification_value.

Source

pub async fn find_verification( &self, token: &str, ) -> Result<Option<Verification>, AuthError>

The unexpired verification for token, without consuming it.

Source

pub async fn consume_verification( &self, token: &str, ) -> Result<Option<Verification>, AuthError>

Finds and deletes the verification for token. None if unknown or expired.

Source

pub fn can_send_email(&self) -> bool

Whether a mailer is configured, so send_email can succeed.

Source

pub async fn send_email(&self, email: AuthEmail) -> Result<(), AuthError>

Source

pub async fn send_email_in_background( &self, email: AuthEmail, ) -> Result<(), AuthError>

Sends email without waiting for the mailer, so the route answers in the same time whether or not it sent anything. Use it where sending depends on whether an account exists. A failed send is logged (tracing::error!), not returned.

Source

pub async fn simulate_verification(&self) -> Result<(), AuthError>

The database work of creating and looking up a verification token, for the branch of a route that sends nothing, so both branches take about as long.

Source

pub async fn send_verification_email( &self, user: &UserView, callback_url: Option<&str>, ) -> Result<(), AuthError>

Creates a verification token and emails the link in the background (see send_email_in_background).

Auto Trait Implementations§

§

impl<'a, D, A> !RefUnwindSafe for Internal<'a, D, A>

§

impl<'a, D, A> !UnwindSafe for Internal<'a, D, A>

§

impl<'a, D, A> Forget for Internal<'a, D, A>

§

impl<'a, D, A> Freeze for Internal<'a, D, A>
where &'a Ctx<D, A>: Freeze,

§

impl<'a, D, A> Move for Internal<'a, D, A>

§

impl<'a, D, A> Send for Internal<'a, D, A>
where &'a Ctx<D, A>: Send,

§

impl<'a, D, A> Sync for Internal<'a, D, A>
where &'a Ctx<D, A>: Sync,

§

impl<'a, D, A> Unpin for Internal<'a, D, A>
where &'a Ctx<D, A>: Unpin,

§

impl<'a, D, A> UnsafeUnpin for Internal<'a, D, A>
where &'a Ctx<D, A>: UnsafeUnpin,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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