lishuxu commented on code in PR #985:
URL: https://github.com/apache/iceberg-cpp/pull/985#discussion_r4323580545


##########
src/iceberg/catalog/rest/auth/auth_manager.cc:
##########
@@ -283,42 +320,78 @@ class OAuth2Manager : public AuthManager {
     ICEBERG_PRECHECK(parent_info.has_value(),
                      "OAuth2 child session requires OAuth2 parent metadata");
 
+    // Use cache if we have a valid cache_key
+    if (!cache_key.empty() && session_cache_) {
+      return session_cache_->Get(cache_key,
+                                 [&]() -> Result<std::shared_ptr<AuthSession>> 
{
+                                   return CreateChildSessionUncached(
+                                       credentials, allow_credential, 
*parent_info);
+                                 });
+    }
+
+    // No cache, create session directly
+    return CreateChildSessionUncached(credentials, allow_credential, 
*parent_info);
+  }
+
+  Result<std::shared_ptr<AuthSession>> CreateChildSessionUncached(
+      const std::unordered_map<std::string, std::string>& credentials,
+      bool allow_credential, const OAuth2SessionInfo& parent_info) {
+    auto token_it = credentials.find(AuthProperties::kToken.key());
+    auto credential_it = credentials.find(AuthProperties::kCredential.key());
+    auto typed_token = FindPreferredTypedToken(credentials);
+
     if (token_it != credentials.end()) {
-      ICEBERG_ASSIGN_OR_RAISE(auto config,
-                              ChildConfig(*parent_info, 
parent_info->credential));
+      // Token child session: don't include parent credential in config.
+      // Token-based sessions are not refreshed; when token exchange is 
enabled,
+      // refresh will use the token itself rather than the parent credential.
+      auto properties = parent_info.optional_oauth_params;
+      properties[AuthProperties::kScope.key()] = parent_info.scope;
+      properties[AuthProperties::kOAuth2ServerUri.key()] = 
parent_info.oauth2_server_uri;
+      ICEBERG_ASSIGN_OR_RAISE(auto config, 
AuthProperties::FromProperties(properties));
       return MakeSession(AccessTokenResponse(token_it->second), config,
                          /*keep_refreshed=*/false);
     }
 
     if (allow_credential && credential_it != credentials.end()) {
       ICEBERG_ASSIGN_OR_RAISE(auto config,
-                              ChildConfig(*parent_info, 
credential_it->second));
-      ICEBERG_ASSIGN_OR_RAISE(auto response,
-                              OAuth2Util::FetchToken(*refresh_client_, 
*parent, config));
-      return MakeSession(response, config, /*keep_refreshed=*/false);
+                              ChildConfig(parent_info, credential_it->second));
+      // Use parent session headers for FetchToken authentication
+      auto temp_parent = AuthSession::MakeDefault(parent_info.headers);
+      ICEBERG_ASSIGN_OR_RAISE(
+          auto response, OAuth2Util::FetchToken(*refresh_client_, 
*temp_parent, config));
+      // Credential child sessions use keep_refreshed from catalog level
+      return MakeSession(response, config, keep_refreshed_);
     }
 
     std::optional<std::string> actor_token;
     std::optional<std::string> actor_token_type;
-    if (!parent_info->token.empty()) {
-      actor_token = parent_info->token;
-      actor_token_type = parent_info->issued_token_type;
+    if (!parent_info.token.empty()) {
+      actor_token = parent_info.token;
+      actor_token_type = parent_info.issued_token_type;
     }
+    // Use parent session headers for ExchangeToken authentication
+    auto temp_parent = AuthSession::MakeDefault(parent_info.headers);
     ICEBERG_ASSIGN_OR_RAISE(
         auto response,
-        OAuth2Util::ExchangeToken(*refresh_client_, *parent, {}, 
typed_token->second,
+        OAuth2Util::ExchangeToken(*refresh_client_, *temp_parent, {}, 
typed_token->second,
                                   typed_token->first, actor_token, 
actor_token_type,
-                                  parent_info->scope, 
parent_info->oauth2_server_uri,
-                                  parent_info->optional_oauth_params));
+                                  parent_info.scope, 
parent_info.oauth2_server_uri,
+                                  parent_info.optional_oauth_params));
     ICEBERG_ASSIGN_OR_RAISE(auto config,
-                            ChildConfig(*parent_info, 
parent_info->credential));
+                            ChildConfig(parent_info, parent_info.credential));
     return MakeSession(response, config, /*keep_refreshed=*/false);

Review Comment:
   Agreed, but enabling refresh now would switch the child to the catalog 
identity, because refresh only uses the parent's credential. I'll enable it 
once refresh via token exchange lands (TODO in DoRefreshAttempt).



-- 
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