@@ -193,15 +193,17 @@ impl HttpClient {
193
193
Ok ( ( ) )
194
194
}
195
195
196
- /// Authenticate the request by filling token.
196
+ /// Authenticates the request by adding a bearer token to the authorization header .
197
197
///
198
- /// - If neither token nor credential is provided, this method will do nothing.
199
- /// - If only credential is provided, this method will try to fetch token from the server.
200
- /// - If token is provided, this method will use the token directly.
198
+ /// This method supports three authentication modes:
201
199
///
202
- /// # TODO
200
+ /// 1. **No authentication** - Skip authentication when both `credential` and `token` are missing.
201
+ /// 2. **Token authentication** - Use the provided `token` directly for authentication.
202
+ /// 3. **OAuth authentication** - Exchange `credential` for a token, cache it, then use it for authentication.
203
203
///
204
- /// Support refreshing token while needed.
204
+ /// When both `credential` and `token` are present, `token` takes precedence.
205
+ ///
206
+ /// # TODO: Support automatic token refreshing.
205
207
async fn authenticate ( & self , req : & mut Request ) -> Result < ( ) > {
206
208
// Clone the token from lock without holding the lock for entire function.
207
209
let token = self . token . lock ( ) . await . clone ( ) ;
@@ -210,24 +212,18 @@ impl HttpClient {
210
212
return Ok ( ( ) ) ;
211
213
}
212
214
213
- // Use token if provided.
214
- if let Some ( token) = & token {
215
- req. headers_mut ( ) . insert (
216
- http:: header:: AUTHORIZATION ,
217
- format ! ( "Bearer {token}" ) . parse ( ) . map_err ( |e| {
218
- Error :: new (
219
- ErrorKind :: DataInvalid ,
220
- "Invalid token received from catalog server!" ,
221
- )
222
- . with_source ( e)
223
- } ) ?,
224
- ) ;
225
- return Ok ( ( ) ) ;
226
- }
215
+ // Either use the provided token or exchange credential for token, cache and use that
216
+ let token = match token {
217
+ Some ( token) => token,
218
+ None => {
219
+ let token = self . exchange_credential_for_token ( ) . await ?;
220
+ // Update token so that we use it for next request instead of
221
+ // exchanging credential for token from the server again
222
+ * self . token . lock ( ) . await = Some ( token. clone ( ) ) ;
223
+ token
224
+ }
225
+ } ;
227
226
228
- let token = self . exchange_credential_for_token ( ) . await ?;
229
- // Update token.
230
- * self . token . lock ( ) . await = Some ( token. clone ( ) ) ;
231
227
// Insert token in request.
232
228
req. headers_mut ( ) . insert (
233
229
http:: header:: AUTHORIZATION ,
0 commit comments