Skip to content

Commit 522c8b8

Browse files
aldogonzalez8octavia-squidington-iii
andauthored
feat(cdk): allow empty secrets (bing-ads) (#519)
Co-authored-by: octavia-squidington-iii <[email protected]>
1 parent 4d04ed5 commit 522c8b8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

airbyte_cdk/sources/declarative/auth/oauth.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
33
#
44

5+
import logging
56
from dataclasses import InitVar, dataclass, field
67
from datetime import datetime, timedelta
7-
from typing import Any, List, Mapping, MutableMapping, Optional, Union
8+
from typing import Any, List, Mapping, Optional, Union
89

910
from airbyte_cdk.sources.declarative.auth.declarative_authenticator import DeclarativeAuthenticator
1011
from airbyte_cdk.sources.declarative.interpolation.interpolated_boolean import InterpolatedBoolean
@@ -19,6 +20,8 @@
1920
)
2021
from airbyte_cdk.utils.datetime_helpers import AirbyteDateTime, ab_datetime_now, ab_datetime_parse
2122

23+
logger = logging.getLogger("airbyte")
24+
2225

2326
@dataclass
2427
class DeclarativeOauth2Authenticator(AbstractOauth2Authenticator, DeclarativeAuthenticator):
@@ -30,7 +33,7 @@ class DeclarativeOauth2Authenticator(AbstractOauth2Authenticator, DeclarativeAut
3033
Attributes:
3134
token_refresh_endpoint (Union[InterpolatedString, str]): The endpoint to refresh the access token
3235
client_id (Union[InterpolatedString, str]): The client id
33-
client_secret (Union[InterpolatedString, str]): Client secret
36+
client_secret (Union[InterpolatedString, str]): Client secret (can be empty for APIs that support this)
3437
refresh_token (Union[InterpolatedString, str]): The token used to refresh the access token
3538
access_token_name (Union[InterpolatedString, str]): THe field to extract access token from in the response
3639
expires_in_name (Union[InterpolatedString, str]): The field to extract expires_in from in the response
@@ -201,8 +204,11 @@ def get_client_secret(self) -> str:
201204
self._client_secret.eval(self.config) if self._client_secret else self._client_secret
202205
)
203206
if not client_secret:
204-
raise ValueError("OAuthAuthenticator was unable to evaluate client_secret parameter")
205-
return client_secret # type: ignore # value will be returned as a string, or an error will be raised
207+
# We've seen some APIs allowing empty client_secret so we will only log here
208+
logger.warning(
209+
"OAuthAuthenticator was unable to evaluate client_secret parameter hence it'll be empty"
210+
)
211+
return client_secret # type: ignore # value will be returned as a string, which might be empty
206212

207213
def get_refresh_token_name(self) -> str:
208214
return self._refresh_token_name.eval(self.config) # type: ignore # eval returns a string in this context

unit_tests/sources/declarative/auth/test_oauth.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import base64
66
import json
77
import logging
8+
from copy import deepcopy
89
from datetime import timedelta, timezone
910
from unittest.mock import Mock
1011

@@ -128,6 +129,20 @@ def test_refresh_with_encode_config_params(self):
128129
}
129130
assert body == expected
130131

132+
def test_client_secret_empty(self):
133+
config_without_client_secret = deepcopy(config)
134+
del config_without_client_secret["client_secret"]
135+
oauth = DeclarativeOauth2Authenticator(
136+
token_refresh_endpoint="{{ config['refresh_endpoint'] }}",
137+
client_id="{{ config['client_id'] }}",
138+
client_secret="{{ config['client_secret'] }}",
139+
config=config_without_client_secret,
140+
parameters={},
141+
grant_type="client_credentials",
142+
)
143+
body = oauth.build_refresh_request_body()
144+
assert body["client_secret"] == ""
145+
131146
def test_refresh_with_decode_config_params(self):
132147
updated_config_fields = {
133148
"client_id": base64.b64encode(config["client_id"].encode("utf-8")).decode(),

0 commit comments

Comments
 (0)