Skip to content

Commit 0e1fe8a

Browse files
himadripalhpal
andauthored
Add scope as configurable option (#484)
added scope as configurable option, defaults to `CATALOG_SCOPE` resolve conflicts. change to constant style fix Co-authored-by: hpal <[email protected]>
1 parent 7688633 commit 0e1fe8a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

pyiceberg/catalog/rest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,11 @@ def _fetch_access_token(self, session: Session, credential: str) -> str:
294294
client_id, client_secret = credential.split(SEMICOLON)
295295
else:
296296
client_id, client_secret = None, credential
297-
data = {GRANT_TYPE: CLIENT_CREDENTIALS, CLIENT_ID: client_id, CLIENT_SECRET: client_secret, SCOPE: CATALOG_SCOPE}
297+
298+
# take scope from properties or use default CATALOG_SCOPE
299+
scope = self.properties.get(SCOPE) or CATALOG_SCOPE
300+
301+
data = {GRANT_TYPE: CLIENT_CREDENTIALS, CLIENT_ID: client_id, CLIENT_SECRET: client_secret, SCOPE: scope}
298302
response = session.post(
299303
url=self.auth_url, data=data, headers={**session.headers, "Content-type": "application/x-www-form-urlencoded"}
300304
)

tests/catalog/test_rest.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
TEST_CREDENTIALS = "client:secret"
4747
TEST_AUTH_URL = "https://auth-endpoint/"
4848
TEST_TOKEN = "some_jwt_token"
49+
TEST_SCOPE = "openid_offline_corpds_ds_profile"
4950
TEST_HEADERS = {
5051
"Content-type": "application/json",
5152
"X-Client-Version": "0.14.1",
@@ -136,6 +137,43 @@ def test_token_200_without_optional_fields(rest_mock: Mocker) -> None:
136137
)
137138

138139

140+
def test_token_with_default_scope(rest_mock: Mocker) -> None:
141+
mock_request = rest_mock.post(
142+
f"{TEST_URI}v1/oauth/tokens",
143+
json={
144+
"access_token": TEST_TOKEN,
145+
"token_type": "Bearer",
146+
"expires_in": 86400,
147+
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
148+
},
149+
status_code=200,
150+
request_headers=OAUTH_TEST_HEADERS,
151+
)
152+
assert (
153+
RestCatalog("rest", uri=TEST_URI, credential=TEST_CREDENTIALS)._session.headers["Authorization"] == f"Bearer {TEST_TOKEN}"
154+
)
155+
assert "catalog" in mock_request.last_request.text
156+
157+
158+
def test_token_with_custom_scope(rest_mock: Mocker) -> None:
159+
mock_request = rest_mock.post(
160+
f"{TEST_URI}v1/oauth/tokens",
161+
json={
162+
"access_token": TEST_TOKEN,
163+
"token_type": "Bearer",
164+
"expires_in": 86400,
165+
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
166+
},
167+
status_code=200,
168+
request_headers=OAUTH_TEST_HEADERS,
169+
)
170+
assert (
171+
RestCatalog("rest", uri=TEST_URI, credential=TEST_CREDENTIALS, scope=TEST_SCOPE)._session.headers["Authorization"]
172+
== f"Bearer {TEST_TOKEN}"
173+
)
174+
assert TEST_SCOPE in mock_request.last_request.text
175+
176+
139177
def test_token_200_w_auth_url(rest_mock: Mocker) -> None:
140178
rest_mock.post(
141179
TEST_AUTH_URL,

0 commit comments

Comments
 (0)