@@ -131,25 +131,7 @@ impl Middleware for AuthMiddleware {
131
131
132
132
// In the middleware, existing credentials are already moved from the URL
133
133
// to the headers so for display purposes we restore some information
134
- let url = if tracing:: enabled!( tracing:: Level :: DEBUG ) {
135
- let mut url = request. url ( ) . clone ( ) ;
136
- if let Some ( username) = credentials
137
- . as_ref ( )
138
- . and_then ( |credentials| credentials. username ( ) )
139
- {
140
- let _ = url. set_username ( username) ;
141
- } ;
142
- if credentials
143
- . as_ref ( )
144
- . and_then ( |credentials| credentials. password ( ) )
145
- . is_some ( )
146
- {
147
- let _ = url. set_password ( Some ( "****" ) ) ;
148
- } ;
149
- url. to_string ( )
150
- } else {
151
- request. url ( ) . to_string ( )
152
- } ;
134
+ let url = tracing_url ( & request, credentials. as_ref ( ) ) ;
153
135
trace ! ( "Handling request for {url}" ) ;
154
136
155
137
if let Some ( credentials) = credentials {
@@ -198,13 +180,20 @@ impl Middleware for AuthMiddleware {
198
180
// We have no credentials
199
181
trace ! ( "Request for {url} is unauthenticated, checking cache" ) ;
200
182
201
- // Check the cache for a URL match
183
+ // Check the cache for a URL match first, this can save us from making a failing request
202
184
let credentials = self . cache ( ) . get_url ( request. url ( ) , & Username :: none ( ) ) ;
203
185
if let Some ( credentials) = credentials. as_ref ( ) {
204
186
request = credentials. authenticate ( request) ;
187
+
188
+ // If it's fully authenticated, finish the request
205
189
if credentials. password ( ) . is_some ( ) {
190
+ trace ! ( "Request for {url} is fully authenticated" ) ;
206
191
return self . complete_request ( None , request, extensions, next) . await ;
207
192
}
193
+
194
+ // If we just found a username, we'll make the request then look for password elsewhere
195
+ // if it fails
196
+ trace ! ( "Found username for {url} in cache, attempting request" ) ;
208
197
}
209
198
let attempt_has_username = credentials
210
199
. as_ref ( )
@@ -216,8 +205,12 @@ impl Middleware for AuthMiddleware {
216
205
trace ! ( "Checking for credentials for {url}" ) ;
217
206
( request, None )
218
207
} else {
219
- // Otherwise, attempt an anonymous request
220
- trace ! ( "Attempting unauthenticated request for {url}" ) ;
208
+ let url = tracing_url ( & request, credentials. as_deref ( ) ) ;
209
+ if credentials. is_none ( ) {
210
+ trace ! ( "Attempting unauthenticated request for {url}" ) ;
211
+ } else {
212
+ trace ! ( "Attempting partially authenticated request for {url}" ) ;
213
+ }
221
214
222
215
// <https://github.com/TrueLayer/reqwest-middleware/blob/abdf1844c37092d323683c2396b7eefda1418d3c/reqwest-retry/src/middleware.rs#L141-L149>
223
216
// Clone the request so we can retry it on authentication failure
@@ -247,13 +240,17 @@ impl Middleware for AuthMiddleware {
247
240
( retry_request, Some ( response) )
248
241
} ;
249
242
250
- // Check in the cache first
251
- let credentials = self . cache ( ) . get_realm (
252
- Realm :: from ( retry_request. url ( ) ) ,
253
- credentials
254
- . map ( |credentials| credentials. to_username ( ) )
255
- . unwrap_or ( Username :: none ( ) ) ,
256
- ) ;
243
+ // Check if there are credentials in the realm-level cache
244
+ let credentials = self
245
+ . cache ( )
246
+ . get_realm (
247
+ Realm :: from ( retry_request. url ( ) ) ,
248
+ credentials
249
+ . as_ref ( )
250
+ . map ( |credentials| credentials. to_username ( ) )
251
+ . unwrap_or ( Username :: none ( ) ) ,
252
+ )
253
+ . or ( credentials) ;
257
254
if let Some ( credentials) = credentials. as_ref ( ) {
258
255
if credentials. password ( ) . is_some ( ) {
259
256
trace ! ( "Retrying request for {url} with credentials from cache {credentials:?}" ) ;
@@ -265,7 +262,7 @@ impl Middleware for AuthMiddleware {
265
262
}
266
263
267
264
// Then, fetch from external services.
268
- // Here we use the username from the cache if present.
265
+ // Here, we use the username from the cache if present.
269
266
if let Some ( credentials) = self
270
267
. fetch_credentials ( credentials. as_deref ( ) , retry_request. url ( ) )
271
268
. await
@@ -406,5 +403,27 @@ impl AuthMiddleware {
406
403
}
407
404
}
408
405
406
+ fn tracing_url ( request : & Request , credentials : Option < & Credentials > ) -> String {
407
+ if tracing:: enabled!( tracing:: Level :: DEBUG ) {
408
+ let mut url = request. url ( ) . clone ( ) ;
409
+ if let Some ( username) = credentials
410
+ . as_ref ( )
411
+ . and_then ( |credentials| credentials. username ( ) )
412
+ {
413
+ let _ = url. set_username ( username) ;
414
+ } ;
415
+ if credentials
416
+ . as_ref ( )
417
+ . and_then ( |credentials| credentials. password ( ) )
418
+ . is_some ( )
419
+ {
420
+ let _ = url. set_password ( Some ( "****" ) ) ;
421
+ } ;
422
+ url. to_string ( )
423
+ } else {
424
+ request. url ( ) . to_string ( )
425
+ }
426
+ }
427
+
409
428
#[ cfg( test) ]
410
429
mod tests;
0 commit comments