@@ -20,6 +20,7 @@ use crate::authenticator::Authenticator;
2020use crate :: error;
2121use crate :: error:: ErrorKind ;
2222use crate :: httpx:: client:: Client ;
23+ use crate :: httpx:: request:: { Auth , BasicAuth , BearerAuth } ;
2324use crate :: retrybesteffort:: BackoffCalculator ;
2425use crate :: service_type:: ServiceType ;
2526use crate :: util:: get_host_port_from_uri;
@@ -44,12 +45,37 @@ pub(crate) struct HttpComponentState {
4445}
4546
4647pub ( crate ) struct HttpEndpointProperties {
47- pub username : String ,
48- pub password : String ,
48+ pub auth : Auth ,
4949 pub endpoint : String ,
5050 pub endpoint_id : Option < String > ,
5151}
5252
53+ pub ( crate ) fn auth_from_authenticator (
54+ authenticator : & Authenticator ,
55+ service_type : & ServiceType ,
56+ host : & str ,
57+ ) -> error:: Result < Auth > {
58+ match authenticator {
59+ Authenticator :: PasswordAuthenticator ( authenticator) => {
60+ let creds = authenticator. get_credentials ( service_type, host. to_string ( ) ) ?;
61+ Ok ( Auth :: BasicAuth ( BasicAuth :: new (
62+ creds. username ,
63+ creds. password ,
64+ ) ) )
65+ }
66+ Authenticator :: CertificateAuthenticator ( authenticator) => {
67+ let creds = authenticator. get_credentials ( service_type, host. to_string ( ) ) ?;
68+ Ok ( Auth :: BasicAuth ( BasicAuth :: new (
69+ creds. username ,
70+ creds. password ,
71+ ) ) )
72+ }
73+ Authenticator :: JwtAuthenticator ( authenticator) => {
74+ Ok ( Auth :: BearerAuth ( BearerAuth :: new ( authenticator. get_token ( ) ) ) )
75+ }
76+ }
77+ }
78+
5379impl < C : Client > HttpComponent < C > {
5480 pub fn new (
5581 service_type : ServiceType ,
@@ -94,22 +120,14 @@ impl<C: Client> HttpComponent<C> {
94120 } ;
95121
96122 let host = get_host_port_from_uri ( found_endpoint) ?;
97- let user_pass = match & state. authenticator {
98- Authenticator :: PasswordAuthenticator ( authenticator) => {
99- authenticator. get_credentials ( & self . service_type , host) ?
100- }
101- Authenticator :: CertificateAuthenticator ( a) => {
102- a. get_credentials ( & self . service_type , host) ?
103- }
104- } ;
123+ let auth = auth_from_authenticator ( & state. authenticator , & self . service_type , & host) ?;
105124
106125 Ok ( (
107126 self . client . clone ( ) ,
108127 HttpEndpointProperties {
109128 endpoint_id : None ,
110129 endpoint : found_endpoint. clone ( ) ,
111- username : user_pass. username ,
112- password : user_pass. password ,
130+ auth,
113131 } ,
114132 ) )
115133 }
@@ -145,22 +163,14 @@ impl<C: Client> HttpComponent<C> {
145163 let endpoint = remaining_endpoints[ endpoint_id] ;
146164
147165 let host = get_host_port_from_uri ( endpoint) ?;
148- let user_pass = match & state. authenticator {
149- Authenticator :: PasswordAuthenticator ( authenticator) => {
150- authenticator. get_credentials ( & self . service_type , host) ?
151- }
152- Authenticator :: CertificateAuthenticator ( a) => {
153- a. get_credentials ( & self . service_type , host) ?
154- }
155- } ;
166+ let auth = auth_from_authenticator ( & state. authenticator , & self . service_type , & host) ?;
156167
157168 Ok ( Some ( (
158169 self . client . clone ( ) ,
159170 HttpEndpointProperties {
160171 endpoint_id : Some ( endpoint_id. clone ( ) ) ,
161172 endpoint : endpoint. clone ( ) ,
162- username : user_pass. username ,
163- password : user_pass. password ,
173+ auth,
164174 } ,
165175 ) ) )
166176 }
@@ -172,7 +182,7 @@ impl<C: Client> HttpComponent<C> {
172182 pub async fn orchestrate_endpoint < Resp , Fut > (
173183 & self ,
174184 endpoint_id : Option < String > ,
175- operation : impl Fn ( Arc < C > , String , String , String , String ) -> Fut + Send + Sync ,
185+ operation : impl Fn ( Arc < C > , String , String , Auth ) -> Fut + Send + Sync ,
176186 ) -> error:: Result < Resp >
177187 where
178188 C : Client ,
@@ -186,8 +196,7 @@ impl<C: Client> HttpComponent<C> {
186196 client,
187197 endpoint_id,
188198 endpoint_properties. endpoint ,
189- endpoint_properties. username ,
190- endpoint_properties. password ,
199+ endpoint_properties. auth ,
191200 )
192201 . await ;
193202 }
@@ -205,8 +214,7 @@ impl<C: Client> HttpComponent<C> {
205214 client,
206215 endpoint_properties. endpoint_id . unwrap_or_default ( ) ,
207216 endpoint_properties. endpoint ,
208- endpoint_properties. username ,
209- endpoint_properties. password ,
217+ endpoint_properties. auth ,
210218 )
211219 . await
212220 }
@@ -231,21 +239,9 @@ impl<C: Client> HttpComponent<C> {
231239 let mut targets = Vec :: with_capacity ( remaining_endpoints. len ( ) ) ;
232240 for ( _ep_id, endpoint) in remaining_endpoints {
233241 let host = get_host_port_from_uri ( endpoint) ?;
242+ let auth = auth_from_authenticator ( & state. authenticator , & self . service_type , & host) ?;
234243
235- let user_pass = match & state. authenticator {
236- Authenticator :: PasswordAuthenticator ( authenticator) => {
237- authenticator. get_credentials ( & self . service_type , host) ?
238- }
239- Authenticator :: CertificateAuthenticator ( a) => {
240- a. get_credentials ( & self . service_type , host) ?
241- }
242- } ;
243-
244- targets. push ( T :: new (
245- endpoint. clone ( ) ,
246- user_pass. username ,
247- user_pass. password ,
248- ) ) ;
244+ targets. push ( T :: new ( endpoint. clone ( ) , auth) ) ;
249245 }
250246
251247 Ok ( ( self . client . clone ( ) , targets) )
@@ -289,5 +285,5 @@ impl HttpComponentState {
289285}
290286
291287pub ( crate ) trait NodeTarget {
292- fn new ( endpoint : String , username : String , password : String ) -> Self ;
288+ fn new ( endpoint : String , auth : Auth ) -> Self ;
293289}
0 commit comments