anonymous.rs

 1use crate::common::Identity;
 2use crate::server::{Mechanism, MechanismError, Response};
 3use alloc::format;
 4use alloc::vec::Vec;
 5
 6pub struct Anonymous;
 7
 8impl Anonymous {
 9    #[allow(clippy::new_without_default)]
10    pub fn new() -> Anonymous {
11        Anonymous
12    }
13}
14
15impl Mechanism for Anonymous {
16    fn name(&self) -> &str {
17        "ANONYMOUS"
18    }
19
20    fn respond(&mut self, payload: &[u8]) -> Result<Response, MechanismError> {
21        if !payload.is_empty() {
22            return Err(MechanismError::FailedToDecodeMessage);
23        }
24        let mut rand = [0u8; 16];
25        getrandom::fill(&mut rand)?;
26        let username = format!("{:02x?}", rand);
27        let ident = Identity::Username(username);
28        Ok(Response::Success(ident, Vec::new()))
29    }
30}