tfa: clippy fixes

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-11-26 14:55:23 +01:00 committed by Thomas Lamprecht
parent 637188d4ba
commit d85ebbb464
7 changed files with 31 additions and 29 deletions

View File

@ -142,7 +142,7 @@ pub fn get_tfa_entry(config: &TfaConfig, userid: &str, id: &str) -> Option<Typed
Some( Some(
match { match {
// scope to prevent the temporary iter from borrowing across the whole match // scope to prevent the temporary iter from borrowing across the whole match
let entry = tfa_id_iter(&user_data).find(|(_ty, _index, entry_id)| id == *entry_id); let entry = tfa_id_iter(user_data).find(|(_ty, _index, entry_id)| id == *entry_id);
entry.map(|(ty, index, _)| (ty, index)) entry.map(|(ty, index, _)| (ty, index))
} { } {
Some((TfaType::Recovery, _)) => match user_data.recovery() { Some((TfaType::Recovery, _)) => match user_data.recovery() {
@ -155,21 +155,20 @@ pub fn get_tfa_entry(config: &TfaConfig, userid: &str, id: &str) -> Option<Typed
Some((TfaType::Totp, index)) => { Some((TfaType::Totp, index)) => {
TypedTfaInfo { TypedTfaInfo {
ty: TfaType::Totp, ty: TfaType::Totp,
// `into_iter().nth()` to *move* out of it info: user_data.totp.get(index).unwrap().info.clone(),
info: user_data.totp.iter().nth(index).unwrap().info.clone(),
} }
} }
Some((TfaType::Webauthn, index)) => TypedTfaInfo { Some((TfaType::Webauthn, index)) => TypedTfaInfo {
ty: TfaType::Webauthn, ty: TfaType::Webauthn,
info: user_data.webauthn.iter().nth(index).unwrap().info.clone(), info: user_data.webauthn.get(index).unwrap().info.clone(),
}, },
Some((TfaType::U2f, index)) => TypedTfaInfo { Some((TfaType::U2f, index)) => TypedTfaInfo {
ty: TfaType::U2f, ty: TfaType::U2f,
info: user_data.u2f.iter().nth(index).unwrap().info.clone(), info: user_data.u2f.get(index).unwrap().info.clone(),
}, },
Some((TfaType::Yubico, index)) => TypedTfaInfo { Some((TfaType::Yubico, index)) => TypedTfaInfo {
ty: TfaType::Yubico, ty: TfaType::Yubico,
info: user_data.yubico.iter().nth(index).unwrap().info.clone(), info: user_data.yubico.get(index).unwrap().info.clone(),
}, },
None => return None, None => return None,
}, },
@ -195,7 +194,7 @@ pub fn delete_tfa(config: &mut TfaConfig, userid: &str, id: &str) -> Result<bool
match { match {
// scope to prevent the temporary iter from borrowing across the whole match // scope to prevent the temporary iter from borrowing across the whole match
let entry = tfa_id_iter(&user_data).find(|(_, _, entry_id)| id == *entry_id); let entry = tfa_id_iter(user_data).find(|(_, _, entry_id)| id == *entry_id);
entry.map(|(ty, index, _)| (ty, index)) entry.map(|(ty, index, _)| (ty, index))
} { } {
Some((TfaType::Recovery, _)) => user_data.recovery = None, Some((TfaType::Recovery, _)) => user_data.recovery = None,
@ -308,6 +307,7 @@ fn need_description(description: Option<String>) -> Result<String, Error> {
/// Permissions for accessing `userid` must have been verified by the caller. /// Permissions for accessing `userid` must have been verified by the caller.
/// ///
/// The caller must have already verified the user's password! /// The caller must have already verified the user's password!
#[allow(clippy::too_many_arguments)]
pub fn add_tfa_entry<A: OpenUserChallengeData>( pub fn add_tfa_entry<A: OpenUserChallengeData>(
config: &mut TfaConfig, config: &mut TfaConfig,
access: A, access: A,
@ -354,7 +354,7 @@ pub fn add_tfa_entry<A: OpenUserChallengeData>(
bail!("generating recovery tokens does not allow additional parameters"); bail!("generating recovery tokens does not allow additional parameters");
} }
let recovery = config.add_recovery(&userid)?; let recovery = config.add_recovery(userid)?;
Ok(TfaUpdateInfo { Ok(TfaUpdateInfo {
id: Some("recovery".to_string()), id: Some("recovery".to_string()),
@ -451,7 +451,7 @@ fn add_webauthn<A: OpenUserChallengeData>(
None => config None => config
.webauthn_registration_challenge( .webauthn_registration_challenge(
access, access,
&userid, userid,
need_description(description)?, need_description(description)?,
origin, origin,
) )
@ -464,7 +464,7 @@ fn add_webauthn<A: OpenUserChallengeData>(
format_err!("missing 'value' parameter (webauthn challenge response missing)") format_err!("missing 'value' parameter (webauthn challenge response missing)")
})?; })?;
config config
.webauthn_registration_finish(access, &userid, &challenge, &value, origin) .webauthn_registration_finish(access, userid, &challenge, &value, origin)
.map(TfaUpdateInfo::id) .map(TfaUpdateInfo::id)
} }
} }

View File

@ -247,13 +247,13 @@ impl TfaConfig {
TfaResponse::U2f(value) => match &challenge.u2f { TfaResponse::U2f(value) => match &challenge.u2f {
Some(challenge) => { Some(challenge) => {
let u2f = check_u2f(&self.u2f)?; let u2f = check_u2f(&self.u2f)?;
user.verify_u2f(access.clone(), userid, u2f, &challenge.challenge, value) user.verify_u2f(access, userid, u2f, &challenge.challenge, value)
} }
None => bail!("no u2f factor available for user '{}'", userid), None => bail!("no u2f factor available for user '{}'", userid),
}, },
TfaResponse::Webauthn(value) => { TfaResponse::Webauthn(value) => {
let webauthn = check_webauthn(&self.webauthn, origin)?; let webauthn = check_webauthn(&self.webauthn, origin)?;
user.verify_webauthn(access.clone(), userid, webauthn, value) user.verify_webauthn(access, userid, webauthn, value)
} }
TfaResponse::Recovery(value) => { TfaResponse::Recovery(value) => {
user.verify_recovery(&value)?; user.verify_recovery(&value)?;
@ -587,7 +587,7 @@ impl TfaUserData {
None => None, None => None,
}, },
u2f: match u2f { u2f: match u2f {
Some(u2f) => self.u2f_challenge(access.clone(), userid, u2f)?, Some(u2f) => self.u2f_challenge(access, userid, u2f)?,
None => None, None => None,
}, },
yubico: self.yubico.iter().any(|e| e.info.enable), yubico: self.yubico.iter().any(|e| e.info.enable),

View File

@ -12,7 +12,7 @@ fn getrandom(mut buffer: &mut [u8]) -> Result<(), io::Error> {
libc::getrandom( libc::getrandom(
buffer.as_mut_ptr() as *mut libc::c_void, buffer.as_mut_ptr() as *mut libc::c_void,
buffer.len() as libc::size_t, buffer.len() as libc::size_t,
0 as libc::c_uint, 0,
) )
}; };
@ -49,7 +49,7 @@ impl Recovery {
getrandom(&mut secret)?; getrandom(&mut secret)?;
let mut this = Self { let mut this = Self {
secret: hex::encode(&secret).to_string(), secret: hex::encode(&secret),
entries: Vec::with_capacity(10), entries: Vec::with_capacity(10),
created: proxmox_time::epoch_i64(), created: proxmox_time::epoch_i64(),
}; };

View File

@ -11,7 +11,7 @@ use serde::Deserialize;
pub struct FoldSeqVisitor<T, Out, F, Init> pub struct FoldSeqVisitor<T, Out, F, Init>
where where
Init: FnOnce(Option<usize>) -> Out, Init: FnOnce(Option<usize>) -> Out,
F: Fn(&mut Out, T) -> (), F: Fn(&mut Out, T),
{ {
init: Option<Init>, init: Option<Init>,
closure: F, closure: F,
@ -22,7 +22,7 @@ where
impl<T, Out, F, Init> FoldSeqVisitor<T, Out, F, Init> impl<T, Out, F, Init> FoldSeqVisitor<T, Out, F, Init>
where where
Init: FnOnce(Option<usize>) -> Out, Init: FnOnce(Option<usize>) -> Out,
F: Fn(&mut Out, T) -> (), F: Fn(&mut Out, T),
{ {
pub fn new(expecting: &'static str, init: Init, closure: F) -> Self { pub fn new(expecting: &'static str, init: Init, closure: F) -> Self {
Self { Self {
@ -37,7 +37,7 @@ where
impl<'de, T, Out, F, Init> serde::de::Visitor<'de> for FoldSeqVisitor<T, Out, F, Init> impl<'de, T, Out, F, Init> serde::de::Visitor<'de> for FoldSeqVisitor<T, Out, F, Init>
where where
Init: FnOnce(Option<usize>) -> Out, Init: FnOnce(Option<usize>) -> Out,
F: Fn(&mut Out, T) -> (), F: Fn(&mut Out, T),
T: Deserialize<'de>, T: Deserialize<'de>,
{ {
type Value = Out; type Value = Out;
@ -104,7 +104,7 @@ pub fn fold<'de, T, Out, Init, Fold>(
) -> FoldSeqVisitor<T, Out, Fold, Init> ) -> FoldSeqVisitor<T, Out, Fold, Init>
where where
Init: FnOnce(Option<usize>) -> Out, Init: FnOnce(Option<usize>) -> Out,
Fold: Fn(&mut Out, T) -> (), Fold: Fn(&mut Out, T),
T: Deserialize<'de>, T: Deserialize<'de>,
{ {
FoldSeqVisitor::new(expected, init, fold) FoldSeqVisitor::new(expected, init, fold)

View File

@ -41,9 +41,9 @@ impl std::ops::DerefMut for OriginUrl {
} }
} }
impl Into<String> for OriginUrl { impl From<OriginUrl> for String {
fn into(self) -> String { fn from(url: OriginUrl) -> String {
self.0.into() url.0.into()
} }
} }

View File

@ -20,9 +20,9 @@ pub enum Algorithm {
Sha512, Sha512,
} }
impl Into<MessageDigest> for Algorithm { impl From<Algorithm> for MessageDigest {
fn into(self) -> MessageDigest { fn from(algo: Algorithm) -> MessageDigest {
match self { match algo {
Algorithm::Sha1 => MessageDigest::sha1(), Algorithm::Sha1 => MessageDigest::sha1(),
Algorithm::Sha256 => MessageDigest::sha256(), Algorithm::Sha256 => MessageDigest::sha256(),
Algorithm::Sha512 => MessageDigest::sha512(), Algorithm::Sha512 => MessageDigest::sha512(),
@ -343,7 +343,7 @@ impl std::str::FromStr for Totp {
// FIXME: Also split on "%3A" / "%3a" // FIXME: Also split on "%3A" / "%3a"
let mut account = account.splitn(2, |&b| b == b':'); let mut account = account.splitn(2, |&b| b == b':');
let first_part = percent_decode( let first_part = percent_decode(
&account account
.next() .next()
.ok_or_else(|| anyhow!("missing account in otpauth uri"))?, .ok_or_else(|| anyhow!("missing account in otpauth uri"))?,
) )
@ -364,13 +364,13 @@ impl std::str::FromStr for Totp {
for parts in uri.split(|&b| b == b'&') { for parts in uri.split(|&b| b == b'&') {
let mut parts = parts.splitn(2, |&b| b == b'='); let mut parts = parts.splitn(2, |&b| b == b'=');
let key = percent_decode( let key = percent_decode(
&parts parts
.next() .next()
.ok_or_else(|| anyhow!("bad key in otpauth uri"))?, .ok_or_else(|| anyhow!("bad key in otpauth uri"))?,
) )
.decode_utf8()?; .decode_utf8()?;
let value = percent_decode( let value = percent_decode(
&parts parts
.next() .next()
.ok_or_else(|| anyhow!("bad value in otpauth uri"))?, .ok_or_else(|| anyhow!("bad value in otpauth uri"))?,
); );
@ -467,6 +467,8 @@ impl PartialEq<&str> for TotpValue {
return false; return false;
} }
// I don't trust that `.parse()` never starts accepting `0x` prefixes so:
#[allow(clippy::from_str_radix_10)]
match u32::from_str_radix(*other, 10) { match u32::from_str_radix(*other, 10) {
Ok(value) => self.value() == value, Ok(value) => self.value() == value,
Err(_) => false, Err(_) => false,

View File

@ -579,7 +579,7 @@ mod bytes_as_base64url_nopad {
pub fn serialize<S: Serializer>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error> { pub fn serialize<S: Serializer>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&base64::encode_config( serializer.serialize_str(&base64::encode_config(
data.as_ref(), data,
base64::URL_SAFE_NO_PAD, base64::URL_SAFE_NO_PAD,
)) ))
} }