pub struct DeviceAuthenticator { /* private fields */ }Expand description
Device authenticator manages challenge-response authentication.
§Overview
The authenticator uses Ed25519 signatures for mutual authentication:
- Generate a challenge with random nonce and timestamp
- Peer signs the challenge and returns their public key
- Verify signature and cache the verified peer identity
§Example
ⓘ
use peat_protocol::security::{DeviceKeypair, DeviceAuthenticator};
let keypair = DeviceKeypair::generate();
let authenticator = DeviceAuthenticator::new(keypair);
// Generate challenge for peer
let challenge = authenticator.generate_challenge();
// Peer creates response
let response = peer_authenticator.respond_to_challenge(&challenge)?;
// Verify response
let peer_id = authenticator.verify_response(&response)?;
println!("Authenticated peer: {}", peer_id);Implementations§
Source§impl DeviceAuthenticator
impl DeviceAuthenticator
Sourcepub fn new(keypair: DeviceKeypair) -> Self
pub fn new(keypair: DeviceKeypair) -> Self
Create a new authenticator with the given keypair.
Sourcepub fn with_timeout(keypair: DeviceKeypair, challenge_timeout: Duration) -> Self
pub fn with_timeout(keypair: DeviceKeypair, challenge_timeout: Duration) -> Self
Create an authenticator with a custom challenge timeout.
Sourcepub fn public_key_bytes(&self) -> [u8; 32]
pub fn public_key_bytes(&self) -> [u8; 32]
Get this device’s public key bytes.
Sourcepub fn generate_challenge(&self) -> Challenge
pub fn generate_challenge(&self) -> Challenge
Generate a challenge for authenticating a peer.
The challenge contains:
- Random 32-byte nonce
- Current timestamp
- This device’s ID
- Expiration timestamp
Sourcepub fn respond_to_challenge(
&self,
challenge: &Challenge,
) -> Result<SignedChallengeResponse, SecurityError>
pub fn respond_to_challenge( &self, challenge: &Challenge, ) -> Result<SignedChallengeResponse, SecurityError>
Create a signed response to a challenge.
Signs the challenge data with this device’s private key.
Sourcepub fn verify_response(
&self,
response: &SignedChallengeResponse,
) -> Result<DeviceId, SecurityError>
pub fn verify_response( &self, response: &SignedChallengeResponse, ) -> Result<DeviceId, SecurityError>
Verify a peer’s challenge response.
On success, caches the peer’s identity and returns their DeviceId.
Sourcepub fn is_verified(&self, device_id: &DeviceId) -> bool
pub fn is_verified(&self, device_id: &DeviceId) -> bool
Check if a peer is verified.
Sourcepub fn get_verified_peer(&self, device_id: &DeviceId) -> Option<VerifiedPeer>
pub fn get_verified_peer(&self, device_id: &DeviceId) -> Option<VerifiedPeer>
Get a verified peer’s info.
Sourcepub fn remove_peer(&self, device_id: &DeviceId)
pub fn remove_peer(&self, device_id: &DeviceId)
Remove a peer from the verified cache.
Sourcepub fn clear_verified_peers(&self)
pub fn clear_verified_peers(&self)
Clear all verified peers.
Sourcepub fn verified_peer_count(&self) -> usize
pub fn verified_peer_count(&self) -> usize
Get number of verified peers.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for DeviceAuthenticator
impl RefUnwindSafe for DeviceAuthenticator
impl Send for DeviceAuthenticator
impl Sync for DeviceAuthenticator
impl Unpin for DeviceAuthenticator
impl UnsafeUnpin for DeviceAuthenticator
impl UnwindSafe for DeviceAuthenticator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more