Skip to main content

ansible_inventory_cloud/http/
authentication.rs

1use core::{future::Future, pin::Pin};
2use std::sync::Arc;
3
4use serde::{de::DeserializeOwned, Deserialize, Serialize};
5
6//
7//
8//
9#[derive(Debug, Clone)]
10pub enum Authentication<AQ>
11where
12    AQ: AuthenticationQuery,
13{
14    HeaderAuthorizationBearer(String),
15    Query(AQ),
16}
17
18#[derive(Debug, Clone, Copy)]
19pub enum AuthenticationType {
20    HeaderAuthorizationBearer,
21    Query,
22}
23
24//
25//
26//
27pub trait AuthenticationQuery: DeserializeOwned {}
28impl<T> AuthenticationQuery for T where T: DeserializeOwned {}
29
30#[derive(Deserialize, Serialize, Debug, Clone)]
31pub struct GenericAuthenticationQuery {
32    pub access_token: String,
33}
34
35//
36//
37//
38pub enum AuthenticationVerifier<AQ, AO, Ctx>
39where
40    AQ: AuthenticationQuery,
41{
42    Sync(
43        #[allow(clippy::type_complexity)]
44        Arc<
45            dyn Fn(Authentication<AQ>, Ctx) -> Result<AO, Box<dyn std::error::Error>> + Send + Sync,
46        >,
47    ),
48    Async(
49        #[allow(clippy::type_complexity)]
50        Arc<
51            dyn Fn(
52                    Authentication<AQ>,
53                    Ctx,
54                ) -> Pin<
55                    Box<
56                        dyn Future<Output = Result<AO, Box<dyn std::error::Error>>>
57                            + Send
58                            + 'static,
59                    >,
60                > + Send
61                + Sync,
62        >,
63    ),
64}
65impl<AQ, AO, Ctx> Clone for AuthenticationVerifier<AQ, AO, Ctx>
66where
67    AQ: AuthenticationQuery,
68{
69    fn clone(&self) -> Self {
70        match self {
71            Self::Sync(x) => Self::Sync(x.clone()),
72            Self::Async(x) => Self::Async(x.clone()),
73        }
74    }
75}
76
77impl<AQ, AO, Ctx> AuthenticationVerifier<AQ, AO, Ctx>
78where
79    AQ: AuthenticationQuery,
80{
81    pub fn sync<F>(f: F) -> Self
82    where
83        F: Fn(Authentication<AQ>, Ctx) -> Result<AO, Box<dyn std::error::Error>>
84            + Send
85            + Sync
86            + 'static,
87    {
88        Self::Sync(Arc::new(f))
89    }
90
91    pub fn r#async<F>(f: F) -> Self
92    where
93        F: Fn(
94                Authentication<AQ>,
95                Ctx,
96            ) -> Pin<
97                Box<dyn Future<Output = Result<AO, Box<dyn std::error::Error>>> + Send + 'static>,
98            > + Send
99            + Sync
100            + 'static,
101    {
102        Self::Async(Arc::new(f))
103    }
104}