|
17 | 17 | from typing import Any, Dict, List, Optional |
18 | 18 |
|
19 | 19 | from google.auth import credentials as google_creds |
| 20 | +from google.adk.auth.auth_credential import AuthCredential, AuthCredentialTypes |
| 21 | +from google.adk.auth.auth_tool import AuthConfig, AuthScheme |
20 | 22 |
|
21 | 23 |
|
22 | 24 | class CredentialType(Enum): |
@@ -106,3 +108,73 @@ def manual_creds(credentials: Any) -> CredentialConfig: |
106 | 108 | type=CredentialType.MANUAL_CREDS, |
107 | 109 | credentials=credentials, |
108 | 110 | ) |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def from_adk_auth_config(auth_config: "AuthConfig") -> CredentialConfig: |
| 114 | + """ |
| 115 | + Creates a CredentialConfig from an ADK AuthConfig object. |
| 116 | +
|
| 117 | + Args: |
| 118 | + auth_config: The ADK AuthConfig object. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + CredentialConfig: The corresponding credential configuration. |
| 122 | + """ |
| 123 | + if auth_config.raw_auth_credential is None: |
| 124 | + raise ValueError("AuthConfig must have a raw_auth_credential.") |
| 125 | + |
| 126 | + return CredentialStrategy.from_adk_credentials( |
| 127 | + auth_config.auth_scheme, auth_config.raw_auth_credential |
| 128 | + ) |
| 129 | + |
| 130 | + @staticmethod |
| 131 | + def from_adk_credentials( |
| 132 | + auth_scheme: "AuthScheme", auth_credential: "AuthCredential" |
| 133 | + ) -> CredentialConfig: |
| 134 | + """ |
| 135 | + Creates a CredentialConfig from ADK AuthScheme and AuthCredential objects. |
| 136 | +
|
| 137 | + Args: |
| 138 | + auth_scheme: The ADK AuthScheme (e.g. OAuth2, HTTP, etc). |
| 139 | + auth_credential: The ADK AuthCredential (containing secrets/tokens). |
| 140 | +
|
| 141 | + Returns: |
| 142 | + CredentialConfig: The corresponding credential configuration. |
| 143 | +
|
| 144 | + Raises: |
| 145 | + ValueError: If the credential type is not supported. |
| 146 | + """ |
| 147 | + # Handle OAuth2 |
| 148 | + if ( |
| 149 | + auth_credential.auth_type == AuthCredentialTypes.OAUTH2 |
| 150 | + and auth_credential.oauth2 |
| 151 | + ): |
| 152 | + # Try to get scopes from scheme if not in credential, though usually |
| 153 | + # we rely on the credential object for the full config in ADK |
| 154 | + # strict mode implies we should look at the credential. |
| 155 | + return CredentialStrategy.user_identity( |
| 156 | + client_id=auth_credential.oauth2.client_id, |
| 157 | + client_secret=auth_credential.oauth2.client_secret, |
| 158 | + scopes=auth_credential.oauth2.scopes, |
| 159 | + ) |
| 160 | + |
| 161 | + # Handle HTTP Bearer |
| 162 | + if ( |
| 163 | + auth_credential.auth_type == AuthCredentialTypes.HTTP |
| 164 | + and auth_credential.http |
| 165 | + ): |
| 166 | + # auth_credential.http is HttpAuth, verifying scheme is Bearer |
| 167 | + # and extracting token from nested credentials object |
| 168 | + scheme_type = (auth_credential.http.scheme or "").lower() |
| 169 | + if ( |
| 170 | + scheme_type == "bearer" |
| 171 | + and auth_credential.http.credentials |
| 172 | + and auth_credential.http.credentials.token |
| 173 | + ): |
| 174 | + return CredentialStrategy.manual_token( |
| 175 | + token=auth_credential.http.credentials.token, scheme="Bearer" |
| 176 | + ) |
| 177 | + |
| 178 | + raise ValueError( |
| 179 | + f"Unsupported ADK credential type: {auth_credential.auth_type}" |
| 180 | + ) |
0 commit comments