Skip to content

Commit

Permalink
feat: Support optional headers for OAuth request (#1815)
Browse files Browse the repository at this point in the history
* Supporting optional headers for oauth.

* Optional OAuth only Headers with tests for None

---------

Co-authored-by: Edgar R. M <[email protected]>
  • Loading branch information
s7clarke10 and edgarrmondragon authored Jul 10, 2023
1 parent 17114e4 commit 1637b48
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion singer_sdk/authenticators.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ def __init__(
auth_endpoint: str | None = None,
oauth_scopes: str | None = None,
default_expiration: int | None = None,
oauth_headers: dict | None = None,
) -> None:
"""Create a new authenticator.
Expand All @@ -366,11 +367,13 @@ def __init__(
auth_endpoint: The OAuth 2.0 authorization endpoint.
oauth_scopes: A comma-separated list of OAuth scopes.
default_expiration: Default token expiry in seconds.
oauth_headers: An optional dict of headers required to get a token.
"""
super().__init__(stream=stream)
self._auth_endpoint = auth_endpoint
self._default_expiration = default_expiration
self._oauth_scopes = oauth_scopes
self._oauth_headers = oauth_headers or {}

# Initialize internal tracking attributes
self.access_token: str | None = None
Expand Down Expand Up @@ -499,6 +502,7 @@ def update_access_token(self) -> None:
auth_request_payload = self.oauth_request_payload
token_response = requests.post(
self.auth_endpoint,
headers=self._oauth_headers,
data=auth_request_payload,
timeout=60,
)
Expand All @@ -512,7 +516,8 @@ def update_access_token(self) -> None:

token_json = token_response.json()
self.access_token = token_json["access_token"]
self.expires_in = token_json.get("expires_in", self._default_expiration)
expiration = token_json.get("expires_in", self._default_expiration)
self.expires_in = int(expiration) if expiration else None
if self.expires_in is None:
self.logger.debug(
"No expires_in receied in OAuth response and no "
Expand Down

0 comments on commit 1637b48

Please sign in to comment.