Skip to main content

TwoFactor

Struct TwoFactor 

Source
pub struct TwoFactor { /* private fields */ }
Expand description

Makes secrets, QR codes and codes, and checks the codes users type.

As a handler argument, or from from_app, it carries the application’s TwoFactorConfig.

Implementations§

Source§

impl TwoFactor

Source

pub fn new(config: TwoFactorConfig) -> Result<Self, Error>

Two-factor with the given config.

§Errors

Fails when the config doesn’t validate.

Source

pub fn from_app() -> Result<Self, Error>

Two-factor with the application’s config, or the defaults outside the application.

§Errors

Fails when the application’s config doesn’t validate.

Source

pub fn config(&self) -> &TwoFactorConfig

The config codes are made and checked with.

Source

pub fn generate_secret(&self) -> Result<Secret, Error>

A new random secret of TwoFactorConfig::secret_bytes bytes.

§Errors

Fails when the operating system can’t provide random bytes.

Source

pub fn url(&self, secret: &Secret, account: &str) -> Result<String, Error>

The otpauth://totp/ URL an authenticator app adds an account from. account is the name the app shows under the issuer, usually the user’s email address.

§Errors

Fails with Error::InvalidAccount when the account holds a :.

Source

pub fn qr_code_svg( &self, secret: &Secret, account: &str, ) -> Result<String, Error>

The url as a QR code: an SVG image at least 192 pixels wide, black on white.

§Errors

Fails with Error::InvalidAccount when the account holds a :, and with Error::QrCode when the URL is too long for a QR code.

Source

pub fn code(&self, secret: &Secret) -> String

The code the user’s app shows now.

Source

pub fn code_at(&self, secret: &Secret, time: u64) -> String

The code the app shows at time, in seconds since the Unix epoch.

Source

pub fn verify(&self, secret: &Secret, code: &str) -> bool

Whether code is the app’s code now, or within TwoFactorConfig::window periods of it. Spaces and hyphens are ignored. Use verify_once for logins.

Source

pub fn verify_at(&self, secret: &Secret, code: &str, time: u64) -> Option<u64>

The counter (period) code belongs to when it passes at time, in seconds since the Unix epoch. When two counters share the digits, it’s the latest.

Source

pub async fn attempt(&self, key: &str) -> Result<Result<Quota, TooManyAttempts>>

Counts a try at a code or a recovery code for key, such as the user’s ID: five at once, then one more every 12 seconds. In a handler, ?? turns a try over the limit into a 429.

§Errors

Fails when the default store is a discard or failover store, or when the cache is unavailable.

§Panics

Panics outside the application, which the cache belongs to.

Source

pub async fn verify_once( &self, secret: &Secret, code: &str, key: &str, ) -> Result<bool>

Like verify, but each counter (period) passes once per key, such as the user’s ID. Once a code passes, it and every earlier code in the window fail.

The marks live in TwoFactorConfig::cache_store. A mark the store loses early lets its code pass once more. With the database store, a call inside a transaction on the cache’s connection is final only once that transaction commits.

§Errors

Fails when the store is a discard or failover store, or when the cache is unavailable.

§Panics

Panics outside the application, which the cache belongs to.

Trait Implementations§

Source§

impl Clone for TwoFactor

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TwoFactor

Source§

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

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

impl Default for TwoFactor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S: Send + Sync> FromRequestParts<S> for TwoFactor

Takes TwoFactor as a handler argument, with the application’s config.

Source§

type Rejection = Error

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( _parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.

Auto Trait Implementations§

§

impl Forget for TwoFactor

§

impl Freeze for TwoFactor
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for TwoFactor

§

impl RefUnwindSafe for TwoFactor

§

impl Send for TwoFactor
where RawVecInner: Send,

§

impl Sync for TwoFactor
where RawVecInner: Sync,

§

impl Unpin for TwoFactor
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for TwoFactor

§

impl UnwindSafe for TwoFactor
where RawVecInner: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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, N> NotifiedBy<T> for N
where T: Notification<N>,

Source§

fn __routes(&self) -> Vec<Route>

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

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