@@ -61,9 +61,35 @@ public class GoogleCredentials extends OAuth2Credentials implements QuotaProject
6161 private static final long serialVersionUID = -1522852442442473691L ;
6262
6363 static final String QUOTA_PROJECT_ID_HEADER_KEY = "x-goog-user-project" ;
64- static final String USER_FILE_TYPE = "authorized_user" ;
65- static final String SERVICE_ACCOUNT_FILE_TYPE = "service_account" ;
66- static final String GDCH_SERVICE_ACCOUNT_FILE_TYPE = "gdch_service_account" ;
64+
65+ enum GoogleCredentialsType {
66+ USER_CREDENTIALS ("User Credentials" , "authorized_user" ),
67+ SERVICE_ACCOUNT_CREDENTIALS ("Service Account Credentials" , "service_account" ),
68+ GDCH_CREDENTIALS ("GDCH Credentials" , "gdch_service_account" ),
69+ EXTERNAL_ACCOUNT_CREDENTIALS ("External Account Credentials" , "external_account" ),
70+ EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS ("External Account Authorized User Credentials" , "external_account_authorized_user" ),
71+ IMPERSONATED_CREDENTIALS ("Impersonated Credentials" ,"impersonated_service_account" ),
72+ APP_ENGINE_CREDENTIALS ("App Engine Credentials" , null ),
73+ CLOUD_SHELL_CREDENTIALS ("Cloud Shell Credentials" , null ),
74+ COMPUTE_ENGINE_CREDENTIALS ("Compute Engine Credentials" , null );
75+
76+ private final String credentialName ;
77+ private final String fileType ;
78+
79+ GoogleCredentialsType (String credentialName , String fileType ) {
80+ this .credentialName = credentialName ;
81+ this .fileType = fileType ;
82+ }
83+
84+ public String getCredentialName () {
85+ return credentialName ;
86+ }
87+
88+ public String getFileType () {
89+ return fileType ;
90+ }
91+ }
92+
6793
6894 /* The following package-private fields provide additional info for errors message */
6995 // Source of the credential (e.g. env var value or well know file location)
@@ -216,36 +242,35 @@ public static GoogleCredentials fromStream(
216242 throw new IOException ("Error reading credentials from stream, 'type' field not specified." );
217243 }
218244
219- if (USER_FILE_TYPE .equals (fileType )) {
245+ if (GoogleCredentialsType . USER_CREDENTIALS . getFileType () .equals (fileType )) {
220246 return UserCredentials .fromJson (fileContents , transportFactory );
221247 }
222- if (SERVICE_ACCOUNT_FILE_TYPE .equals (fileType )) {
248+ if (GoogleCredentialsType . SERVICE_ACCOUNT_CREDENTIALS . getFileType () .equals (fileType )) {
223249 return ServiceAccountCredentials .fromJson (fileContents , transportFactory );
224250 }
225- if (GDCH_SERVICE_ACCOUNT_FILE_TYPE .equals (fileType )) {
251+ if (GoogleCredentialsType . GDCH_CREDENTIALS . getFileType () .equals (fileType )) {
226252 return GdchCredentials .fromJson (fileContents );
227253 }
228- if (ExternalAccountCredentials . EXTERNAL_ACCOUNT_FILE_TYPE .equals (fileType )) {
254+ if (GoogleCredentialsType . EXTERNAL_ACCOUNT_CREDENTIALS . getFileType () .equals (fileType )) {
229255 return ExternalAccountCredentials .fromJson (fileContents , transportFactory );
230256 }
231- if (ExternalAccountAuthorizedUserCredentials .EXTERNAL_ACCOUNT_AUTHORIZED_USER_FILE_TYPE .equals (
232- fileType )) {
257+ if (GoogleCredentialsType .EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS .getFileType ().equals (fileType )) {
233258 return ExternalAccountAuthorizedUserCredentials .fromJson (fileContents , transportFactory );
234259 }
235- if (ImpersonatedCredentials . IMPERSONATED_CREDENTIALS_FILE_TYPE .equals (fileType )) {
260+ if (GoogleCredentialsType . IMPERSONATED_CREDENTIALS . getFileType () .equals (fileType )) {
236261 return ImpersonatedCredentials .fromJson (fileContents , transportFactory );
237262 }
238263 throw new IOException (
239264 String .format (
240265 "Error reading credentials from stream, 'type' value '%s' not recognized."
241266 + " Valid values are '%s', '%s', '%s', '%s', '%s', '%s'." ,
242267 fileType ,
243- USER_FILE_TYPE ,
244- SERVICE_ACCOUNT_FILE_TYPE ,
245- GDCH_SERVICE_ACCOUNT_FILE_TYPE ,
246- ExternalAccountCredentials . EXTERNAL_ACCOUNT_FILE_TYPE ,
247- ExternalAccountAuthorizedUserCredentials . EXTERNAL_ACCOUNT_AUTHORIZED_USER_FILE_TYPE ,
248- ImpersonatedCredentials . IMPERSONATED_CREDENTIALS_FILE_TYPE ));
268+ GoogleCredentialsType . USER_CREDENTIALS . getFileType () ,
269+ GoogleCredentialsType . SERVICE_ACCOUNT_CREDENTIALS . getFileType () ,
270+ GoogleCredentialsType . GDCH_CREDENTIALS . getFileType () ,
271+ GoogleCredentialsType . EXTERNAL_ACCOUNT_CREDENTIALS . getFileType () ,
272+ GoogleCredentialsType . EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS . getFileType () ,
273+ GoogleCredentialsType . IMPERSONATED_CREDENTIALS . getFileType () ));
249274 }
250275
251276 /**
@@ -517,10 +542,13 @@ GoogleCredentials withSource(String source) {
517542 }
518543
519544 /**
520- * Provides additional information regarding credential initialization source - Initialized via
521- * the GOOGLE_APPLICATION_CREDENTIALS env var or well known file type - The type of credential
522- * created principal - Identity used for the credential These fields are populated on a
523- * best-effort basis and may be null or missing
545+ * Provides additional information regarding credential initialization source
546+ * <ul>
547+ * <li> credential source - Initialized via the GOOGLE_APPLICATION_CREDENTIALS env var or well known file type
548+ * <li> credential type - The type of credential created
549+ * <li> principal - Identity used for the credential
550+ * </ul>
551+ * These fields are populated on a best-effort basis and may be populated missing
524552 *
525553 * @return Map of information regarding how the Credential was initialized
526554 */
@@ -541,7 +569,7 @@ public Map<String, String> getCredentialInfo() {
541569 public static class Builder extends OAuth2Credentials .Builder {
542570 @ Nullable protected String quotaProjectId ;
543571 @ Nullable protected String universeDomain ;
544- String source ;
572+ @ Nullable String source ;
545573
546574 protected Builder () {}
547575
0 commit comments