Skip to content

Commit 6567b9c

Browse files
chore(ci): add code formatting checks (#180)
* Runs `cargo fmt` * Adds pre-commit hook to fail unformatted commits * Adds CI to enforce commit format if pre-commit hook is skipped
1 parent 5df814b commit 6567b9c

26 files changed

Lines changed: 129 additions & 106 deletions

.githooks/pre-commit

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Pre-commit hook: runs cargo fmt and fails if code is not formatted.
4+
# Install: git config core.hooksPath .githooks
5+
6+
set -eu
7+
8+
# Find the Cargo.toml relative to the repo root
9+
REPO_ROOT="$(git rev-parse --show-toplevel)"
10+
MANIFEST="$REPO_ROOT/libwebauthn/Cargo.toml"
11+
12+
if ! command -v cargo >/dev/null 2>&1; then
13+
echo "warning: cargo not found, skipping format check"
14+
exit 0
15+
fi
16+
17+
if ! cargo fmt --manifest-path "$MANIFEST" -- --check >/dev/null 2>&1; then
18+
echo "error: code is not formatted. Run 'cargo fmt --manifest-path libwebauthn/Cargo.toml' and re-commit."
19+
exit 1
20+
fi

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121
run: sudo apt-get install libudev-dev libdbus-1-dev libsodium-dev libnfc-dev libpcsclite-dev
2222
- name: Clippy
2323
run: cargo clippy --all-targets --all-features -- -D warnings
24+
- name: Check formatting
25+
run: cargo fmt -- --check
2426
- name: Build
2527
run: cargo build --all-targets --all-features
2628
- name: Run tests

libwebauthn/examples/prf_test.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
131131
eval_by_credential,
132132
};
133133

134-
run_success_test(
135-
&mut channel,
136-
&credential,
137-
&challenge,
138-
prf,
139-
"PRF output: ",
140-
)
141-
.await;
134+
run_success_test(&mut channel, &credential, &challenge, prf, "PRF output: ").await;
142135
}
143136
Ok(())
144137
}

libwebauthn/examples/u2f_ble.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
5555
// Signature ceremony
5656
println!("Signature request sent (timeout: {:?} seconds).", TIMEOUT);
5757
let new_key = response.as_registered_key()?;
58-
let sign_request =
59-
SignRequest::new(APP_ID, challenge, &new_key.key_handle, TIMEOUT, true);
58+
let sign_request = SignRequest::new(APP_ID, challenge, &new_key.key_handle, TIMEOUT, true);
6059
let response = channel.u2f_sign(&sign_request).await?;
6160
println!("Response: {:?}", response);
6261
}

libwebauthn/examples/u2f_hid.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
5858
// Signature ceremony
5959
println!("Signature request sent (timeout: {:?} seconds).", TIMEOUT);
6060
let new_key = response.as_registered_key()?;
61-
let sign_request =
62-
SignRequest::new(APP_ID, challenge, &new_key.key_handle, TIMEOUT, true);
61+
let sign_request = SignRequest::new(APP_ID, challenge, &new_key.key_handle, TIMEOUT, true);
6362
let response = channel.u2f_sign(&sign_request).await?;
6463
println!("Response: {:?}", response);
6564
}

libwebauthn/examples/webauthn_cable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::sync::Arc;
44
use std::time::Duration;
55

66
use libwebauthn::pin::PinRequestReason;
7-
use libwebauthn::transport::cable::is_available;
87
use libwebauthn::transport::cable::channel::{CableUpdate, CableUxUpdate};
8+
use libwebauthn::transport::cable::is_available;
99
use libwebauthn::transport::cable::known_devices::{
1010
CableKnownDevice, ClientPayloadHint, EphemeralDeviceInfoStore,
1111
};

libwebauthn/examples/webauthn_json_hid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use tokio::sync::broadcast::Receiver;
88
use tracing_subscriber::{self, EnvFilter};
99

1010
use libwebauthn::ops::webauthn::{
11-
GetAssertionRequest, JsonFormat, MakeCredentialRequest, RelyingPartyId,
12-
WebAuthnIDL as _, WebAuthnIDLResponse as _,
11+
GetAssertionRequest, JsonFormat, MakeCredentialRequest, RelyingPartyId, WebAuthnIDL as _,
12+
WebAuthnIDLResponse as _,
1313
};
1414
use libwebauthn::pin::PinRequestReason;
1515
use libwebauthn::transport::hid::list_devices;

libwebauthn/examples/webauthn_prf_hid.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
200200
eval,
201201
eval_by_credential,
202202
};
203-
run_success_test(
204-
&mut channel,
205-
&credential,
206-
&challenge,
207-
prf,
208-
"eval only",
209-
)
210-
.await;
203+
run_success_test(&mut channel, &credential, &challenge, prf, "eval only").await;
211204

212205
// Test 4: eval and a full list of eval_by_credential
213206
let eval = Some(PRFValue {

libwebauthn/src/fido.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ impl<'de, T: DeserializeOwned> Deserialize<'de> for AuthenticatorData<T> {
203203
.read_u8()
204204
.map_err(|e| DesError::custom(format!("failed to read flags: {e}")))?;
205205
let flags = AuthenticatorDataFlags::from_bits_truncate(flags_raw);
206-
let signature_count = cursor
207-
.read_u32::<BigEndian>()
208-
.map_err(|e| DesError::custom(format!("failed to read signature_count: {e}")))?;
206+
let signature_count = cursor.read_u32::<BigEndian>().map_err(|e| {
207+
DesError::custom(format!("failed to read signature_count: {e}"))
208+
})?;
209209

210210
let attested_credential =
211211
if flags.contains(AuthenticatorDataFlags::ATTESTED_CREDENTIALS) {
@@ -218,17 +218,16 @@ impl<'de, T: DeserializeOwned> Deserialize<'de> for AuthenticatorData<T> {
218218
cursor
219219
.read_exact(&mut aaguid)
220220
.map_err(|e| DesError::custom(format!("failed to read aaguid: {e}")))?;
221-
let credential_id_len = cursor
222-
.read_u16::<BigEndian>()
223-
.map_err(|e| DesError::custom(format!("failed to read credential_id_len: {e}")))?
224-
as usize;
221+
let credential_id_len = cursor.read_u16::<BigEndian>().map_err(|e| {
222+
DesError::custom(format!("failed to read credential_id_len: {e}"))
223+
})? as usize;
225224
if data.len() < 55 + credential_id_len {
226225
return Err(DesError::invalid_length(data.len(), &"55+L"));
227226
}
228227
let mut credential_id = vec![0u8; credential_id_len];
229-
cursor
230-
.read_exact(&mut credential_id)
231-
.map_err(|e| DesError::custom(format!("failed to read credential_id: {e}")))?;
228+
cursor.read_exact(&mut credential_id).map_err(|e| {
229+
DesError::custom(format!("failed to read credential_id: {e}"))
230+
})?;
232231

233232
let credential_public_key: PublicKey =
234233
cbor::from_cursor(&mut cursor).map_err(DesError::custom)?;

libwebauthn/src/management/authenticator_config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ impl Ctap2UserVerifiableRequest for Ctap2AuthenticatorConfigRequest {
181181
Ok(())
182182
}
183183

184-
185184
fn permissions(&self) -> Ctap2AuthTokenPermissionRole {
186185
Ctap2AuthTokenPermissionRole::AUTHENTICATOR_CONFIGURATION
187186
}

0 commit comments

Comments
 (0)