File tree Expand file tree Collapse file tree
src/google/adk/tools/openapi_tool/auth
tests/unittests/tools/openapi_tool/auth Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1414
1515from __future__ import annotations
1616
17+ import base64
1718from typing import Any
1819from typing import Dict
1920from 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
Original file line number Diff line number Diff line change 1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import base64
1516from unittest .mock import patch
1617
1718from 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
430433def test_credential_to_param_http_invalid_credentials_no_http ():
You can’t perform that action at this time.
0 commit comments