-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
196 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
use std::marker::PhantomData; | ||
|
||
use codec::{Decode, Encode, FullCodec, MaxEncodedLen}; | ||
use frame_support::{sp_runtime::traits::Morph, traits::Get}; | ||
Check warning on line 4 in traits/authn/src/util.rs GitHub Actions / clippyunused import: `sp_runtime::traits::Morph`
|
||
use scale_info::TypeInfo; | ||
|
||
use crate::{ | ||
Authenticator, AuthorityId, Challenger, DeviceChallengeResponse, DeviceId, UserAuthenticator, | ||
UserChallengeResponse, | ||
}; | ||
|
||
type ChallengerOf<Dev> = <Dev as UserAuthenticator>::Challenger; | ||
type CxOf<C> = <C as Challenger>::Context; | ||
|
||
pub struct Auth<Dev, Att>(PhantomData<(Dev, Att)>); | ||
|
||
impl<Dev, Att> Authenticator for Auth<Dev, Att> | ||
where | ||
Att: DeviceChallengeResponse<CxOf<ChallengerOf<Dev>>>, | ||
Dev: UserAuthenticator + From<Att>, | ||
{ | ||
type Authority = <Dev as UserAuthenticator>::Authority; | ||
type Challenger = ChallengerOf<Dev>; | ||
type DeviceAttestation = Att; | ||
type Device = Dev; | ||
|
||
fn unpack_device(attestation: Self::DeviceAttestation) -> Self::Device { | ||
attestation.into() | ||
} | ||
} | ||
|
||
#[derive(Encode, Decode, TypeInfo)] | ||
#[scale_info(skip_type_params(A, Ch, Cred))] | ||
struct Dev<T, A, Ch, Cred>(T, PhantomData<(A, Ch, Cred)>); | ||
|
||
impl<T, A, Ch, Cred> UserAuthenticator for Dev<T, A, Ch, Cred> | ||
Check failure on line 36 in traits/authn/src/util.rs GitHub Actions / clippythe trait bound `util::Dev<T, A, Ch, Cred>: parity_scale_codec::MaxEncodedLen` is not satisfied
|
||
where | ||
T: AsRef<DeviceId> + FullCodec + MaxEncodedLen + TypeInfo, | ||
A: Get<AuthorityId>, | ||
Ch: Challenger, | ||
Cred: UserChallengeResponse<Ch::Context>, | ||
{ | ||
type Authority = A; | ||
type Challenger = Ch; | ||
type Credential = Cred; | ||
|
||
fn device_id(&self) -> &DeviceId { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
// TODO implement here | ||
mod pass_key { | ||
use codec::{Decode, Encode}; | ||
use scale_info::TypeInfo; | ||
|
||
use super::{Auth, Dev}; | ||
use crate::{DeviceChallengeResponse, DeviceId, UserChallengeResponse}; | ||
|
||
pub type PassKey<A> = Dev<(), A, (), PassKeyAssertion>; | ||
pub type PassKeyManager<A> = Auth<PassKey<A>, PassKeyAttestation>; | ||
|
||
#[derive(Clone, Debug, Decode, Encode, TypeInfo, PartialEq, Eq)] | ||
pub struct PassKeyAttestation; | ||
|
||
impl<Cx> DeviceChallengeResponse<Cx> for PassKeyAttestation { | ||
fn is_valid(&self) -> bool { | ||
todo!() | ||
} | ||
|
||
fn used_challenge(&self) -> (Cx, crate::Challenge) { | ||
todo!() | ||
} | ||
|
||
fn authority(&self) -> crate::AuthorityId { | ||
todo!() | ||
} | ||
|
||
fn device_id(&self) -> &DeviceId { | ||
todo!() | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq)] | ||
pub struct PassKeyAssertion; | ||
|
||
impl<Cx> UserChallengeResponse<Cx> for PassKeyAssertion { | ||
fn is_valid(&self) -> bool { | ||
todo!() | ||
} | ||
|
||
fn used_challenge(&self) -> (Cx, crate::Challenge) { | ||
todo!() | ||
} | ||
|
||
fn authority(&self) -> crate::AuthorityId { | ||
todo!() | ||
} | ||
|
||
fn user_id(&self) -> crate::HashedUserId { | ||
todo!() | ||
} | ||
} | ||
} | ||
|
||
mod dummy { | ||
use frame_support::{parameter_types, sp_runtime::str_array as s}; | ||
|
||
use crate::{ | ||
AuthorityId, Challenger, DeviceChallengeResponse, DeviceId, HashedUserId, | ||
UserChallengeResponse, | ||
}; | ||
|
||
use super::{Auth, Dev}; | ||
|
||
type DummyAttestation = bool; | ||
type DummyCredential = bool; | ||
type DummyChallenger = u8; | ||
type DummyCx = <DummyChallenger as Challenger>::Context; | ||
|
||
parameter_types! { | ||
const DummyAuthority: AuthorityId = s("dummy_authority"); | ||
} | ||
pub const DUMMY_DEV: DeviceId = s("dummy_device"); | ||
pub const DUMMY_USER: HashedUserId = s("dummy_user_hash"); | ||
|
||
impl Challenger for DummyChallenger { | ||
type Context = Self; | ||
fn generate(cx: &Self::Context) -> crate::Challenge { | ||
[*cx; 32] | ||
} | ||
} | ||
|
||
pub type DummyDev = Dev<DeviceId, DummyAttestation, DummyChallenger, DummyCredential>; | ||
pub type Dummy = Auth<DummyDev, DummyAttestation>; | ||
|
||
impl DeviceChallengeResponse<DummyCx> for DummyAttestation { | ||
fn device_id(&self) -> &DeviceId { | ||
&DUMMY_DEV | ||
} | ||
|
||
fn is_valid(&self) -> bool { | ||
*self | ||
} | ||
fn used_challenge(&self) -> (DummyCx, crate::Challenge) { | ||
(0, [0; 32]) | ||
} | ||
fn authority(&self) -> crate::AuthorityId { | ||
DummyAuthority::get() | ||
} | ||
} | ||
|
||
impl UserChallengeResponse<DummyCx> for DummyCredential { | ||
fn user_id(&self) -> crate::HashedUserId { | ||
DUMMY_USER | ||
} | ||
|
||
fn is_valid(&self) -> bool { | ||
*self | ||
} | ||
|
||
fn used_challenge(&self) -> (DummyCx, crate::Challenge) { | ||
(0, [0; 32]) | ||
} | ||
|
||
fn authority(&self) -> crate::AuthorityId { | ||
DummyAuthority::get() | ||
} | ||
} | ||
} |