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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::borrow::Cow;
use std::sync::Arc;
use crate::credential::{
    AuthenticationCredentialToken, AuthenticationCredentialUsernamePassword, HttpRealmCredentials,
};
use crate::sensitive::SetSensitiveHeader;
use crate::{AuthenticError, AuthenticationScheme};
#[cfg(feature = "reqwest_blocking")]
pub mod blocking;
pub struct NoAuthentication;
impl NoAuthentication {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {}
    }
}
impl AuthenticationScheme for NoAuthentication {
    type Builder = reqwest::RequestBuilder;
    type Request = reqwest::Request;
    type Response = reqwest::Response;
    type Error = reqwest::Error;
}
pub struct HeaderAuthentication<Credential> {
    header_name: Cow<'static, [u8]>,
    credential: Arc<Credential>,
}
impl<Credential: 'static> HeaderAuthentication<Credential>
where
    Credential: AuthenticationCredentialToken,
{
    pub fn new(header_name: Cow<'static, [u8]>, credential: &Arc<Credential>) -> Self {
        Self {
            header_name,
            credential: credential.clone(),
        }
    }
}
impl<Credential> AuthenticationScheme for HeaderAuthentication<Credential>
where
    Credential: AuthenticationCredentialToken,
{
    type Builder = reqwest::RequestBuilder;
    type Request = reqwest::Request;
    type Response = reqwest::Response;
    type Error = reqwest::Error;
    fn configure(&self, builder: Self::Builder) -> Self::Builder {
        builder.set_sensitive_header(self.header_name.as_ref(), self.credential.token())
    }
}
pub struct BasicAuthentication<Credential> {
    credential: Arc<Credential>,
}
impl<Credential> BasicAuthentication<Credential>
where
    Credential: AuthenticationCredentialUsernamePassword + 'static,
{
    pub fn new(credential: &Arc<Credential>) -> Self {
        Self {
            credential: credential.clone(),
        }
    }
}
impl<Credential> AuthenticationScheme for BasicAuthentication<Credential>
where
    Credential: AuthenticationCredentialUsernamePassword,
{
    type Builder = reqwest::RequestBuilder;
    type Request = reqwest::Request;
    type Response = reqwest::Response;
    type Error = reqwest::Error;
    fn configure(&self, builder: Self::Builder) -> Self::Builder {
        builder.basic_auth(self.credential.username(), Some(self.credential.password()))
    }
}
pub struct HttpAuthentication<Credential> {
    credential: Arc<HttpRealmCredentials<Credential>>,
}
impl<Credential> HttpAuthentication<Credential> {
    pub fn new(credential: &Arc<HttpRealmCredentials<Credential>>) -> Self {
        Self {
            credential: credential.clone(),
        }
    }
}
impl<Credential> AuthenticationScheme for HttpAuthentication<Credential>
where
    Credential: AuthenticationCredentialUsernamePassword + 'static,
{
    type Builder = reqwest::RequestBuilder;
    type Request = reqwest::Request;
    type Response = reqwest::Response;
    type Error = reqwest::Error;
    fn switch(
        &mut self,
        response: &Self::Response,
    ) -> Result<
        Option<
            Box<
                dyn AuthenticationScheme<
                    Builder = Self::Builder,
                    Request = Self::Request,
                    Response = Self::Response,
                    Error = Self::Error,
                >,
            >,
        >,
        AuthenticError,
    > {
        if response.status() == ::http::StatusCode::UNAUTHORIZED {
            let pw_client = ::http_auth::PasswordClient::try_from(
                response
                    .headers()
                    .get_all(::hyper::header::WWW_AUTHENTICATE),
            )
            .map_err(AuthenticError::Other)?;
            match pw_client {
                http_auth::PasswordClient::Basic(client) => {
                    let realm = client.realm();
                    match self.credential.get_credential(realm) {
                        Some(credential) => {
                            Ok(Some(Box::new(BasicAuthentication::new(credential))))
                        }
                        None => Err(AuthenticError::UnknownRealm(realm.to_owned())),
                    }
                }
                http_auth::PasswordClient::Digest(_) => todo!(),
                _ => todo!(),
            }
        } else {
            Ok(None)
        }
    }
}