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(
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))
} {
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)) => {
TypedTfaInfo {
ty: TfaType::Totp,
// `into_iter().nth()` to *move* out of it
info: user_data.totp.iter().nth(index).unwrap().info.clone(),
info: user_data.totp.get(index).unwrap().info.clone(),
}
}
Some((TfaType::Webauthn, index)) => TypedTfaInfo {
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 {
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 {
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,
},
@ -195,7 +194,7 @@ pub fn delete_tfa(config: &mut TfaConfig, userid: &str, id: &str) -> Result<bool
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))
} {
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.
///
/// The caller must have already verified the user's password!
#[allow(clippy::too_many_arguments)]
pub fn add_tfa_entry<A: OpenUserChallengeData>(
config: &mut TfaConfig,
access: A,
@ -354,7 +354,7 @@ pub fn add_tfa_entry<A: OpenUserChallengeData>(
bail!("generating recovery tokens does not allow additional parameters");
}
let recovery = config.add_recovery(&userid)?;
let recovery = config.add_recovery(userid)?;
Ok(TfaUpdateInfo {
id: Some("recovery".to_string()),
@ -451,7 +451,7 @@ fn add_webauthn<A: OpenUserChallengeData>(
None => config
.webauthn_registration_challenge(
access,
&userid,
userid,
need_description(description)?,
origin,
)
@ -464,7 +464,7 @@ fn add_webauthn<A: OpenUserChallengeData>(
format_err!("missing 'value' parameter (webauthn challenge response missing)")
})?;
config
.webauthn_registration_finish(access, &userid, &challenge, &value, origin)
.webauthn_registration_finish(access, userid, &challenge, &value, origin)
.map(TfaUpdateInfo::id)
}
}

View File

@ -247,13 +247,13 @@ impl TfaConfig {
TfaResponse::U2f(value) => match &challenge.u2f {
Some(challenge) => {
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),
},
TfaResponse::Webauthn(value) => {
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) => {
user.verify_recovery(&value)?;
@ -587,7 +587,7 @@ impl TfaUserData {
None => None,
},
u2f: match u2f {
Some(u2f) => self.u2f_challenge(access.clone(), userid, u2f)?,
Some(u2f) => self.u2f_challenge(access, userid, u2f)?,
None => None,
},
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(
buffer.as_mut_ptr() as *mut libc::c_void,
buffer.len() as libc::size_t,
0 as libc::c_uint,
0,
)
};
@ -49,7 +49,7 @@ impl Recovery {
getrandom(&mut secret)?;
let mut this = Self {
secret: hex::encode(&secret).to_string(),
secret: hex::encode(&secret),
entries: Vec::with_capacity(10),
created: proxmox_time::epoch_i64(),
};

View File

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

View File

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

View File

@ -20,9 +20,9 @@ pub enum Algorithm {
Sha512,
}
impl Into<MessageDigest> for Algorithm {
fn into(self) -> MessageDigest {
match self {
impl From<Algorithm> for MessageDigest {
fn from(algo: Algorithm) -> MessageDigest {
match algo {
Algorithm::Sha1 => MessageDigest::sha1(),
Algorithm::Sha256 => MessageDigest::sha256(),
Algorithm::Sha512 => MessageDigest::sha512(),
@ -343,7 +343,7 @@ impl std::str::FromStr for Totp {
// FIXME: Also split on "%3A" / "%3a"
let mut account = account.splitn(2, |&b| b == b':');
let first_part = percent_decode(
&account
account
.next()
.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'&') {
let mut parts = parts.splitn(2, |&b| b == b'=');
let key = percent_decode(
&parts
parts
.next()
.ok_or_else(|| anyhow!("bad key in otpauth uri"))?,
)
.decode_utf8()?;
let value = percent_decode(
&parts
parts
.next()
.ok_or_else(|| anyhow!("bad value in otpauth uri"))?,
);
@ -467,6 +467,8 @@ impl PartialEq<&str> for TotpValue {
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) {
Ok(value) => self.value() == value,
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> {
serializer.serialize_str(&base64::encode_config(
data.as_ref(),
data,
base64::URL_SAFE_NO_PAD,
))
}