aboutsummaryrefslogtreecommitdiffstats
path: root/src/api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api.rs')
-rw-r--r--src/api.rs211
1 files changed, 109 insertions, 102 deletions
diff --git a/src/api.rs b/src/api.rs
index 85a0ce5..bf608b3 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -58,27 +58,27 @@ pub enum TwoFactorProviderType {
}
impl TwoFactorProviderType {
+ #[must_use]
pub fn message(&self) -> &str {
match *self {
- TwoFactorProviderType::Authenticator => "Enter the 6 digit verification code from your authenticator app.",
- TwoFactorProviderType::Email => "Enter the PIN you received via email.",
+ Self::Authenticator => "Enter the 6 digit verification code from your authenticator app.",
+ Self::Email => "Enter the PIN you received via email.",
_ => "Enter the code."
}
}
+ #[must_use]
pub fn header(&self) -> &str {
match *self {
- TwoFactorProviderType::Authenticator => "Authenticator App",
- TwoFactorProviderType::Email => "Email Code",
- _ => "Two Factor Authentication"
+ Self::Authenticator => "Authenticator App",
+ Self::Email => "Email Code",
+ _ => "Two Factor Authentication",
}
}
+ #[must_use]
pub fn grab(&self) -> bool {
- match *self {
- TwoFactorProviderType::Email => false,
- _ => true
- }
+ !matches!(self, Self::Email)
}
}
@@ -162,6 +162,96 @@ impl std::str::FromStr for TwoFactorProviderType {
}
}
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub enum KdfType {
+ Pbkdf2 = 0,
+ Argon2id = 1,
+}
+
+impl<'de> serde::Deserialize<'de> for KdfType {
+ fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ struct KdfTypeVisitor;
+ impl<'de> serde::de::Visitor<'de> for KdfTypeVisitor {
+ type Value = KdfType;
+
+ fn expecting(
+ &self,
+ formatter: &mut std::fmt::Formatter,
+ ) -> std::fmt::Result {
+ formatter.write_str("kdf id")
+ }
+
+ fn visit_str<E>(
+ self,
+ value: &str,
+ ) -> std::result::Result<Self::Value, E>
+ where
+ E: serde::de::Error,
+ {
+ value.parse().map_err(serde::de::Error::custom)
+ }
+
+ fn visit_u64<E>(
+ self,
+ value: u64,
+ ) -> std::result::Result<Self::Value, E>
+ where
+ E: serde::de::Error,
+ {
+ std::convert::TryFrom::try_from(value)
+ .map_err(serde::de::Error::custom)
+ }
+ }
+
+ deserializer.deserialize_any(KdfTypeVisitor)
+ }
+}
+
+impl std::convert::TryFrom<u64> for KdfType {
+ type Error = Error;
+
+ fn try_from(ty: u64) -> Result<Self> {
+ match ty {
+ 0 => Ok(Self::Pbkdf2),
+ 1 => Ok(Self::Argon2id),
+ _ => Err(Error::InvalidKdfType {
+ ty: format!("{ty}"),
+ }),
+ }
+ }
+}
+
+impl std::str::FromStr for KdfType {
+ type Err = Error;
+
+ fn from_str(ty: &str) -> Result<Self> {
+ match ty {
+ "0" => Ok(Self::Pbkdf2),
+ "1" => Ok(Self::Argon2id),
+ _ => Err(Error::InvalidKdfType { ty: ty.to_string() }),
+ }
+ }
+}
+
+impl serde::Serialize for KdfType {
+ fn serialize<S>(
+ &self,
+ serializer: S,
+ ) -> std::result::Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ let s = match self {
+ Self::Pbkdf2 => "0",
+ Self::Argon2id => "1",
+ };
+ serializer.serialize_str(s)
+ }
+}
+
#[derive(serde::Serialize, Debug)]
struct PreloginReq {
email: String,
@@ -634,7 +724,10 @@ impl Client {
}
}
- pub async fn prelogin(&self, email: &str) -> Result<(KdfType, u32, Option<u32>, Option<u32>)> {
+ pub async fn prelogin(
+ &self,
+ email: &str,
+ ) -> Result<(KdfType, u32, Option<u32>, Option<u32>)> {
let prelogin = PreloginReq {
email: email.to_string(),
};
@@ -646,7 +739,12 @@ impl Client {
.await
.map_err(|source| Error::Reqwest { source })?;
let prelogin_res: PreloginRes = res.json_with_path().await?;
- Ok((prelogin_res.kdf, prelogin_res.kdf_iterations, prelogin_res.kdf_memory, prelogin_res.kdf_parallelism))
+ Ok((
+ prelogin_res.kdf,
+ prelogin_res.kdf_iterations,
+ prelogin_res.kdf_memory,
+ prelogin_res.kdf_parallelism,
+ ))
}
pub async fn register(
@@ -1218,94 +1316,3 @@ fn classify_login_error(error_res: &ConnectErrorRes, code: u16) -> Error {
log::warn!("unexpected error received during login: {:?}", error_res);
Error::RequestFailed { status: code }
}
-
-
-#[derive(Debug, Copy, Clone, PartialEq, Eq)]
-pub enum KdfType {
- Pbkdf2 = 0,
- Argon2id = 1,
-}
-
-impl<'de> serde::Deserialize<'de> for KdfType {
- fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
- where
- D: serde::Deserializer<'de>,
- {
- struct KdfTypeVisitor;
- impl<'de> serde::de::Visitor<'de> for KdfTypeVisitor {
- type Value = KdfType;
-
- fn expecting(
- &self,
- formatter: &mut std::fmt::Formatter,
- ) -> std::fmt::Result {
- formatter.write_str("two factor provider id")
- }
-
- fn visit_str<E>(
- self,
- value: &str,
- ) -> std::result::Result<Self::Value, E>
- where
- E: serde::de::Error,
- {
- value.parse().map_err(serde::de::Error::custom)
- }
-
- fn visit_u64<E>(
- self,
- value: u64,
- ) -> std::result::Result<Self::Value, E>
- where
- E: serde::de::Error,
- {
- std::convert::TryFrom::try_from(value)
- .map_err(serde::de::Error::custom)
- }
- }
-
- deserializer.deserialize_any(KdfTypeVisitor)
- }
-}
-
-impl std::convert::TryFrom<u64> for KdfType {
- type Error = Error;
-
- fn try_from(ty: u64) -> Result<Self> {
- match ty {
- 0 => Ok(Self::Pbkdf2),
- 1 => Ok(Self::Argon2id),
- _ => Err(Error::InvalidTwoFactorProvider {
- ty: format!("{ty}"),
- }),
- }
- }
-}
-
-impl std::str::FromStr for KdfType {
- type Err = Error;
-
- fn from_str(ty: &str) -> Result<Self> {
- match ty {
- "0" => Ok(Self::Pbkdf2),
- "1" => Ok(Self::Argon2id),
- _ => Err(Error::InvalidTwoFactorProvider { ty: ty.to_string() }),
- }
- }
-}
-
-impl serde::Serialize for KdfType {
- fn serialize<S>(
- &self,
- serializer: S,
- ) -> std::result::Result<S::Ok, S::Error>
- where
- S: serde::Serializer,
- {
- let s = match self {
- Self::Pbkdf2 => "0",
- Self::Argon2id => "1",
- };
- serializer.serialize_str(s)
- }
-}