Skip to content

[ISSUE #27605] support json as auth token refresh query #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,21 @@ def _make_handled_request(self) -> Any:
Exception: For any other exceptions that occur during the request.
"""
try:
response = requests.request(
method="POST",
url=self.get_token_refresh_endpoint(), # type: ignore # returns None, if not provided, but str | bytes is expected.
data=self.build_refresh_request_body(),
headers=self.build_refresh_request_headers(),
)
headers = self.build_refresh_request_headers()
if self._is_application_json(headers):
response = requests.request(
method="POST",
url=self.get_token_refresh_endpoint(), # type: ignore # returns None, if not provided, but str | bytes is expected.
json=self.build_refresh_request_body(),
headers=headers,
)
else:
response = requests.request(
method="POST",
url=self.get_token_refresh_endpoint(), # type: ignore # returns None, if not provided, but str | bytes is expected.
data=self.build_refresh_request_body(),
headers=headers,
)
# log the response even if the request failed for troubleshooting purposes
self._log_response(response)
response.raise_for_status()
Expand All @@ -234,6 +243,16 @@ def _make_handled_request(self) -> Any:
except Exception as e:
raise Exception(f"Error while refreshing access token: {e}") from e

@staticmethod
def _is_application_json(headers: Mapping[str, Any] | None) -> bool:
if not headers:
return False

for key, value in headers.items():
if key.lower() == "content-type" and value.lower() == "application/json":
return True
return False

def _ensure_access_token_in_response(self, response_data: Mapping[str, Any]) -> None:
"""
Ensures that the access token is present in the response data.
Expand Down
36 changes: 35 additions & 1 deletion unit_tests/sources/declarative/auth/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import base64
import json
import logging
from datetime import timedelta, timezone
from datetime import timedelta
from unittest.mock import Mock

import freezegun
Expand Down Expand Up @@ -400,6 +400,40 @@ def test_no_expiry_date_provided_by_auth_server(
assert oauth.access_token == expected_access_token
assert oauth._token_expiry_date == expected_new_expiry_date

@freezegun.freeze_time("2022-01-01")
def test_given_content_type_application_json_when_refresh_token_then_send_request_as_json(
self,
) -> None:
oauth = DeclarativeOauth2Authenticator(
token_refresh_endpoint="https://refresh_endpoint.com/",
refresh_request_headers={"Content-type": "application/json"},
client_id="some_client_id",
client_secret="some_client_secret",
refresh_token="some_refresh_token",
config={},
parameters={},
grant_type="client",
)

with HttpMocker() as http_mocker:
http_mocker.post(
HttpRequest(
url="https://refresh_endpoint.com/",
body=json.dumps(
{
"grant_type": "client",
"client_id": "some_client_id",
"client_secret": "some_client_secret",
"refresh_token": "some_refresh_token",
}
),
),
HttpResponse(body=json.dumps({"access_token": "new_access_token"})),
)
oauth.get_access_token()

assert oauth.access_token == "new_access_token"

@pytest.mark.parametrize(
"expires_in_response, token_expiry_date_format",
[
Expand Down
Loading