distant_auth/
authenticator.rs1use std::io;
2
3use async_trait::async_trait;
4
5use crate::handler::AuthHandler;
6use crate::msg::*;
7
8#[async_trait]
10pub trait Authenticate {
11 async fn authenticate(&mut self, mut handler: impl AuthHandler + Send) -> io::Result<()>;
13}
14
15#[async_trait]
17pub trait Authenticator: Send {
18 async fn initialize(
21 &mut self,
22 initialization: Initialization,
23 ) -> io::Result<InitializationResponse>;
24
25 async fn challenge(&mut self, challenge: Challenge) -> io::Result<ChallengeResponse>;
27
28 async fn verify(&mut self, verification: Verification) -> io::Result<VerificationResponse>;
30
31 async fn info(&mut self, info: Info) -> io::Result<()>;
33
34 async fn error(&mut self, error: Error) -> io::Result<()>;
37
38 async fn start_method(&mut self, start_method: StartMethod) -> io::Result<()>;
40
41 async fn finished(&mut self) -> io::Result<()>;
44}
45
46#[cfg(any(test, feature = "tests"))]
48pub struct TestAuthenticator {
49 pub initialize: Box<dyn FnMut(Initialization) -> io::Result<InitializationResponse> + Send>,
50 pub challenge: Box<dyn FnMut(Challenge) -> io::Result<ChallengeResponse> + Send>,
51 pub verify: Box<dyn FnMut(Verification) -> io::Result<VerificationResponse> + Send>,
52 pub info: Box<dyn FnMut(Info) -> io::Result<()> + Send>,
53 pub error: Box<dyn FnMut(Error) -> io::Result<()> + Send>,
54 pub start_method: Box<dyn FnMut(StartMethod) -> io::Result<()> + Send>,
55 pub finished: Box<dyn FnMut() -> io::Result<()> + Send>,
56}
57
58#[cfg(any(test, feature = "tests"))]
59impl Default for TestAuthenticator {
60 fn default() -> Self {
61 Self {
62 initialize: Box::new(|x| Ok(InitializationResponse { methods: x.methods })),
63 challenge: Box::new(|x| {
64 Ok(ChallengeResponse {
65 answers: x.questions.into_iter().map(|x| x.text).collect(),
66 })
67 }),
68 verify: Box::new(|_| Ok(VerificationResponse { valid: true })),
69 info: Box::new(|_| Ok(())),
70 error: Box::new(|_| Ok(())),
71 start_method: Box::new(|_| Ok(())),
72 finished: Box::new(|| Ok(())),
73 }
74 }
75}
76
77#[cfg(any(test, feature = "tests"))]
78#[async_trait]
79impl Authenticator for TestAuthenticator {
80 async fn initialize(
81 &mut self,
82 initialization: Initialization,
83 ) -> io::Result<InitializationResponse> {
84 (self.initialize)(initialization)
85 }
86
87 async fn challenge(&mut self, challenge: Challenge) -> io::Result<ChallengeResponse> {
88 (self.challenge)(challenge)
89 }
90
91 async fn verify(&mut self, verification: Verification) -> io::Result<VerificationResponse> {
92 (self.verify)(verification)
93 }
94
95 async fn info(&mut self, info: Info) -> io::Result<()> {
96 (self.info)(info)
97 }
98
99 async fn error(&mut self, error: Error) -> io::Result<()> {
100 (self.error)(error)
101 }
102
103 async fn start_method(&mut self, start_method: StartMethod) -> io::Result<()> {
104 (self.start_method)(start_method)
105 }
106
107 async fn finished(&mut self) -> io::Result<()> {
108 (self.finished)()
109 }
110}