Skip to content

Commit e203c11

Browse files
committed
chore: Refactor parseJsonInputStream to only accept credentialStream
1 parent 046ec89 commit e203c11

File tree

8 files changed

+24
-19
lines changed

8 files changed

+24
-19
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public static ExternalAccountAuthorizedUserCredentials fromStream(InputStream cr
160160
*/
161161
public static ExternalAccountAuthorizedUserCredentials fromStream(
162162
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
163-
GenericJson fileContents = parseJsonInputStream(credentialsStream, transportFactory);
163+
Preconditions.checkNotNull(transportFactory);
164+
GenericJson fileContents = parseJsonInputStream(credentialsStream);
164165
String fileType = (String) fileContents.get("type");
165166
if (fileType == null) {
166167
throw new IOException("Error reading credentials from stream, 'type' field not specified.");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.google.auth.RequestMetadataCallback;
4040
import com.google.auth.http.HttpTransportFactory;
4141
import com.google.common.base.MoreObjects;
42+
import com.google.common.base.Preconditions;
4243
import com.google.errorprone.annotations.CanIgnoreReturnValue;
4344
import java.io.IOException;
4445
import java.io.InputStream;
@@ -385,7 +386,8 @@ public static ExternalAccountCredentials fromStream(InputStream credentialsStrea
385386
*/
386387
public static ExternalAccountCredentials fromStream(
387388
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
388-
GenericJson fileContents = parseJsonInputStream(credentialsStream, transportFactory);
389+
Preconditions.checkNotNull(transportFactory);
390+
GenericJson fileContents = parseJsonInputStream(credentialsStream);
389391
String fileType = (String) fileContents.get("type");
390392
if (fileType == null) {
391393
throw new IOException("Error reading credentials from stream, 'type' field not specified.");

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class GdchCredentials extends GoogleCredentials {
101101
}
102102

103103
/**
104-
* Returns credentials defined by a GDCHCredential key file in JSON format from the Google
104+
* Returns credentials defined by a GdchCredentials key file in JSON format from the Google
105105
* Developers Console.
106106
*
107107
* <p>Important: If you accept a credential configuration (credential JSON/File/Stream) from an
@@ -120,7 +120,7 @@ public static GdchCredentials fromStream(InputStream credentialsStream) throws I
120120
}
121121

122122
/**
123-
* Returns credentials defined by a GDCHCredential key file in JSON format from the Google
123+
* Returns credentials defined by a GdchCredentials key file in JSON format from the Google
124124
* Developers Console.
125125
*
126126
* <p>Important: If you accept a credential configuration (credential JSON/File/Stream) from an
@@ -138,7 +138,8 @@ public static GdchCredentials fromStream(InputStream credentialsStream) throws I
138138
*/
139139
public static GdchCredentials fromStream(
140140
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
141-
GenericJson fileContents = parseJsonInputStream(credentialsStream, transportFactory);
141+
Preconditions.checkNotNull(transportFactory);
142+
GenericJson fileContents = parseJsonInputStream(credentialsStream);
142143
String fileType = (String) fileContents.get("type");
143144
if (fileType == null) {
144145
throw new IOException("Error reading credentials from stream, 'type' field not specified.");

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
package com.google.auth.oauth2;
3333

3434
import com.google.api.client.json.GenericJson;
35-
import com.google.api.client.json.JsonFactory;
3635
import com.google.api.client.json.JsonObjectParser;
3736
import com.google.api.client.util.Preconditions;
3837
import com.google.api.core.ObsoleteApi;
@@ -235,13 +234,13 @@ public static GoogleCredentials fromStream(InputStream credentialsStream) throws
235234
return fromStream(credentialsStream, OAuth2Utils.HTTP_TRANSPORT_FACTORY);
236235
}
237236

238-
static GenericJson parseJsonInputStream(
239-
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
237+
/**
238+
* Parses the Credential InputStream into JSON for each credential subclass to consume. The
239+
* Credential InputStream must be non-null and valid.
240+
*/
241+
static GenericJson parseJsonInputStream(InputStream credentialsStream) throws IOException {
240242
Preconditions.checkNotNull(credentialsStream);
241-
Preconditions.checkNotNull(transportFactory);
242-
243-
JsonFactory jsonFactory = OAuth2Utils.JSON_FACTORY;
244-
JsonObjectParser parser = new JsonObjectParser(jsonFactory);
243+
JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY);
245244
return parser.parseAndClose(credentialsStream, StandardCharsets.UTF_8, GenericJson.class);
246245
}
247246

@@ -286,7 +285,8 @@ static GenericJson parseJsonInputStream(
286285
"This method is obsolete because of a potential security risk. Use the credential specific load method instead")
287286
public static GoogleCredentials fromStream(
288287
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
289-
GenericJson fileContents = parseJsonInputStream(credentialsStream, transportFactory);
288+
Preconditions.checkNotNull(transportFactory);
289+
GenericJson fileContents = parseJsonInputStream(credentialsStream);
290290
String fileType = (String) fileContents.get("type");
291291
if (fileType == null) {
292292
throw new IOException("Error reading credentials from stream, 'type' field not specified.");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ public static ImpersonatedCredentials fromStream(InputStream credentialsStream)
401401
*/
402402
public static ImpersonatedCredentials fromStream(
403403
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
404-
GenericJson fileContents = parseJsonInputStream(credentialsStream, transportFactory);
404+
Preconditions.checkNotNull(transportFactory);
405+
GenericJson fileContents = parseJsonInputStream(credentialsStream);
405406
String fileType = (String) fileContents.get("type");
406407
if (fileType == null) {
407408
throw new IOException("Error reading credentials from stream, 'type' field not specified.");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,8 @@ public static ServiceAccountCredentials fromStream(InputStream credentialsStream
486486
*/
487487
public static ServiceAccountCredentials fromStream(
488488
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
489-
GenericJson fileContents = parseJsonInputStream(credentialsStream, transportFactory);
489+
Preconditions.checkNotNull(transportFactory);
490+
GenericJson fileContents = parseJsonInputStream(credentialsStream);
490491
String fileType = (String) fileContents.get("type");
491492
if (fileType == null) {
492493
throw new IOException("Error reading credentials from stream, 'type' field not specified.");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public static UserCredentials fromStream(InputStream credentialsStream) throws I
172172
*/
173173
public static UserCredentials fromStream(
174174
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
175-
GenericJson fileContents = parseJsonInputStream(credentialsStream, transportFactory);
175+
Preconditions.checkNotNull(transportFactory);
176+
GenericJson fileContents = parseJsonInputStream(credentialsStream);
176177
String fileType = (String) fileContents.get("type");
177178
if (fileType == null) {
178179
throw new IOException("Error reading credentials from stream, 'type' field not specified.");

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ public void fromStream_noType_throws() throws IOException {
155155
@Test
156156
public void fromStream_nullStream_throws() {
157157
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory();
158-
assertThrows(
159-
NullPointerException.class,
160-
() -> GoogleCredentials.parseJsonInputStream(null, transportFactory));
158+
assertThrows(NullPointerException.class, () -> GoogleCredentials.parseJsonInputStream(null));
161159
}
162160

163161
@Test

0 commit comments

Comments
 (0)