CTTY commented on code in PR #2838:
URL: https://github.com/apache/iceberg-rust/pull/2838#discussion_r3678883785


##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -231,9 +252,13 @@ impl RestCatalogConfig {
         ])
     }
 
-    /// Get the client from the config.
-    pub(crate) fn client(&self) -> Option<Client> {
-        self.client.clone()
+    /// The HTTP client: the configured one, or a lazily-created default that
+    /// is shared across every user of this config (and its clones), so token
+    /// and catalog requests keep sharing one connection pool.
+    pub(crate) fn client(&self) -> Client {
+        self.client
+            .clone()
+            .unwrap_or_else(|| 
self.default_client.get_or_init(Client::default).clone())

Review Comment:
   does `unwrap_or_default` work here? why do we need an extra default_client?



##########
crates/catalog/rest/src/client.rs:
##########
@@ -17,34 +17,27 @@
 
 use std::collections::HashMap;
 use std::fmt::{Debug, Formatter};
+use std::sync::Arc;
 
-use http::StatusCode;
 use iceberg::{Error, ErrorKind, Result};
 use reqwest::header::HeaderMap;
 use reqwest::{Client, IntoUrl, Method, Request, RequestBuilder, Response};
 use serde::de::DeserializeOwned;
-use tokio::sync::Mutex;
 
 use crate::RestCatalogConfig;
-use crate::types::{ErrorResponse, TokenResponse};
+use crate::auth::{AuthManager, AuthRequest, AuthSession};
 
 pub(crate) struct HttpClient {
     client: Client,
 
-    /// The token to be used for authentication.
-    ///
-    /// It's possible to fetch the token from the server while needed.
-    token: Mutex<Option<String>>,
-    /// The token endpoint to be used for authentication.
-    token_endpoint: String,
-    /// The credential to be used for authentication.
-    credential: Option<(Option<String>, String)>,
     /// Extra headers to be added to each request.
     extra_headers: HeaderMap,
-    /// Extra oauth parameters to be added to each authentication request.
-    extra_oauth_params: HashMap<String, String>,
     /// Whether to disable header redaction in error logs (defaults to false 
for security).
     disable_header_redaction: bool,
+    /// The auth manager living for the lifetime of the catalog.
+    auth_manager: Arc<dyn AuthManager>,
+    /// The session authenticating requests in the current phase.
+    session: Arc<dyn AuthSession>,

Review Comment:
   +1 I don't see us keeping auth session and manager in the long term. I'm 
happy if we could address this in the follow up PR



##########
crates/catalog/rest/src/auth/mod.rs:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Pluggable authentication for the REST catalog, mirroring Iceberg Java's
+//! `AuthManager`/`AuthSession` API.
+
+mod oauth2;
+
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use http::{HeaderMap, Method};
+use iceberg::Result;
+pub use oauth2::OAuth2Manager;
+use reqwest::Request;
+
+/// `rest.auth.type` value disabling authentication.
+pub const AUTH_TYPE_NONE: &str = "none";
+/// `rest.auth.type` value selecting OAuth2 token authentication.
+pub const AUTH_TYPE_OAUTH2: &str = "oauth2";
+
+/// Creates the [`AuthSession`]s used to authenticate REST catalog requests.
+///
+/// A manager is created once per catalog, either from the `rest.auth.type`
+/// property or injected through `RestCatalogBuilder::with_auth_manager`, and
+/// lives for the lifetime of the catalog.
+#[async_trait]
+pub trait AuthManager: Debug + Send + Sync {
+    /// Session used for the initial `/v1/config` handshake, built from the
+    /// user-supplied configuration.
+    async fn init_session(&self) -> Result<Arc<dyn AuthSession>>;
+
+    /// Session used for all subsequent catalog requests, given the properties
+    /// merged from the user configuration and the server's config response.
+    ///
+    /// Implementations may carry state (e.g. a cached token) over from the
+    /// init session.
+    async fn catalog_session(
+        &self,
+        props: &HashMap<String, String>,
+    ) -> Result<Arc<dyn AuthSession>>;
+}
+
+/// An outgoing REST request being authenticated by an [`AuthSession`].
+///
+/// Wraps the request so authentication implementations depend only on the
+/// stable `http` crate and standard types, not on the concrete HTTP client the
+/// REST catalog uses internally.
+pub struct AuthRequest<'a> {
+    inner: &'a mut Request,
+}
+
+impl<'a> AuthRequest<'a> {
+    pub(crate) fn new(inner: &'a mut Request) -> Self {
+        Self { inner }
+    }
+
+    /// The request method.
+    pub fn method(&self) -> &Method {
+        self.inner.method()
+    }
+
+    /// The request URL, as a string (scheme, host, path and query).
+    pub fn url_str(&self) -> &str {
+        self.inner.url().as_str()
+    }
+
+    /// The request headers.
+    pub fn headers(&self) -> &HeaderMap {
+        self.inner.headers()
+    }
+
+    /// The mutable request headers, e.g. to add an `Authorization` header.
+    pub fn headers_mut(&mut self) -> &mut HeaderMap {
+        self.inner.headers_mut()
+    }
+
+    /// The in-memory request body, or `None` for an empty or streaming body.
+    pub fn body(&self) -> Option<&[u8]> {
+        self.inner.body().and_then(|body| body.as_bytes())
+    }
+}
+
+/// Authenticates outgoing REST catalog requests.
+#[async_trait]
+pub trait AuthSession: Debug + Send + Sync {
+    /// Applies authentication to the request (adds headers, signs, ...).
+    async fn authenticate(&self, request: &mut AuthRequest<'_>) -> Result<()>;
+
+    /// Drops any cached credentials so the next request re-authenticates.
+    async fn invalidate(&self) -> Result<()> {
+        Ok(())
+    }
+
+    /// Proactively refreshes cached credentials (e.g. re-exchanges an OAuth2
+    /// client credential for a new token), leaving them intact on failure.
+    async fn refresh(&self) -> Result<()> {
+        Ok(())
+    }

Review Comment:
   RestCatalog::invalidate_token/regenerate_token were implemented in the first 
place as a workaround, because we had no mechanism to allow users configure 
token expiry and regeneration, and we still don't have that now :). So I'm 
guessing existing users are adding custom code to build their own 
invalidation/refreshing logic to use it. (the original discussions of adding 
them can be found in https://github.com/apache/iceberg-rust/issues/437)
   
   I think we should drop these two APIs based on the following thoughts: 
   1. With AuthManager, users can implement/inject their own AuthManager to 
refresh/invalidate the token
   2. These APIs won't make sense to non-oauth2 authenticators
   3. It will be somewhat a breaking change, but it's more like users will need 
a different custom code to work with it and the change won't block users from 
doing what they do with a bit more code
   
   With above said, I do think refreshing token is a basic feature that should 
come out of the box, and we should use 
https://github.com/apache/iceberg-rust/issues/301 to track that work separately
   
   Would love to hear other perspectives here!



##########
crates/catalog/rest/src/auth/oauth2.rs:
##########
@@ -0,0 +1,350 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::fmt::{Debug, Formatter};
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use http::StatusCode;
+use iceberg::{Error, ErrorKind, Result};
+use reqwest::header::HeaderMap;
+use reqwest::{Client, Method};
+use tokio::sync::Mutex;
+
+use super::{AuthManager, AuthRequest, AuthSession};
+use crate::catalog::{
+    REST_CATALOG_PROP_URI, RestCatalogConfig, credential_from_props, 
default_token_endpoint,
+    explicit_headers_from_props,
+};
+use crate::types::{ErrorResponse, TokenResponse};
+
+/// Per-phase OAuth2 parameters (init vs. post-handshake catalog phase).
+#[derive(Clone)]
+struct OAuth2Params {
+    extra_headers: HeaderMap,
+    token_endpoint: String,
+    credential: Option<(Option<String>, String)>,
+    extra_oauth_params: HashMap<String, String>,
+}
+
+/// [`AuthManager`] implementing the OAuth2 client-credentials flow used by
+/// Iceberg REST catalogs.
+///
+/// A configured `token` is used directly; otherwise `credential` is exchanged
+/// for a token at the token endpoint and cached. The cached token is shared
+/// across sessions so it survives the config handshake.
+pub struct OAuth2Manager {
+    client: Client,
+    token: Arc<Mutex<Option<String>>>,
+    init_params: OAuth2Params,
+    /// True when the token endpoint was derived from the catalog URI (not
+    /// explicitly configured): it is then recomputed from the merged URI in
+    /// [`Self::catalog_session`], since `/v1/config` may override the URI.
+    endpoint_is_default: bool,
+}
+
+impl OAuth2Manager {
+    /// Creates a manager exchanging credentials at `token_endpoint`, with no
+    /// token or credential configured. Combine with the `with_*` methods:
+    ///
+    /// ```rust,ignore
+    /// let manager = 
OAuth2Manager::new("https://auth.example.com/v1/oauth/tokens";)
+    ///     .with_credential(Some("client-id".into()), "client-secret".into());
+    /// ```
+    pub fn new(token_endpoint: impl Into<String>) -> Self {
+        Self {
+            client: Client::default(),
+            token: Arc::new(Mutex::new(None)),
+            init_params: OAuth2Params {
+                extra_headers: HeaderMap::new(),
+                token_endpoint: token_endpoint.into(),
+                credential: None,
+                // Same default as the configuration path: the catalog scope.
+                extra_oauth_params: HashMap::from([("scope".to_string(), 
"catalog".to_string())]),
+            },
+            endpoint_is_default: false,
+        }
+    }
+
+    /// Sets a bearer token used directly (takes precedence over `credential`).
+    pub fn with_token(mut self, token: impl Into<String>) -> Self {
+        self.token = Arc::new(Mutex::new(Some(token.into())));
+        self
+    }
+
+    /// Sets the client credential exchanged for a token at the token endpoint.
+    pub fn with_credential(mut self, client_id: Option<String>, client_secret: 
String) -> Self {
+        self.init_params.credential = Some((client_id, client_secret));
+        self
+    }
+
+    /// Sets the HTTP client used for token requests.
+    pub fn with_client(mut self, client: Client) -> Self {
+        self.client = client;
+        self
+    }
+
+    /// Sets extra headers sent with token requests.
+    pub fn with_extra_headers(mut self, headers: HeaderMap) -> Self {
+        self.init_params.extra_headers = headers;
+        self
+    }
+
+    /// Adds extra OAuth2 form parameters (e.g. `scope`, `audience`), merged
+    /// onto the defaults: provide a `scope` entry to replace the default
+    /// `catalog` scope.
+    pub fn with_extra_oauth_params(mut self, params: HashMap<String, String>) 
-> Self {
+        self.init_params.extra_oauth_params.extend(params);
+        self
+    }
+
+    pub(crate) fn from_config(cfg: &RestCatalogConfig) -> Result<Self> {
+        Ok(Self {
+            client: cfg.client(),
+            token: Arc::new(Mutex::new(cfg.token())),
+            init_params: OAuth2Params {
+                extra_headers: cfg.extra_headers()?,
+                token_endpoint: cfg.get_token_endpoint(),
+                credential: cfg.credential(),
+                extra_oauth_params: cfg.extra_oauth_params(),
+            },
+            endpoint_is_default: cfg.explicit_oauth2_server_uri().is_none(),
+        })
+    }
+}
+
+impl Debug for OAuth2Manager {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("OAuth2Manager")
+            .field("token_endpoint", &self.init_params.token_endpoint)
+            .finish_non_exhaustive()
+    }
+}
+
+#[async_trait]
+impl AuthManager for OAuth2Manager {
+    async fn init_session(&self) -> Result<Arc<dyn AuthSession>> {
+        Ok(Arc::new(OAuth2Session {
+            client: self.client.clone(),
+            token: self.token.clone(),
+            params: self.init_params.clone(),
+        }))
+    }
+
+    async fn catalog_session(
+        &self,
+        props: &HashMap<String, String>,
+    ) -> Result<Arc<dyn AuthSession>> {
+        // The server config may carry a new token (or restate the user's).
+        if let Some(token) = props.get("token") {
+            *self.token.lock().await = Some(token.clone());
+        }
+
+        // Explicit property overrides merge ONTO the manager's options, so an
+        // injected manager keeps whatever a property doesn't override.
+        let mut extra_headers = self.init_params.extra_headers.clone();
+        extra_headers.extend(explicit_headers_from_props(props)?);
+
+        let mut extra_oauth_params = 
self.init_params.extra_oauth_params.clone();
+        for key in ["scope", "audience", "resource"] {
+            if let Some(value) = props.get(key) {
+                extra_oauth_params.insert(key.to_string(), value.to_string());
+            }
+        }
+
+        let token_endpoint = match props.get("oauth2-server-uri") {
+            Some(uri) if !uri.is_empty() => uri.clone(),
+            // A default endpoint follows the merged catalog URI (which
+            // `/v1/config` may have overridden); explicit ones are kept.
+            _ if self.endpoint_is_default => props
+                .get(REST_CATALOG_PROP_URI)
+                .map(|uri| default_token_endpoint(uri))
+                .unwrap_or_else(|| self.init_params.token_endpoint.clone()),
+            _ => self.init_params.token_endpoint.clone(),
+        };
+
+        Ok(Arc::new(OAuth2Session {
+            client: self.client.clone(),
+            token: self.token.clone(),
+            params: OAuth2Params {
+                extra_headers,
+                token_endpoint,
+                credential: credential_from_props(props)
+                    .or_else(|| self.init_params.credential.clone()),
+                extra_oauth_params,
+            },
+        }))
+    }
+}
+
+/// [`AuthSession`] adding a `Authorization: Bearer <token>` header.
+struct OAuth2Session {
+    client: Client,
+    /// Cached bearer token, shared with the owning [`OAuth2Manager`].
+    token: Arc<Mutex<Option<String>>>,
+    params: OAuth2Params,
+}
+
+impl Debug for OAuth2Session {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("OAuth2Session")
+            .field("token_endpoint", &self.params.token_endpoint)
+            .finish_non_exhaustive()
+    }
+}
+
+impl OAuth2Session {
+    async fn exchange_credential_for_token(&self) -> Result<String> {
+        // Credential must exist here.
+        let (client_id, client_secret) = 
self.params.credential.as_ref().ok_or_else(|| {
+            Error::new(
+                ErrorKind::DataInvalid,
+                "Credential must be provided for authentication",
+            )
+        })?;
+
+        let mut params = HashMap::with_capacity(4);
+        params.insert("grant_type", "client_credentials");
+        if let Some(client_id) = client_id {
+            params.insert("client_id", client_id);
+        }
+        params.insert("client_secret", client_secret);
+        params.extend(
+            self.params
+                .extra_oauth_params
+                .iter()
+                .map(|(k, v)| (k.as_str(), v.as_str())),
+        );
+
+        let mut auth_req = self
+            .client
+            .request(Method::POST, &self.params.token_endpoint)
+            .headers(self.params.extra_headers.clone())
+            .form(&params)
+            .build()?;
+        // extra headers add content-type application/json header it's 
necessary to override it with proper type
+        // note that form call doesn't add content-type header if already 
present
+        auth_req.headers_mut().insert(
+            http::header::CONTENT_TYPE,
+            
http::HeaderValue::from_static("application/x-www-form-urlencoded"),
+        );
+        let auth_url = auth_req.url().clone();
+        let auth_resp = self.client.execute(auth_req).await?;
+
+        let auth_res: TokenResponse = if auth_resp.status() == StatusCode::OK {
+            let text = auth_resp
+                .bytes()
+                .await
+                .map_err(|err| err.with_url(auth_url.clone()))?;
+            Ok(serde_json::from_slice(&text).map_err(|e| {
+                Error::new(
+                    ErrorKind::Unexpected,
+                    "Failed to parse response from rest catalog server!",
+                )
+                .with_context("operation", "auth")
+                .with_context("url", auth_url.to_string())
+                .with_context("json", String::from_utf8_lossy(&text))
+                .with_source(e)
+            })?)
+        } else {
+            let code = auth_resp.status();
+            let text = auth_resp
+                .bytes()
+                .await
+                .map_err(|err| err.with_url(auth_url.clone()))?;
+            let e: ErrorResponse = serde_json::from_slice(&text).map_err(|e| {
+                Error::new(ErrorKind::Unexpected, "Received unexpected 
response")
+                    .with_context("code", code.to_string())
+                    .with_context("operation", "auth")
+                    .with_context("url", auth_url.to_string())
+                    .with_context("json", String::from_utf8_lossy(&text))
+                    .with_source(e)
+            })?;
+            Err(Error::from(e))
+        }?;
+        Ok(auth_res.access_token)
+    }
+}
+
+#[async_trait]
+impl AuthSession for OAuth2Session {
+    /// Adds a bearer token to the authorization header.
+    ///
+    /// Three modes:
+    ///
+    /// 1. **No authentication** - Skip when both `credential` and `token` are 
missing.
+    /// 2. **Token authentication** - Use the provided `token` directly.
+    /// 3. **OAuth authentication** - Exchange `credential` for a token, cache 
it, then use it.

Review Comment:
   +1. I'm a bit confused by the "modes" here, I think we have NoopSession in 
this PR already



##########
crates/catalog/rest/src/auth/mod.rs:
##########
@@ -0,0 +1,313 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Pluggable authentication for the REST catalog, mirroring Iceberg Java's
+//! `AuthManager`/`AuthSession` API.
+
+mod oauth2;
+
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use http::{HeaderMap, Method};
+use iceberg::Result;
+pub use oauth2::OAuth2Manager;
+use reqwest::Request;
+
+/// `rest.auth.type` value disabling authentication.
+pub const AUTH_TYPE_NONE: &str = "none";
+/// `rest.auth.type` value selecting OAuth2 token authentication.
+pub const AUTH_TYPE_OAUTH2: &str = "oauth2";
+
+/// Creates the [`AuthSession`]s used to authenticate REST catalog requests.
+///
+/// A manager is created once per catalog, either from the `rest.auth.type`
+/// property or injected through `RestCatalogBuilder::with_auth_manager`, and
+/// lives for the lifetime of the catalog.
+#[async_trait]
+pub trait AuthManager: Debug + Send + Sync {
+    /// Session used for the initial `/v1/config` handshake, built from the
+    /// user-supplied configuration.
+    ///
+    /// Returns a [`Box`]: an init session is used once and released, unlike
+    /// the shared [`AuthManager::catalog_session`].
+    async fn init_session(&self) -> Result<Box<dyn AuthSession>>;

Review Comment:
   If we are planning to move auth manager and session out side of client, how 
do we ping `/v1/config`?  we just pass all configs from catalog to 
Oauth2Manager::new()? 



##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -341,6 +309,32 @@ impl RestCatalogConfig {
             .unwrap_or(false)
     }
 
+    /// The configured auth scheme: explicit `rest.auth.type` or the default
+    /// `oauth2` (which behaves as no auth when neither `token` nor
+    /// `credential` is set).
+    fn auth_type(&self) -> String {
+        self.props
+            .get(REST_CATALOG_PROP_AUTH_TYPE)
+            .cloned()
+            .unwrap_or_else(|| AUTH_TYPE_OAUTH2.to_string())

Review Comment:
   I'm actually leaning toward using none as default here. Users should be 
aware of what auth type they are using when they absolutely need to use an auth 
manager



##########
crates/catalog/rest/src/auth/oauth2.rs:
##########
@@ -0,0 +1,350 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::fmt::{Debug, Formatter};
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use http::StatusCode;
+use iceberg::{Error, ErrorKind, Result};
+use reqwest::header::HeaderMap;
+use reqwest::{Client, Method};
+use tokio::sync::Mutex;
+
+use super::{AuthManager, AuthRequest, AuthSession};
+use crate::catalog::{
+    REST_CATALOG_PROP_URI, RestCatalogConfig, credential_from_props, 
default_token_endpoint,
+    explicit_headers_from_props,
+};
+use crate::types::{ErrorResponse, TokenResponse};
+
+/// Per-phase OAuth2 parameters (init vs. post-handshake catalog phase).
+#[derive(Clone)]
+struct OAuth2Params {
+    extra_headers: HeaderMap,
+    token_endpoint: String,
+    credential: Option<(Option<String>, String)>,

Review Comment:
   Looks like a good idea to me



##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -341,6 +306,32 @@ impl RestCatalogConfig {
             .unwrap_or(false)
     }
 
+    /// The configured auth scheme: explicit `rest.auth.type` or the default
+    /// `oauth2` (which behaves as no auth when neither `token` nor
+    /// `credential` is set).
+    fn auth_type(&self) -> String {
+        self.props
+            .get(REST_CATALOG_PROP_AUTH_TYPE)
+            .cloned()
+            .unwrap_or_else(|| AUTH_TYPE_OAUTH2.to_string())
+    }
+
+    /// Resolves the auth manager: a `with_auth_manager` override wins,
+    /// otherwise one is built from the `rest.auth.type` configuration.
+    pub(crate) fn resolve_auth_manager(&self) -> Result<Arc<dyn AuthManager>> {
+        if let Some(auth_manager) = &self.auth_manager {
+            return Ok(auth_manager.clone());
+        }
+        match self.auth_type().as_str() {
+            AUTH_TYPE_NONE => Ok(Arc::new(NoopAuthManager)),
+            AUTH_TYPE_OAUTH2 => 
Ok(Arc::new(OAuth2Manager::from_config(self)?)),
+            other => Err(Error::new(
+                ErrorKind::DataInvalid,
+                format!("unknown '{REST_CATALOG_PROP_AUTH_TYPE}': {other}"),

Review Comment:
   we should give hint to users and ask them to use `with_auth_manager` in 
CatalogBuilder to inject custom auth manager



##########
crates/catalog/rest/src/auth/oauth2.rs:
##########
@@ -0,0 +1,350 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::fmt::{Debug, Formatter};
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use http::StatusCode;
+use iceberg::{Error, ErrorKind, Result};
+use reqwest::header::HeaderMap;
+use reqwest::{Client, Method};
+use tokio::sync::Mutex;
+
+use super::{AuthManager, AuthRequest, AuthSession};
+use crate::catalog::{
+    REST_CATALOG_PROP_URI, RestCatalogConfig, credential_from_props, 
default_token_endpoint,
+    explicit_headers_from_props,
+};
+use crate::types::{ErrorResponse, TokenResponse};
+
+/// Per-phase OAuth2 parameters (init vs. post-handshake catalog phase).
+#[derive(Clone)]
+struct OAuth2Params {
+    extra_headers: HeaderMap,
+    token_endpoint: String,
+    credential: Option<(Option<String>, String)>,
+    extra_oauth_params: HashMap<String, String>,
+}
+
+/// [`AuthManager`] implementing the OAuth2 client-credentials flow used by
+/// Iceberg REST catalogs.
+///
+/// A configured `token` is used directly; otherwise `credential` is exchanged
+/// for a token at the token endpoint and cached. The cached token is shared
+/// across sessions so it survives the config handshake.
+pub struct OAuth2Manager {
+    client: Client,
+    token: Arc<Mutex<Option<String>>>,
+    init_params: OAuth2Params,
+    /// True when the token endpoint was derived from the catalog URI (not
+    /// explicitly configured): it is then recomputed from the merged URI in
+    /// [`Self::catalog_session`], since `/v1/config` may override the URI.
+    endpoint_is_default: bool,
+}
+
+impl OAuth2Manager {
+    /// Creates a manager exchanging credentials at `token_endpoint`, with no
+    /// token or credential configured. Combine with the `with_*` methods:
+    ///
+    /// ```rust,ignore
+    /// let manager = 
OAuth2Manager::new("https://auth.example.com/v1/oauth/tokens";)
+    ///     .with_credential(Some("client-id".into()), "client-secret".into());
+    /// ```
+    pub fn new(token_endpoint: impl Into<String>) -> Self {
+        Self {
+            client: Client::default(),
+            token: Arc::new(Mutex::new(None)),
+            init_params: OAuth2Params {
+                extra_headers: HeaderMap::new(),
+                token_endpoint: token_endpoint.into(),
+                credential: None,
+                // Same default as the configuration path: the catalog scope.
+                extra_oauth_params: HashMap::from([("scope".to_string(), 
"catalog".to_string())]),
+            },
+            endpoint_is_default: false,
+        }
+    }
+
+    /// Sets a bearer token used directly (takes precedence over `credential`).
+    pub fn with_token(mut self, token: impl Into<String>) -> Self {
+        self.token = Arc::new(Mutex::new(Some(token.into())));
+        self
+    }
+
+    /// Sets the client credential exchanged for a token at the token endpoint.
+    pub fn with_credential(mut self, client_id: Option<String>, client_secret: 
String) -> Self {
+        self.init_params.credential = Some((client_id, client_secret));
+        self
+    }
+
+    /// Sets the HTTP client used for token requests.
+    pub fn with_client(mut self, client: Client) -> Self {
+        self.client = client;
+        self
+    }
+
+    /// Sets extra headers sent with token requests.
+    pub fn with_extra_headers(mut self, headers: HeaderMap) -> Self {
+        self.init_params.extra_headers = headers;
+        self
+    }
+
+    /// Adds extra OAuth2 form parameters (e.g. `scope`, `audience`), merged
+    /// onto the defaults: provide a `scope` entry to replace the default
+    /// `catalog` scope.
+    pub fn with_extra_oauth_params(mut self, params: HashMap<String, String>) 
-> Self {
+        self.init_params.extra_oauth_params.extend(params);
+        self
+    }
+
+    pub(crate) fn from_config(cfg: &RestCatalogConfig) -> Result<Self> {
+        Ok(Self {
+            client: cfg.client(),
+            token: Arc::new(Mutex::new(cfg.token())),
+            init_params: OAuth2Params {
+                extra_headers: cfg.extra_headers()?,
+                token_endpoint: cfg.get_token_endpoint(),
+                credential: cfg.credential(),
+                extra_oauth_params: cfg.extra_oauth_params(),
+            },
+            endpoint_is_default: cfg.explicit_oauth2_server_uri().is_none(),
+        })
+    }
+}
+
+impl Debug for OAuth2Manager {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("OAuth2Manager")
+            .field("token_endpoint", &self.init_params.token_endpoint)
+            .finish_non_exhaustive()
+    }
+}
+
+#[async_trait]
+impl AuthManager for OAuth2Manager {
+    async fn init_session(&self) -> Result<Arc<dyn AuthSession>> {
+        Ok(Arc::new(OAuth2Session {
+            client: self.client.clone(),
+            token: self.token.clone(),
+            params: self.init_params.clone(),
+        }))
+    }
+
+    async fn catalog_session(
+        &self,
+        props: &HashMap<String, String>,
+    ) -> Result<Arc<dyn AuthSession>> {
+        // The server config may carry a new token (or restate the user's).
+        if let Some(token) = props.get("token") {
+            *self.token.lock().await = Some(token.clone());
+        }
+
+        // Explicit property overrides merge ONTO the manager's options, so an
+        // injected manager keeps whatever a property doesn't override.
+        let mut extra_headers = self.init_params.extra_headers.clone();
+        extra_headers.extend(explicit_headers_from_props(props)?);
+
+        let mut extra_oauth_params = 
self.init_params.extra_oauth_params.clone();
+        for key in ["scope", "audience", "resource"] {
+            if let Some(value) = props.get(key) {
+                extra_oauth_params.insert(key.to_string(), value.to_string());
+            }
+        }
+
+        let token_endpoint = match props.get("oauth2-server-uri") {
+            Some(uri) if !uri.is_empty() => uri.clone(),
+            // A default endpoint follows the merged catalog URI (which
+            // `/v1/config` may have overridden); explicit ones are kept.
+            _ if self.endpoint_is_default => props
+                .get(REST_CATALOG_PROP_URI)
+                .map(|uri| default_token_endpoint(uri))
+                .unwrap_or_else(|| self.init_params.token_endpoint.clone()),
+            _ => self.init_params.token_endpoint.clone(),
+        };
+
+        Ok(Arc::new(OAuth2Session {
+            client: self.client.clone(),
+            token: self.token.clone(),
+            params: OAuth2Params {
+                extra_headers,
+                token_endpoint,
+                credential: credential_from_props(props)
+                    .or_else(|| self.init_params.credential.clone()),
+                extra_oauth_params,
+            },
+        }))
+    }
+}
+
+/// [`AuthSession`] adding a `Authorization: Bearer <token>` header.
+struct OAuth2Session {
+    client: Client,
+    /// Cached bearer token, shared with the owning [`OAuth2Manager`].
+    token: Arc<Mutex<Option<String>>>,
+    params: OAuth2Params,
+}
+
+impl Debug for OAuth2Session {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("OAuth2Session")
+            .field("token_endpoint", &self.params.token_endpoint)
+            .finish_non_exhaustive()
+    }
+}
+
+impl OAuth2Session {
+    async fn exchange_credential_for_token(&self) -> Result<String> {
+        // Credential must exist here.
+        let (client_id, client_secret) = 
self.params.credential.as_ref().ok_or_else(|| {
+            Error::new(
+                ErrorKind::DataInvalid,
+                "Credential must be provided for authentication",
+            )
+        })?;
+
+        let mut params = HashMap::with_capacity(4);
+        params.insert("grant_type", "client_credentials");
+        if let Some(client_id) = client_id {
+            params.insert("client_id", client_id);
+        }
+        params.insert("client_secret", client_secret);
+        params.extend(
+            self.params
+                .extra_oauth_params
+                .iter()
+                .map(|(k, v)| (k.as_str(), v.as_str())),
+        );
+
+        let mut auth_req = self
+            .client
+            .request(Method::POST, &self.params.token_endpoint)
+            .headers(self.params.extra_headers.clone())
+            .form(&params)
+            .build()?;
+        // extra headers add content-type application/json header it's 
necessary to override it with proper type
+        // note that form call doesn't add content-type header if already 
present
+        auth_req.headers_mut().insert(
+            http::header::CONTENT_TYPE,
+            
http::HeaderValue::from_static("application/x-www-form-urlencoded"),
+        );
+        let auth_url = auth_req.url().clone();
+        let auth_resp = self.client.execute(auth_req).await?;
+
+        let auth_res: TokenResponse = if auth_resp.status() == StatusCode::OK {
+            let text = auth_resp
+                .bytes()
+                .await
+                .map_err(|err| err.with_url(auth_url.clone()))?;
+            Ok(serde_json::from_slice(&text).map_err(|e| {
+                Error::new(
+                    ErrorKind::Unexpected,
+                    "Failed to parse response from rest catalog server!",
+                )
+                .with_context("operation", "auth")
+                .with_context("url", auth_url.to_string())
+                .with_context("json", String::from_utf8_lossy(&text))
+                .with_source(e)
+            })?)
+        } else {
+            let code = auth_resp.status();
+            let text = auth_resp
+                .bytes()
+                .await
+                .map_err(|err| err.with_url(auth_url.clone()))?;
+            let e: ErrorResponse = serde_json::from_slice(&text).map_err(|e| {
+                Error::new(ErrorKind::Unexpected, "Received unexpected 
response")
+                    .with_context("code", code.to_string())
+                    .with_context("operation", "auth")
+                    .with_context("url", auth_url.to_string())
+                    .with_context("json", String::from_utf8_lossy(&text))
+                    .with_source(e)
+            })?;
+            Err(Error::from(e))
+        }?;
+        Ok(auth_res.access_token)
+    }
+}
+
+#[async_trait]
+impl AuthSession for OAuth2Session {
+    /// Adds a bearer token to the authorization header.
+    ///
+    /// Three modes:
+    ///
+    /// 1. **No authentication** - Skip when both `credential` and `token` are 
missing.
+    /// 2. **Token authentication** - Use the provided `token` directly.
+    /// 3. **OAuth authentication** - Exchange `credential` for a token, cache 
it, then use it.
+    ///
+    /// When both `credential` and `token` are present, `token` takes 
precedence.
+    ///
+    /// # TODO: Support automatic token refreshing.
+    async fn authenticate(&self, req: &mut AuthRequest<'_>) -> Result<()> {
+        // Clone the token from lock without holding the lock for entire 
function.
+        let token = self.token.lock().await.clone();

Review Comment:
   +1, the lock should be held at least until the token is exchanged



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