Skip to content

Commit d261753

Browse files
committed
chore: Add enums for file type
1 parent bc41872 commit d261753

11 files changed

+851
-835
lines changed

oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ static ImpersonatedCredentials fromJson(
399399
}
400400

401401
GoogleCredentials sourceCredentials;
402-
if (GoogleCredentials.USER_FILE_TYPE.equals(sourceCredentialsType)) {
402+
if (GoogleCredentialsType.USER_CREDENTIALS.getFileType().equals(sourceCredentialsType)) {
403403
sourceCredentials = UserCredentials.fromJson(sourceCredentialsJson, transportFactory);
404-
} else if (GoogleCredentials.SERVICE_ACCOUNT_FILE_TYPE.equals(sourceCredentialsType)) {
404+
} else if (GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(sourceCredentialsType)) {
405405
sourceCredentials =
406406
ServiceAccountCredentials.fromJson(sourceCredentialsJson, transportFactory);
407407
} else {

oauth2_http/java/com/google/auth/oauth2/ServiceAccountJwtAccessCredentials.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
package com.google.auth.oauth2;
3333

34-
import static com.google.auth.oauth2.GoogleCredentials.SERVICE_ACCOUNT_FILE_TYPE;
3534
import static com.google.auth.oauth2.GoogleCredentials.addQuotaProjectIdToRequestMetadata;
3635

3736
import com.google.api.client.json.GenericJson;
@@ -297,14 +296,14 @@ public static ServiceAccountJwtAccessCredentials fromStream(
297296
if (fileType == null) {
298297
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
299298
}
300-
if (SERVICE_ACCOUNT_FILE_TYPE.equals(fileType)) {
299+
if (GoogleCredentials.GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
301300
return fromJson(fileContents, defaultAudience);
302301
}
303302
throw new IOException(
304303
String.format(
305304
"Error reading credentials from stream, 'type' value '%s' not recognized."
306305
+ " Expecting '%s'.",
307-
fileType, SERVICE_ACCOUNT_FILE_TYPE));
306+
fileType, GoogleCredentials.GoogleCredentialsType.SERVICE_ACCOUNT_CREDENTIALS.getFileType()));
308307
}
309308

310309
private LoadingCache<JwtClaims, JwtCredentials> createCache() {

oauth2_http/java/com/google/auth/oauth2/UserCredentials.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ public static UserCredentials fromStream(
177177
if (fileType == null) {
178178
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
179179
}
180-
if (USER_FILE_TYPE.equals(fileType)) {
180+
if (GoogleCredentialsType.USER_CREDENTIALS.getFileType().equals(fileType)) {
181181
return fromJson(fileContents, transportFactory);
182182
}
183183
throw new IOException(
184184
String.format(
185185
"Error reading credentials from stream, 'type' value '%s' not recognized."
186186
+ " Expecting '%s'.",
187-
fileType, USER_FILE_TYPE));
187+
fileType, GoogleCredentialsType.USER_CREDENTIALS.getFileType()));
188188
}
189189

190190
/** Refreshes the OAuth2 access token by getting a new access token from the refresh token */
@@ -312,7 +312,7 @@ private GenericData doRefreshAccessToken() throws IOException {
312312
*/
313313
private InputStream getUserCredentialsStream() throws IOException {
314314
GenericJson json = new GenericJson();
315-
json.put("type", GoogleCredentials.USER_FILE_TYPE);
315+
json.put("type", GoogleCredentialsType.USER_CREDENTIALS.getFileType());
316316
if (refreshToken != null) {
317317
json.put("refresh_token", refreshToken);
318318
}

oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ static GenericJson writeGdchServiceAccountJson(
915915
if (tokenServerUri != null) {
916916
json.put("token_uri", tokenServerUri.toString());
917917
}
918-
json.put("type", GoogleCredentials.GDCH_SERVICE_ACCOUNT_FILE_TYPE);
918+
json.put("type", GoogleCredentials.GoogleCredentialsType.GDCH_CREDENTIALS.getFileType());
919919
return json;
920920
}
921921

0 commit comments

Comments
 (0)