Skip to content

Commit 71ab084

Browse files
committed
update config with access tokens
1 parent ee76bf0 commit 71ab084

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

airbyte-cdk/python/airbyte_cdk/sources/streams/http/requests_native_auth/oauth.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def __init__(
113113
grant_type: str = "refresh_token",
114114
client_id_config_path: Sequence[str] = ("credentials", "client_id"),
115115
client_secret_config_path: Sequence[str] = ("credentials", "client_secret"),
116+
access_token_config_path: Sequence[str] = ("credentials", "access_token"),
116117
refresh_token_config_path: Sequence[str] = ("credentials", "refresh_token"),
117118
):
118119
"""
@@ -129,10 +130,12 @@ def __init__(
129130
grant_type (str, optional): OAuth grant type. Defaults to "refresh_token".
130131
client_id_config_path (Sequence[str]): Dpath to the client_id field in the connector configuration. Defaults to ("credentials", "client_id").
131132
client_secret_config_path (Sequence[str]): Dpath to the client_secret field in the connector configuration. Defaults to ("credentials", "client_secret").
133+
access_token_config_path (Sequence[str]): Dpath to the access_token field in the connector configuration. Defaults to ("credentials", "access_token").
132134
refresh_token_config_path (Sequence[str]): Dpath to the refresh_token field in the connector configuration. Defaults to ("credentials", "refresh_token").
133135
"""
134136
self._client_id_config_path = client_id_config_path
135137
self._client_secret_config_path = client_secret_config_path
138+
self._access_token_config_path = access_token_config_path
136139
self._refresh_token_config_path = refresh_token_config_path
137140
self._refresh_token_name = refresh_token_name
138141
self._connector_config = observe_connector_config(connector_config)
@@ -181,14 +184,21 @@ def get_client_secret(self) -> str:
181184
def get_refresh_token(self) -> str:
182185
return dpath.util.get(self._connector_config, self._refresh_token_config_path)
183186

184-
def set_refresh_token(self, new_refresh_token: str):
185-
"""Set the new refresh token value. The mutation of the connector_config object will emit an Airbyte control message.
187+
188+
def _update_config_with_access_and_refresh_tokens(self, new_access_token: str, new_refresh_token: str):
189+
"""Update the connector configuration with new access and refresh token values.
190+
The mutation of the connector_config object will emit Airbyte control messages.
186191
187192
Args:
193+
new_access_token (str): The new access token value.
188194
new_refresh_token (str): The new refresh token value.
189195
"""
196+
# TODO alafanechere this will sequentially emit two control messages.
197+
# We should rework the observer/config mutation logic if we want to have atomic config updates in a single control message.
198+
dpath.util.set(self._connector_config, self._access_token_config_path, new_access_token)
190199
dpath.util.set(self._connector_config, self._refresh_token_config_path, new_refresh_token)
191200

201+
192202
def get_access_token(self) -> str:
193203
"""Retrieve new access and refresh token if the access token has expired.
194204
The new refresh token is persisted with the set_refresh_token function
@@ -200,7 +210,7 @@ def get_access_token(self) -> str:
200210
new_access_token, access_token_expires_in, new_refresh_token = self.refresh_access_token()
201211
self.access_token = new_access_token
202212
self.set_token_expiry_date(t0, access_token_expires_in)
203-
self.set_refresh_token(new_refresh_token)
213+
self._update_config_with_access_and_refresh_tokens(new_access_token, new_refresh_token)
204214
return self.access_token
205215

206216
def refresh_access_token(self) -> Tuple[str, int, str]:

airbyte-cdk/python/unit_tests/sources/streams/http/requests_native_auth/test_requests_native_auth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,11 @@ def test_get_access_token(self, capsys, mocker, connector_config):
218218
authenticator.token_has_expired = mocker.Mock(return_value=True)
219219
access_token = authenticator.get_access_token()
220220
captured = capsys.readouterr()
221-
airbyte_message = json.loads(captured.out)
221+
airbyte_message = json.loads(captured.out.split("\n")[-2])
222222
expected_new_config = connector_config.copy()
223+
expected_new_config["credentials"]["access_token"] = "new_access_token"
223224
expected_new_config["credentials"]["refresh_token"] = "new_refresh_token"
225+
224226
assert airbyte_message["control"]["connectorConfig"]["config"] == expected_new_config
225227
assert authenticator.access_token == access_token == "new_access_token"
226228
assert authenticator.get_refresh_token() == "new_refresh_token"

0 commit comments

Comments
 (0)