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
impl TwoFactor
Sourcepub fn new(config: TwoFactorConfig) -> Result<Self, Error>
pub fn new(config: TwoFactorConfig) -> Result<Self, Error>
Sourcepub fn config(&self) -> &TwoFactorConfig
pub fn config(&self) -> &TwoFactorConfig
The config codes are made and checked with.
Sourcepub fn generate_secret(&self) -> Result<Secret, Error>
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.
Sourcepub fn url(&self, secret: &Secret, account: &str) -> Result<String, Error>
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 :.
Sourcepub fn qr_code_svg(
&self,
secret: &Secret,
account: &str,
) -> Result<String, Error>
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.
Sourcepub fn code_at(&self, secret: &Secret, time: u64) -> String
pub fn code_at(&self, secret: &Secret, time: u64) -> String
The code the app shows at time, in seconds since the Unix epoch.
Sourcepub fn verify(&self, secret: &Secret, code: &str) -> bool
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.
Sourcepub fn verify_at(&self, secret: &Secret, code: &str, time: u64) -> Option<u64>
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.
Sourcepub async fn attempt(&self, key: &str) -> Result<Result<Quota, TooManyAttempts>>
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.
Sourcepub async fn verify_once(
&self,
secret: &Secret,
code: &str,
key: &str,
) -> Result<bool>
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.