wgtmac commented on code in PR #1132:
URL: https://github.com/apache/iceberg-cpp/pull/1132#discussion_r4658753005


##########
src/iceberg/catalog/rest/auth/auth_session_internal.h:
##########
@@ -254,43 +254,85 @@ class OAuth2Session final : public AuthSession,
     auto result = RetryRunner<retry::StopRetryOn<>>({.num_retries = 
5}).Run([this] {
       return RefreshToken();
     });
+    if (!result.has_value()) {
+      // One final credential-backed attempt, within the same close guard.
+      result = RefreshToken(/*recover=*/true);
+    }
     if (!result.has_value()) {
       ICEBERG_LOG_WARN("Failed to refresh OAuth2 token: {}", 
result.error().message);
       MarkRefreshStopped();
       return std::nullopt;
     }
+    if (!result->has_value()) {
+      MarkRefreshStopped();
+    }
     return *result;
   }
 
-  // Record that no further refresh will be scheduled after a failure.
+  // Record that no further refresh will be scheduled.
   void MarkRefreshStopped() {
     std::lock_guard lock(refresh_mutex_);
     refresh_stopped_ = true;
   }
 
-  Result<std::optional<std::chrono::milliseconds>> RefreshToken() {
+  Result<OAuthTokenResponse> FetchRefreshToken(const std::string& token,
+                                               const std::string& 
issued_token_type,
+                                               bool recover) {
+    auto empty_session = AuthSession::MakeDefault({});
+    if (!config_.exchange_enabled) {
+      return OAuth2Util::FetchToken(*client_, *empty_session, 
refresh_properties_);
+    }
+    auto headers =
+        recover
+            ? std::unordered_map<std::string, std::string>{{"Authorization",
+                                                            "Basic " +
+                                                                Base64::Encode(

Review Comment:
   Encode the parsed client ID and secret separately per RFC 6749 ยง2.3.1, then 
join them with a colon before Base64 encoding. A bare secret currently has no 
Basic separator, and credentials containing `+` can be decoded incorrectly.



##########
src/iceberg/catalog/rest/auth/auth_session_internal.h:
##########
@@ -254,43 +254,85 @@ class OAuth2Session final : public AuthSession,
     auto result = RetryRunner<retry::StopRetryOn<>>({.num_retries = 
5}).Run([this] {
       return RefreshToken();
     });
+    if (!result.has_value()) {
+      // One final credential-backed attempt, within the same close guard.
+      result = RefreshToken(/*recover=*/true);
+    }
     if (!result.has_value()) {
       ICEBERG_LOG_WARN("Failed to refresh OAuth2 token: {}", 
result.error().message);
       MarkRefreshStopped();
       return std::nullopt;
     }
+    if (!result->has_value()) {
+      MarkRefreshStopped();
+    }
     return *result;
   }
 
-  // Record that no further refresh will be scheduled after a failure.
+  // Record that no further refresh will be scheduled.
   void MarkRefreshStopped() {
     std::lock_guard lock(refresh_mutex_);
     refresh_stopped_ = true;
   }
 
-  Result<std::optional<std::chrono::milliseconds>> RefreshToken() {
+  Result<OAuthTokenResponse> FetchRefreshToken(const std::string& token,
+                                               const std::string& 
issued_token_type,
+                                               bool recover) {
+    auto empty_session = AuthSession::MakeDefault({});
+    if (!config_.exchange_enabled) {
+      return OAuth2Util::FetchToken(*client_, *empty_session, 
refresh_properties_);
+    }
+    auto headers =
+        recover
+            ? std::unordered_map<std::string, std::string>{{"Authorization",
+                                                            "Basic " +
+                                                                Base64::Encode(
+                                                                    
config_.credential)}}
+            : OAuth2Util::AuthHeaders(token);
+    // Explicit request headers override any default Authorization on the 
client.
+    // Keep the current token as subject, without a parent actor.
+    return OAuth2Util::ExchangeToken(*client_, *empty_session, headers, token,
+                                     issued_token_type, std::nullopt, 
std::nullopt,
+                                     config_.scope, config_.token_endpoint,
+                                     config_.optional_oauth_params);
+  }
+
+  Result<std::optional<std::chrono::milliseconds>> RefreshToken(bool recover = 
false) {
     {
       std::lock_guard lock(refresh_mutex_);
       if (closed_) {
         return std::nullopt;
       }
     }
     try {
-      auto request_started_at = std::chrono::steady_clock::now();
-      auto empty_session = AuthSession::MakeDefault({});
-      // TODO(lishuxu): Honor token-exchange-enabled when refreshing tokens.
-      ICEBERG_ASSIGN_OR_RAISE(
-          auto response,
-          OAuth2Util::FetchToken(*client_, *empty_session, 
refresh_properties_));
+      std::string token;
+      std::string issued_token_type;
       {
-        std::unique_lock lock(mutex_);
-        UpdateTokenState(response, request_started_at);
+        std::shared_lock lock(mutex_);
+        token = token_;
+        issued_token_type = issued_token_type_;
+        recover = recover || (expires_at_ != 
std::chrono::steady_clock::time_point{} &&
+                              expires_at_ <= config_.now());
+      }
+      if ((!config_.exchange_enabled || recover) && 
config_.credential.empty()) {

Review Comment:
   The public factory previously sent the refresh request with both credential 
arguments empty, allowing the supplied HTTP client to authenticate it. Please 
preserve that path and cover it in the default Authorization test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to