refactor: extract credentail key building logic to auth_config

PiperOrigin-RevId: 768124459
This commit is contained in:
Xiang (Sean) Zhou
2025-06-06 10:19:20 -07:00
committed by Copybara-Service
parent 9abb8414da
commit 309a656f49
4 changed files with 131 additions and 56 deletions
+2 -25
View File
@@ -112,7 +112,7 @@ class AuthHandler:
def parse_and_store_auth_response(self, state: State) -> None:
credential_key = self.get_credential_key()
credential_key = "temp:" + self.auth_config.get_credential_key()
state[credential_key] = self.auth_config.exchanged_auth_credential
if not isinstance(
@@ -130,7 +130,7 @@ class AuthHandler:
raise ValueError("auth_scheme is empty.")
def get_auth_response(self, state: State) -> AuthCredential:
credential_key = self.get_credential_key()
credential_key = "temp:" + self.auth_config.get_credential_key()
return state.get(credential_key, None)
def generate_auth_request(self) -> AuthConfig:
@@ -192,29 +192,6 @@ class AuthHandler:
exchanged_auth_credential=exchanged_credential,
)
def get_credential_key(self) -> str:
"""Generates a unique key for the given auth scheme and credential."""
auth_scheme = self.auth_config.auth_scheme
auth_credential = self.auth_config.raw_auth_credential
if auth_scheme.model_extra:
auth_scheme = auth_scheme.model_copy(deep=True)
auth_scheme.model_extra.clear()
scheme_name = (
f"{auth_scheme.type_.name}_{hash(auth_scheme.model_dump_json())}"
if auth_scheme
else ""
)
if auth_credential.model_extra:
auth_credential = auth_credential.model_copy(deep=True)
auth_credential.model_extra.clear()
credential_name = (
f"{auth_credential.auth_type.value}_{hash(auth_credential.model_dump_json())}"
if auth_credential
else ""
)
return f"temp:adk_{scheme_name}_{credential_name}"
def generate_auth_uri(
self,
) -> AuthCredential:
+30
View File
@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from .auth_credential import AuthCredential
from .auth_credential import BaseModelWithConfig
from .auth_schemes import AuthScheme
@@ -43,6 +45,34 @@ class AuthConfig(BaseModelWithConfig):
this field to guide the user through the OAuth2 flow and fill auth response in
this field"""
def get_credential_key(self):
"""Generates a hash key based on auth_scheme and raw_auth_credential. This
hash key can be used to store / retrieve exchanged_auth_credential in a
credentials store.
"""
auth_scheme = self.auth_scheme
if auth_scheme.model_extra:
auth_scheme = auth_scheme.model_copy(deep=True)
auth_scheme.model_extra.clear()
scheme_name = (
f"{auth_scheme.type_.name}_{hash(auth_scheme.model_dump_json())}"
if auth_scheme
else ""
)
auth_credential = self.raw_auth_credential
if auth_credential.model_extra:
auth_credential = auth_credential.model_copy(deep=True)
auth_credential.model_extra.clear()
credential_name = (
f"{auth_credential.auth_type.value}_{hash(auth_credential.model_dump_json())}"
if auth_credential
else ""
)
return f"adk_{scheme_name}_{credential_name}"
class AuthToolArguments(BaseModelWithConfig):
"""the arguments for the special long running function tool that is used to