Add TOTP::from_url_unchecked variant with uses TOTP::new_unchecked
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
diff --git a/src/lib.rs b/src/lib.rs
index 94024f8..0df86c9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -442,6 +442,32 @@ impl TOTP {
/// Generate a TOTP from the standard otpauth URL
#[cfg(feature = "otpauth")]
pub fn from_url<S: AsRef<str>>(url: S) -> Result<TOTP, TotpUrlError> {
+ let (algorithm, digits, skew, step, secret, issuer, account_name) =
+ Self::parts_from_url(url)?;
+ TOTP::new(algorithm, digits, skew, step, secret, issuer, account_name)
+ }
+
+ /// Generate a TOTP from the standard otpauth URL, using `TOTP::new_unchecked` internally
+ #[cfg(feature = "otpauth")]
+ pub fn from_url_unchecked<S: AsRef<str>>(url: S) -> Result<TOTP, TotpUrlError> {
+ let (algorithm, digits, skew, step, secret, issuer, account_name) =
+ Self::parts_from_url(url)?;
+ Ok(TOTP::new_unchecked(
+ algorithm,
+ digits,
+ skew,
+ step,
+ secret,
+ issuer,
+ account_name,
+ ))
+ }
+
+ /// Parse the TOTP parts from the standard otpauth URL
+ #[cfg(feature = "otpauth")]
+ fn parts_from_url<S: AsRef<str>>(
+ url: S,
+ ) -> Result<(Algorithm, usize, u8, u64, Vec<u8>, Option<String>, String), TotpUrlError> {
let url = Url::parse(url.as_ref()).map_err(TotpUrlError::Url)?;
if url.scheme() != "otpauth" {
return Err(TotpUrlError::Scheme(url.scheme().to_string()));
@@ -521,7 +547,7 @@ impl TOTP {
return Err(TotpUrlError::Secret("".to_string()));
}
- TOTP::new(algorithm, digits, 1, step, secret, issuer, account_name)
+ Ok((algorithm, digits, 1, step, secret, issuer, account_name))
}
/// Will generate a standard URL used to automatically add TOTP auths. Usually used with qr codes