2424from google .api_core import gapic_v1 # type: ignore
2525from google .api_core import retry as retries # type: ignore
2626from google .auth import credentials as ga_credentials # type: ignore
27+ from google .oauth2 import service_account # type: ignore
2728
2829from google .cloud .speech_v1p1beta1 .types import cloud_speech_adaptation
2930from google .cloud .speech_v1p1beta1 .types import resource
4546 except pkg_resources .DistributionNotFound : # pragma: NO COVER
4647 _GOOGLE_AUTH_VERSION = None
4748
48- _API_CORE_VERSION = google .api_core .__version__
49-
5049
5150class AdaptationTransport (abc .ABC ):
5251 """Abstract transport class for Adaptation."""
@@ -64,6 +63,7 @@ def __init__(
6463 scopes : Optional [Sequence [str ]] = None ,
6564 quota_project_id : Optional [str ] = None ,
6665 client_info : gapic_v1 .client_info .ClientInfo = DEFAULT_CLIENT_INFO ,
66+ always_use_jwt_access : Optional [bool ] = False ,
6767 ** kwargs ,
6868 ) -> None :
6969 """Instantiate the transport.
@@ -87,6 +87,8 @@ def __init__(
8787 API requests. If ``None``, then default info will be used.
8888 Generally, you only need to set this if you're developing
8989 your own client library.
90+ always_use_jwt_access (Optional[bool]): Whether self signed JWT should
91+ be used for service account credentials.
9092 """
9193 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
9294 if ":" not in host :
@@ -96,7 +98,7 @@ def __init__(
9698 scopes_kwargs = self ._get_scopes_kwargs (self ._host , scopes )
9799
98100 # Save the scopes.
99- self ._scopes = scopes or self . AUTH_SCOPES
101+ self ._scopes = scopes
100102
101103 # If no credentials are provided, then determine the appropriate
102104 # defaults.
@@ -115,13 +117,20 @@ def __init__(
115117 ** scopes_kwargs , quota_project_id = quota_project_id
116118 )
117119
120+ # If the credentials is service account credentials, then always try to use self signed JWT.
121+ if (
122+ always_use_jwt_access
123+ and isinstance (credentials , service_account .Credentials )
124+ and hasattr (service_account .Credentials , "with_always_use_jwt_access" )
125+ ):
126+ credentials = credentials .with_always_use_jwt_access (True )
127+
118128 # Save the credentials.
119129 self ._credentials = credentials
120130
121- # TODO(busunkim): These two class methods are in the base transport
131+ # TODO(busunkim): This method is in the base transport
122132 # to avoid duplicating code across the transport classes. These functions
123- # should be deleted once the minimum required versions of google-api-core
124- # and google-auth are increased.
133+ # should be deleted once the minimum required versions of google-auth is increased.
125134
126135 # TODO: Remove this function once google-auth >= 1.25.0 is required
127136 @classmethod
@@ -142,27 +151,6 @@ def _get_scopes_kwargs(
142151
143152 return scopes_kwargs
144153
145- # TODO: Remove this function once google-api-core >= 1.26.0 is required
146- @classmethod
147- def _get_self_signed_jwt_kwargs (
148- cls , host : str , scopes : Optional [Sequence [str ]]
149- ) -> Dict [str , Union [Optional [Sequence [str ]], str ]]:
150- """Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version"""
151-
152- self_signed_jwt_kwargs : Dict [str , Union [Optional [Sequence [str ]], str ]] = {}
153-
154- if _API_CORE_VERSION and (
155- packaging .version .parse (_API_CORE_VERSION )
156- >= packaging .version .parse ("1.26.0" )
157- ):
158- self_signed_jwt_kwargs ["default_scopes" ] = cls .AUTH_SCOPES
159- self_signed_jwt_kwargs ["scopes" ] = scopes
160- self_signed_jwt_kwargs ["default_host" ] = cls .DEFAULT_HOST
161- else :
162- self_signed_jwt_kwargs ["scopes" ] = scopes or cls .AUTH_SCOPES
163-
164- return self_signed_jwt_kwargs
165-
166154 def _prep_wrapped_messages (self , client_info ):
167155 # Precompute the wrapped methods.
168156 self ._wrapped_methods = {
0 commit comments