You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by ze...@apache.org on 2026/03/31 20:23:29 UTC

(iceberg-go) branch main updated: feat(rest): add audience and resource params to OAuth requests (#815)

This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 7493f2c6 feat(rest): add audience and resource params to OAuth requests (#815)
7493f2c6 is described below

commit 7493f2c671674decd71b059896cb3bd7bc076ccf
Author: Lovro Mažgon <lo...@gmail.com>
AuthorDate: Tue Mar 31 22:23:24 2026 +0200

    feat(rest): add audience and resource params to OAuth requests (#815)
    
    - Add `WithAudience` and `WithResource` options to the REST catalog
    client
    - These are forwarded as form parameters in OAuth2 token requests, per
    [RFC 8707](https://datatracker.ietf.org/doc/html/rfc8707) and [RFC
    8693](https://datatracker.ietf.org/doc/html/rfc8693)
    - Refactor `TestScope` into a table-driven `TestOAuthTokenRequestParams`
    covering scope, audience, and resource combinations
---
 catalog/rest/options.go            |  18 +++++
 catalog/rest/rest.go               |  16 +++++
 catalog/rest/rest_internal_test.go | 133 ++++++++++++++++++++++++++++---------
 3 files changed, 134 insertions(+), 33 deletions(-)

diff --git a/catalog/rest/options.go b/catalog/rest/options.go
index 7f5add71..04e13d09 100644
--- a/catalog/rest/options.go
+++ b/catalog/rest/options.go
@@ -115,6 +115,22 @@ func WithScope(scope string) Option {
 	}
 }
 
+// WithAudience sets the audience parameter sent in OAuth token requests.
+// See RFC 8693 for details on token exchange audiences.
+func WithAudience(audience string) Option {
+	return func(o *options) {
+		o.audience = audience
+	}
+}
+
+// WithResource sets the resource parameter sent in OAuth token requests.
+// See RFC 8707 for details on resource indicators.
+func WithResource(resource string) Option {
+	return func(o *options) {
+		o.resource = resource
+	}
+}
+
 func WithAdditionalProps(props iceberg.Properties) Option {
 	return func(o *options) {
 		o.additionalProps = props
@@ -144,6 +160,8 @@ type options struct {
 	prefix            string
 	authUri           *url.URL
 	scope             string
+	audience          string
+	resource          string
 	transport         http.RoundTripper
 	headers           map[string]string
 
diff --git a/catalog/rest/rest.go b/catalog/rest/rest.go
index 104ff16f..aaa6f1cd 100644
--- a/catalog/rest/rest.go
+++ b/catalog/rest/rest.go
@@ -65,6 +65,8 @@ const (
 	keyMetadataLocation  = "metadata_location"
 	keyOauthCredential   = "credential"
 	keyScope             = "scope"
+	keyAudience          = "audience"
+	keyResource          = "resource"
 
 	authorizationHeader = "Authorization"
 	bearerPrefix        = "Bearer"
@@ -427,6 +429,10 @@ func fromProps(props iceberg.Properties, o *options) {
 			o.credential = v
 		case keyScope:
 			o.scope = v
+		case keyAudience:
+			o.audience = v
+		case keyResource:
+			o.resource = v
 		case keyPrefix:
 			o.prefix = v
 		case keyTlsSkipVerify:
@@ -557,6 +563,16 @@ func setupOAuthManager(r *Catalog, cl *http.Client, opts *options) AuthManager {
 	}
 	cfg.Scopes = []string{scope}
 
+	if opts.audience != "" || opts.resource != "" {
+		cfg.EndpointParams = url.Values{}
+		if opts.audience != "" {
+			cfg.EndpointParams.Set("audience", opts.audience)
+		}
+		if opts.resource != "" {
+			cfg.EndpointParams.Set("resource", opts.resource)
+		}
+	}
+
 	// Add skip oauth so we don't get in cycles trying to refresh the token
 	ctx := context.WithValue(context.Background(), skipOAuth, true)
 	ctx = context.WithValue(ctx, oauth2.HTTPClient, cl)
diff --git a/catalog/rest/rest_internal_test.go b/catalog/rest/rest_internal_test.go
index bad6060b..274040a6 100644
--- a/catalog/rest/rest_internal_test.go
+++ b/catalog/rest/rest_internal_test.go
@@ -119,46 +119,113 @@ func TestTokenAuthenticationPriority(t *testing.T) {
 	})
 }
 
-func TestScope(t *testing.T) {
+func TestOAuthTokenRequestParams(t *testing.T) {
 	t.Parallel()
-	mux := http.NewServeMux()
-	srv := httptest.NewServer(mux)
 
-	mux.HandleFunc("/v1/config", func(w http.ResponseWriter, r *http.Request) {
-		json.NewEncoder(w).Encode(map[string]any{
-			"defaults": map[string]any{}, "overrides": map[string]any{},
-		})
-	})
+	tests := []struct {
+		name   string
+		opts   []Option
+		expect map[string]string
+		absent []string
+	}{
+		{
+			name: "default scope",
+			expect: map[string]string{
+				"scope": "catalog",
+			},
+			absent: []string{"audience", "resource"},
+		},
+		{
+			name: "custom scope",
+			opts: []Option{WithScope("my_scope")},
+			expect: map[string]string{
+				"scope": "my_scope",
+			},
+			absent: []string{"audience", "resource"},
+		},
+		{
+			name: "audience only",
+			opts: []Option{WithAudience("my-aud")},
+			expect: map[string]string{
+				"scope":    "catalog",
+				"audience": "my-aud",
+			},
+			absent: []string{"resource"},
+		},
+		{
+			name: "resource only",
+			opts: []Option{WithResource("my-res")},
+			expect: map[string]string{
+				"scope":    "catalog",
+				"resource": "my-res",
+			},
+			absent: []string{"audience"},
+		},
+		{
+			name: "audience and resource",
+			opts: []Option{WithAudience("my-aud"), WithResource("my-res")},
+			expect: map[string]string{
+				"scope":    "catalog",
+				"audience": "my-aud",
+				"resource": "my-res",
+			},
+		},
+		{
+			name: "scope, audience and resource",
+			opts: []Option{WithScope("s"), WithAudience("a"), WithResource("r")},
+			expect: map[string]string{
+				"scope":    "s",
+				"audience": "a",
+				"resource": "r",
+			},
+		},
+	}
 
-	mux.HandleFunc("/v1/oauth/tokens", func(w http.ResponseWriter, req *http.Request) {
-		assert.Equal(t, http.MethodPost, req.Method)
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			t.Parallel()
+			mux := http.NewServeMux()
+			srv := httptest.NewServer(mux)
+			defer srv.Close()
+
+			mux.HandleFunc("/v1/config", func(w http.ResponseWriter, r *http.Request) {
+				err := json.NewEncoder(w).Encode(map[string]any{
+					"defaults": map[string]any{}, "overrides": map[string]any{},
+				})
+				assert.NoError(t, err)
+			})
 
-		assert.Equal(t, req.Header.Get("Content-Type"), "application/x-www-form-urlencoded")
+			mux.HandleFunc("/v1/oauth/tokens", func(w http.ResponseWriter, req *http.Request) {
+				assert.Equal(t, http.MethodPost, req.Method)
+				assert.Equal(t, "application/x-www-form-urlencoded", req.Header.Get("Content-Type"))
 
-		require.NoError(t, req.ParseForm())
-		values := req.PostForm
-		assert.Equal(t, "client_credentials", values.Get("grant_type"))
-		assert.Equal(t, "secret", values.Get("client_secret"))
-		assert.Equal(t, "my_scope", values.Get("scope"))
+				require.NoError(t, req.ParseForm())
+				values := req.PostForm
+				assert.Equal(t, "client_credentials", values.Get("grant_type"))
+				assert.Equal(t, "secret", values.Get("client_secret"))
 
-		w.Header().Set("Content-Type", "application/json")
-		json.NewEncoder(w).Encode(map[string]any{
-			"access_token":      "some_jwt_token",
-			"token_type":        "Bearer",
-			"expires_in":        86400,
-			"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
-		})
-	})
+				for k, v := range tt.expect {
+					assert.Equal(t, v, values.Get(k), "form param %s", k)
+				}
+				for _, k := range tt.absent {
+					assert.Empty(t, values.Get(k), "form param %s should be absent", k)
+				}
 
-	cat, err := NewCatalog(
-		context.Background(),
-		"rest",
-		srv.URL,
-		WithCredential("secret"),
-		WithScope("my_scope"),
-	)
-	require.NoError(t, err)
-	assert.NotNil(t, cat)
+				w.Header().Set("Content-Type", "application/json")
+				err := json.NewEncoder(w).Encode(map[string]any{
+					"access_token": "tok",
+					"token_type":   "Bearer",
+					"expires_in":   86400,
+				})
+				assert.NoError(t, err)
+			})
+
+			opts := append([]Option{WithCredential("secret")}, tt.opts...)
+			cat, err := NewCatalog(context.Background(), "rest", srv.URL, opts...)
+			require.NoError(t, err)
+			assert.NotNil(t, cat)
+		})
+	}
 }
 
 func TestAuthHeader(t *testing.T) {