distant_auth/
authenticator.rs

1use std::io;
2
3use async_trait::async_trait;
4
5use crate::handler::AuthHandler;
6use crate::msg::*;
7
8/// Represents an interface for authenticating with a server.
9#[async_trait]
10pub trait Authenticate {
11    /// Performs authentication by leveraging the `handler` for any received challenge.
12    async fn authenticate(&mut self, mut handler: impl AuthHandler + Send) -> io::Result<()>;
13}
14
15/// Represents an interface for submitting challenges for authentication.
16#[async_trait]
17pub trait Authenticator: Send {
18    /// Issues an initialization notice and returns the response indicating which authentication
19    /// methods to pursue
20    async fn initialize(
21        &mut self,
22        initialization: Initialization,
23    ) -> io::Result<InitializationResponse>;
24
25    /// Issues a challenge and returns the answers to the `questions` asked.
26    async fn challenge(&mut self, challenge: Challenge) -> io::Result<ChallengeResponse>;
27
28    /// Requests verification of some `kind` and `text`, returning true if passed verification.
29    async fn verify(&mut self, verification: Verification) -> io::Result<VerificationResponse>;
30
31    /// Reports information with no response expected.
32    async fn info(&mut self, info: Info) -> io::Result<()>;
33
34    /// Reports an error occurred during authentication, consuming the authenticator since no more
35    /// challenges should be issued.
36    async fn error(&mut self, error: Error) -> io::Result<()>;
37
38    /// Reports that the authentication has started for a specific method.
39    async fn start_method(&mut self, start_method: StartMethod) -> io::Result<()>;
40
41    /// Reports that the authentication has finished successfully, consuming the authenticator
42    /// since no more challenges should be issued.
43    async fn finished(&mut self) -> io::Result<()>;
44}
45
46/// Represents an implementator of [`Authenticator`] used purely for testing purposes.
47#[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}