Skip to content

Commit e9df028

Browse files
committed
chore: Create helper method to extract the type field
1 parent e203c11 commit e9df028

File tree

7 files changed

+19
-24
lines changed

7 files changed

+19
-24
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ public static ExternalAccountAuthorizedUserCredentials fromStream(
162162
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
163163
Preconditions.checkNotNull(transportFactory);
164164
GenericJson fileContents = parseJsonInputStream(credentialsStream);
165-
String fileType = (String) fileContents.get("type");
166-
if (fileType == null) {
167-
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
168-
}
165+
String fileType = extractFromJson(fileContents, "type");
169166
if (fileType.equals(
170167
GoogleCredentialsInfo.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS.getFileType())) {
171168
try {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,7 @@ public static ExternalAccountCredentials fromStream(
388388
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
389389
Preconditions.checkNotNull(transportFactory);
390390
GenericJson fileContents = parseJsonInputStream(credentialsStream);
391-
String fileType = (String) fileContents.get("type");
392-
if (fileType == null) {
393-
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
394-
}
391+
String fileType = extractFromJson(fileContents, "type");
395392
if (fileType.equals(GoogleCredentialsInfo.EXTERNAL_ACCOUNT_CREDENTIALS.getFileType())) {
396393
try {
397394
return fromJson(fileContents, transportFactory);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ public static GdchCredentials fromStream(
140140
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
141141
Preconditions.checkNotNull(transportFactory);
142142
GenericJson fileContents = parseJsonInputStream(credentialsStream);
143-
String fileType = (String) fileContents.get("type");
144-
if (fileType == null) {
145-
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
146-
}
143+
String fileType = extractFromJson(fileContents, "type");
147144
if (fileType.equals(GoogleCredentialsInfo.GDCH_CREDENTIALS.getFileType())) {
148145
return fromJson(fileContents, transportFactory);
149146
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,19 @@ static GenericJson parseJsonInputStream(InputStream credentialsStream) throws IO
244244
return parser.parseAndClose(credentialsStream, StandardCharsets.UTF_8, GenericJson.class);
245245
}
246246

247+
/**
248+
* Internal helper method to try and extract a field from the json stream and throw an exception
249+
* if it doesn't exist.
250+
*/
251+
static String extractFromJson(Map<String, Object> json, String field) throws IOException {
252+
String fileType = (String) json.get(field);
253+
if (fileType == null) {
254+
throw new IOException(
255+
"Error reading credentials from stream, '" + field + "' field not specified.");
256+
}
257+
return fileType;
258+
}
259+
247260
/**
248261
* This method is obsolete because of a potential security risk. Use the credential specific load
249262
* method instead

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,7 @@ public static ImpersonatedCredentials fromStream(
403403
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
404404
Preconditions.checkNotNull(transportFactory);
405405
GenericJson fileContents = parseJsonInputStream(credentialsStream);
406-
String fileType = (String) fileContents.get("type");
407-
if (fileType == null) {
408-
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
409-
}
406+
String fileType = extractFromJson(fileContents, "type");
410407
if (fileType.equals(GoogleCredentialsInfo.IMPERSONATED_CREDENTIALS.getFileType())) {
411408
return fromJson(fileContents, transportFactory);
412409
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,7 @@ public static ServiceAccountCredentials fromStream(
488488
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
489489
Preconditions.checkNotNull(transportFactory);
490490
GenericJson fileContents = parseJsonInputStream(credentialsStream);
491-
String fileType = (String) fileContents.get("type");
492-
if (fileType == null) {
493-
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
494-
}
491+
String fileType = extractFromJson(fileContents, "type");
495492
if (fileType.equals(GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getFileType())) {
496493
return fromJson(fileContents, transportFactory);
497494
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ public static UserCredentials fromStream(
174174
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
175175
Preconditions.checkNotNull(transportFactory);
176176
GenericJson fileContents = parseJsonInputStream(credentialsStream);
177-
String fileType = (String) fileContents.get("type");
178-
if (fileType == null) {
179-
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
180-
}
177+
String fileType = extractFromJson(fileContents, "type");
181178
if (fileType.equals(GoogleCredentialsInfo.USER_CREDENTIALS.getFileType())) {
182179
return fromJson(fileContents, transportFactory);
183180
}

0 commit comments

Comments
 (0)