|
36 | 36 | import com.google.api.client.http.HttpHeaders; |
37 | 37 | import com.google.api.client.json.GenericJson; |
38 | 38 | import com.google.api.client.json.JsonObjectParser; |
| 39 | +import com.google.api.client.util.Data; |
39 | 40 | import com.google.auth.RequestMetadataCallback; |
40 | 41 | import com.google.auth.http.HttpTransportFactory; |
41 | 42 | import com.google.common.base.MoreObjects; |
@@ -418,15 +419,16 @@ static ExternalAccountCredentials fromJson( |
418 | 419 | Map<String, Object> credentialSourceMap = (Map<String, Object>) json.get("credential_source"); |
419 | 420 |
|
420 | 421 | // Optional params. |
421 | | - String serviceAccountImpersonationUrl = (String) json.get("service_account_impersonation_url"); |
422 | | - String tokenInfoUrl = (String) json.get("token_info_url"); |
423 | | - String clientId = (String) json.get("client_id"); |
424 | | - String clientSecret = (String) json.get("client_secret"); |
425 | | - String quotaProjectId = (String) json.get("quota_project_id"); |
426 | | - String userProject = (String) json.get("workforce_pool_user_project"); |
427 | | - String universeDomain = (String) json.get("universe_domain"); |
| 422 | + String serviceAccountImpersonationUrl = |
| 423 | + getOptional(json, "service_account_impersonation_url", String.class); |
| 424 | + String tokenInfoUrl = getOptional(json, "token_info_url", String.class); |
| 425 | + String clientId = getOptional(json, "client_id", String.class); |
| 426 | + String clientSecret = getOptional(json, "client_secret", String.class); |
| 427 | + String quotaProjectId = getOptional(json, "quota_project_id", String.class); |
| 428 | + String userProject = getOptional(json, "workforce_pool_user_project", String.class); |
| 429 | + String universeDomain = getOptional(json, "universe_domain", String.class); |
428 | 430 | Map<String, Object> impersonationOptionsMap = |
429 | | - (Map<String, Object>) json.get("service_account_impersonation"); |
| 431 | + getOptional(json, "service_account_impersonation", Map.class); |
430 | 432 |
|
431 | 433 | if (impersonationOptionsMap == null) { |
432 | 434 | impersonationOptionsMap = new HashMap<String, Object>(); |
@@ -481,6 +483,14 @@ static ExternalAccountCredentials fromJson( |
481 | 483 | .build(); |
482 | 484 | } |
483 | 485 |
|
| 486 | + private static <T> T getOptional(Map<String, Object> json, String fieldName, Class<T> clazz) { |
| 487 | + Object value = json.get(fieldName); |
| 488 | + if (value == null || Data.isNull(value)) { |
| 489 | + return null; |
| 490 | + } |
| 491 | + return clazz.cast(value); |
| 492 | + } |
| 493 | + |
484 | 494 | private static boolean isPluggableAuthCredential(Map<String, Object> credentialSource) { |
485 | 495 | // Pluggable Auth is enabled via a nested executable field in the credential source. |
486 | 496 | return credentialSource.containsKey(EXECUTABLE_SOURCE_KEY); |
|
0 commit comments