Skip to content

Commit

Permalink
feat(traits/authn): add log information
Browse files Browse the repository at this point in the history
  • Loading branch information
pandres95 committed Nov 18, 2024
1 parent 4f34f38 commit dfdd167
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ target/
*.pdb
.vscode
.DS_Store
.idea
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions traits/authn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version = "0.1.0"
codec.workspace = true
frame-support.workspace = true
fc-traits-authn-proc.workspace = true
log.workspace = true
scale-info.workspace = true

[features]
Expand Down
26 changes: 25 additions & 1 deletion traits/authn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod util;

pub use fc_traits_authn_proc::composite_authenticator;

const LOG_TARGET: &str = "authn";

pub mod composite_prelude {
pub use crate::{
Authenticator, AuthorityId, Challenge, Challenger, DeviceChallengeResponse, DeviceId,
Expand Down Expand Up @@ -36,7 +38,7 @@ macro_rules! composite_authenticators {
}
}

// A reasonabily sized secure challenge
// A reasonably sized secure challenge
const CHALLENGE_SIZE: usize = 32;
pub type Challenge = [u8; CHALLENGE_SIZE];
type CxOf<C> = <C as Challenger>::Context;
Expand Down Expand Up @@ -65,13 +67,24 @@ pub trait Authenticator {
type Device: UserAuthenticator<Challenger = Self::Challenger>;

fn verify_device(attestation: Self::DeviceAttestation) -> Option<Self::Device> {
log::trace!(target: LOG_TARGET, "Verifying device with attestation: {:?}", attestation);

log::trace!(target: LOG_TARGET, "Assert authority {:?}", attestation.authority());
attestation
.authority()
.eq(&Self::Authority::get())
.then_some(())?;
log::trace!(target: LOG_TARGET, "Authority verified");

let (cx, challenge) = attestation.used_challenge();
log::trace!(target: LOG_TARGET, "Check challenge {:?}", &challenge);
Self::Challenger::check_challenge(&cx, &challenge)?;
log::trace!(target: LOG_TARGET, "Challenge checked");

log::trace!(target: LOG_TARGET, "Validate attestation");
attestation.is_valid().then_some(())?;

log::trace!(target: LOG_TARGET, "Retrieve device");
Some(Self::unpack_device(attestation))
}

Expand All @@ -86,13 +99,24 @@ pub trait UserAuthenticator: FullCodec + MaxEncodedLen + TypeInfo {
type Credential: UserChallengeResponse<CxOf<Self::Challenger>>;

fn verify_user(&self, credential: &Self::Credential) -> Option<()> {
log::trace!(target: LOG_TARGET, "Verifying user for credential: {:?}", credential);

log::trace!(target: LOG_TARGET, "Assert authority {:?}", credential.authority());
credential
.authority()
.eq(&Self::Authority::get())
.then_some(())?;
log::trace!(target: LOG_TARGET, "Authority verified");

let (cx, challenge) = credential.used_challenge();
log::trace!(target: LOG_TARGET, "Check challenge {:?}", &challenge);
Self::Challenger::check_challenge(&cx, &challenge)?;
log::trace!(target: LOG_TARGET, "Challenge checked");

log::trace!(target: LOG_TARGET, "Credential verified");
credential.is_valid().then_some(())?;

log::trace!(target: LOG_TARGET, "Verify credential");
self.verify_credential(credential)
}

Expand Down
2 changes: 1 addition & 1 deletion traits/authn/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<Id: Get<PalletId>> Get<AuthorityId> for AuthorityFromPalletId<Id> {
}
}

/// Convenient auto-implemtator of the Authenticator trait
/// Convenient auto-implementor of the Authenticator trait
pub struct Auth<Dev, Att>(PhantomData<(Dev, Att)>);

impl<Dev, Att> Authenticator for Auth<Dev, Att>
Expand Down

0 comments on commit dfdd167

Please sign in to comment.