Skip to content

Commit 6b144d4

Browse files
author
maxi297
committed
[ISSUE #27605] support json as auth token refresh query
1 parent 0006ceb commit 6b144d4

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

airbyte_cdk/sources/streams/http/requests_native_auth/abstract_oauth.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,21 @@ def _make_handled_request(self) -> Any:
211211
Exception: For any other exceptions that occur during the request.
212212
"""
213213
try:
214-
response = requests.request(
215-
method="POST",
216-
url=self.get_token_refresh_endpoint(), # type: ignore # returns None, if not provided, but str | bytes is expected.
217-
data=self.build_refresh_request_body(),
218-
headers=self.build_refresh_request_headers(),
219-
)
214+
headers = self.build_refresh_request_headers()
215+
if self._is_application_json(headers):
216+
response = requests.request(
217+
method="POST",
218+
url=self.get_token_refresh_endpoint(), # type: ignore # returns None, if not provided, but str | bytes is expected.
219+
json=self.build_refresh_request_body(),
220+
headers=headers,
221+
)
222+
else:
223+
response = requests.request(
224+
method="POST",
225+
url=self.get_token_refresh_endpoint(), # type: ignore # returns None, if not provided, but str | bytes is expected.
226+
data=self.build_refresh_request_body(),
227+
headers=headers,
228+
)
220229
# log the response even if the request failed for troubleshooting purposes
221230
self._log_response(response)
222231
response.raise_for_status()
@@ -234,6 +243,16 @@ def _make_handled_request(self) -> Any:
234243
except Exception as e:
235244
raise Exception(f"Error while refreshing access token: {e}") from e
236245

246+
@staticmethod
247+
def _is_application_json(headers: Mapping[str, Any] | None) -> bool:
248+
if not headers:
249+
return False
250+
251+
for key, value in headers.items():
252+
if key.lower() == "content-type" and value.lower() == "application/json":
253+
return True
254+
return False
255+
237256
def _ensure_access_token_in_response(self, response_data: Mapping[str, Any]) -> None:
238257
"""
239258
Ensures that the access token is present in the response data.

unit_tests/sources/declarative/auth/test_oauth.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import base64
66
import json
77
import logging
8-
from datetime import timedelta, timezone
8+
from datetime import timedelta
99
from unittest.mock import Mock
1010

1111
import freezegun
@@ -400,6 +400,38 @@ def test_no_expiry_date_provided_by_auth_server(
400400
assert oauth.access_token == expected_access_token
401401
assert oauth._token_expiry_date == expected_new_expiry_date
402402

403+
@freezegun.freeze_time("2022-01-01")
404+
def test_given_content_type_application_json_when_refresh_token_then_send_request_as_json(
405+
self,
406+
) -> None:
407+
oauth = DeclarativeOauth2Authenticator(
408+
token_refresh_endpoint="https://refresh_endpoint.com/",
409+
refresh_request_headers={"Content-type": "application/json"},
410+
client_id="some_client_id",
411+
client_secret="some_client_secret",
412+
refresh_token="some_refresh_token",
413+
config={},
414+
parameters={},
415+
grant_type="client",
416+
)
417+
418+
with HttpMocker() as http_mocker:
419+
http_mocker.post(
420+
HttpRequest(
421+
url="https://refresh_endpoint.com/",
422+
body=json.dumps({
423+
"grant_type": "client",
424+
"client_id": "some_client_id",
425+
"client_secret": "some_client_secret",
426+
"refresh_token": "some_refresh_token",
427+
}),
428+
),
429+
HttpResponse(body=json.dumps({"access_token": "new_access_token"})),
430+
)
431+
oauth.get_access_token()
432+
433+
assert oauth.access_token == "new_access_token"
434+
403435
@pytest.mark.parametrize(
404436
"expires_in_response, token_expiry_date_format",
405437
[

0 commit comments

Comments
 (0)