@@ -54,6 +54,17 @@ class ApiClient:
5454 self.default_headers["Accept-Encoding"] = "gzip"
5555 # Set default User-Agent.
5656 self.user_agent = user_agent()
57+
58+ # Initialize delegated token config if delegated auth is configured
59+ self._delegated_token_config = None
60+ if (self.configuration.delegated_auth_provider is not None and
61+ self.configuration.delegated_auth_org_uuid is not None):
62+ from {{ package }}.delegated_auth import DelegatedTokenConfig
63+ self._delegated_token_config = DelegatedTokenConfig(
64+ org_uuid=self.configuration.delegated_auth_org_uuid,
65+ provider="aws",
66+ provider_auth=self.configuration.delegated_auth_provider,
67+ )
5768
5869 def __enter__(self) -> Self:
5970 return self
@@ -454,6 +465,32 @@ class ApiClient:
454465 return "application/json"
455466 return content_types[0]
456467
468+ def use_delegated_token_auth(self, headers: Dict[str, Any]) -> None:
469+ """Use delegated token authentication if configured.
470+
471+ :param headers: Header parameters dict to be updated.
472+ :raises: ApiValueError if delegated token authentication fails
473+ """
474+ # Skip if no delegated token config
475+ if self._delegated_token_config is None:
476+ return
477+
478+ # Check if we need to get or refresh the token
479+ if (self.configuration._delegated_token_credentials is None or
480+ self.configuration._delegated_token_credentials.is_expired()):
481+
482+ # Get new token from provider, passing the API configuration
483+ try:
484+ self.configuration._delegated_token_credentials = self.configuration.delegated_auth_provider.authenticate(
485+ self._delegated_token_config, self.configuration
486+ )
487+ except Exception as e:
488+ raise ApiValueError(f"Failed to get delegated token: {str(e)}")
489+
490+ # Set the Authorization header with the delegated token
491+ token = self.configuration._delegated_token_credentials.delegated_token
492+ headers["Authorization"] = f"Bearer {token}"
493+
457494
458495class ThreadedApiClient(ApiClient):
459496
@@ -824,18 +861,34 @@ class Endpoint:
824861 if not self.settings["auth"]:
825862 return
826863
827- for auth in self.settings["auth"]:
828- auth_setting = self.api_client.configuration.auth_settings().get(auth)
829- if auth_setting:
830- if auth_setting["in"] == "header":
831- if auth_setting["type"] != "http-signature":
832- if auth_setting["value"] is None:
833- raise ApiValueError("Invalid authentication token for {}".format(auth_setting["key"]))
834- headers[auth_setting["key"]] = auth_setting["value"]
835- elif auth_setting["in"] == "query":
836- queries.append((auth_setting["key"], auth_setting["value"]))
837- else:
838- raise ApiValueError("Authentication token must be in `query` or `header`")
864+ # check if endpoint uses appKeyAuth and if delegated token config is available
865+ has_app_key_auth = "appKeyAuth" in self.settings["auth"]
866+
867+ # Check if delegated auth is configured (using our actual attributes)
868+ has_delegated_auth = (
869+ hasattr(self.api_client.configuration, 'delegated_auth_provider') and
870+ self.api_client.configuration.delegated_auth_provider is not None and
871+ hasattr(self.api_client.configuration, 'delegated_auth_org_uuid') and
872+ self.api_client.configuration.delegated_auth_org_uuid is not None
873+ )
874+
875+ if has_app_key_auth and has_delegated_auth:
876+ # Use delegated token authentication
877+ self.api_client.use_delegated_token_auth(headers)
878+ else:
879+ # Use regular authentication
880+ for auth in self.settings["auth"]:
881+ auth_setting = self.api_client.configuration.auth_settings().get(auth)
882+ if auth_setting:
883+ if auth_setting["in"] == "header":
884+ if auth_setting["type"] != "http-signature":
885+ if auth_setting["value"] is None:
886+ raise ApiValueError("Invalid authentication token for {}".format(auth_setting["key"]))
887+ headers[auth_setting["key"]] = auth_setting["value"]
888+ elif auth_setting["in"] == "query":
889+ queries.append((auth_setting["key"], auth_setting["value"]))
890+ else:
891+ raise ApiValueError("Authentication token must be in `query` or `header`")
839892
840893
841894def user_agent() -> str:
0 commit comments