Skip to content

Commit 3e8a603

Browse files
committed
Support HTTP Basic auth for OpenAPI tools
1 parent 3e282d2 commit 3e8a603

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/google/adk/tools/openapi_tool/auth/auth_helpers.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
import base64
1718
from typing import Any
1819
from typing import Dict
1920
from typing import List
@@ -391,12 +392,22 @@ def credential_to_param(
391392
and auth_credential.http
392393
and auth_credential.http.credentials
393394
and (
394-
auth_credential.http.credentials.username
395-
or auth_credential.http.credentials.password
395+
auth_credential.http.credentials.username is not None
396+
or auth_credential.http.credentials.password is not None
396397
)
397398
):
398-
# Basic Auth is explicitly NOT supported
399-
raise NotImplementedError("Basic Authentication is not supported.")
399+
username = auth_credential.http.credentials.username or ""
400+
password = auth_credential.http.credentials.password or ""
401+
encoded = base64.b64encode(f"{username}:{password}".encode()).decode()
402+
param = ApiParameter(
403+
original_name="Authorization",
404+
param_location="header",
405+
param_schema=Schema(type="string"),
406+
description=auth_scheme.description or "Basic auth",
407+
py_name=INTERNAL_AUTH_PREFIX + "Authorization",
408+
)
409+
kwargs = {param.py_name: f"Basic {encoded}"}
410+
return param, kwargs
400411
else:
401412
raise ValueError("Invalid HTTP auth credentials")
402413

tests/unittests/tools/openapi_tool/auth/test_auth_helper.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import base64
1516
from unittest.mock import patch
1617

1718
from fastapi.openapi.models import APIKey
@@ -411,7 +412,7 @@ def test_credential_to_param_http_bearer():
411412
assert kwargs == {INTERNAL_AUTH_PREFIX + "Authorization": "Bearer test_token"}
412413

413414

414-
def test_credential_to_param_http_basic_not_supported():
415+
def test_credential_to_param_http_basic():
415416
auth_scheme = HTTPBase(scheme="basic")
416417
auth_credential = AuthCredential(
417418
auth_type=AuthCredentialTypes.HTTP,
@@ -421,10 +422,12 @@ def test_credential_to_param_http_basic_not_supported():
421422
),
422423
)
423424

424-
with pytest.raises(
425-
NotImplementedError, match="Basic Authentication is not supported."
426-
):
427-
credential_to_param(auth_scheme, auth_credential)
425+
param, kwargs = credential_to_param(auth_scheme, auth_credential)
426+
427+
expected = base64.b64encode(b"user:password").decode()
428+
assert param.original_name == "Authorization"
429+
assert param.param_location == "header"
430+
assert kwargs == {INTERNAL_AUTH_PREFIX + "Authorization": f"Basic {expected}"}
428431

429432

430433
def test_credential_to_param_http_invalid_credentials_no_http():

0 commit comments

Comments
 (0)