1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::authentication::error::error_type::AuthenticationError;
use crate::authentication::scheme::authentication::Authentication;
use crate::authentication::scheme::authentication_provider::AuthenticationProvider;
use crate::user_details::UserDetails;

pub mod endpoint_matcher;
pub mod error;
pub mod middleware;
pub mod scheme;


#[derive(Clone)]
pub struct ProviderManager {
    providers: Vec<Box<dyn AuthenticationProvider>>
}

impl ProviderManager {
    pub fn new(providers: Vec<Box<dyn AuthenticationProvider>>) -> ProviderManager {
        ProviderManager {
            providers
        }
    }

    #[allow(clippy::borrowed_box)]
    pub async fn authenticate(&self, authentication: &Box<dyn Authentication>) -> Result<Box<dyn UserDetails>, AuthenticationError> {
        let providers = &self.providers;
        let mut last_error: Option<AuthenticationError> = None;
        for provider in providers {
            let result = provider.authenticate(authentication).await;
            match result {
                Ok(user) => return Ok(user),
                Err(err) => {
                    last_error = Some(err);
                    continue;
                }
            }
        }
        match last_error {
            Some(e) => Err(e),
            None => Err(AuthenticationError::UsernameNotFound)
        }
    }
}