Skip to content

Commit f98b1ba

Browse files
committed
extract extra parsing logic to calling method.
1 parent 57ab3c7 commit f98b1ba

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ class OAuth2Utils {
9797

9898
static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
9999

100-
static final String VALUE_NOT_FOUND_MESSAGE = "%sExpected value %s not found.";
101-
static final String VALUE_WRONG_TYPE_MESSAGE = "%sExpected %s value %s of wrong type.";
100+
private static String VALUE_NOT_FOUND_MESSAGE = "%sExpected value %s not found.";
101+
private static String VALUE_WRONG_TYPE_MESSAGE = "%sExpected %s value %s of wrong type.";
102102

103103
static final String BEARER_PREFIX = AuthHttpConstants.BEARER + " ";
104104

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,28 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() {
190190

191191
String plaintextS2AAddress = "";
192192
String mtlsS2AAddress = "";
193+
Object s2aAddressConfig = responseData.get(S2A_JSON_KEY);
194+
if (s2aAddressConfig == null) {
195+
/*
196+
* Return empty addresses in {@link SecureSessionAgentConfig} if endpoint doesn't return anything.
197+
*/
198+
return SecureSessionAgentConfig.createBuilder().build();
199+
}
193200
try {
194201
plaintextS2AAddress =
195-
validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
202+
OAuth2Utils.validateString(
203+
(Map<String, Object>) s2aAddressConfig,
204+
S2A_PLAINTEXT_ADDRESS_JSON_KEY,
205+
PARSE_ERROR_S2A);
196206
} catch (IOException ignore) {
197207
/*
198208
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.
199209
*/
200210
}
201211
try {
202-
mtlsS2AAddress = validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
212+
mtlsS2AAddress =
213+
OAuth2Utils.validateString(
214+
(Map<String, Object>) s2aAddressConfig, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
203215
} catch (IOException ignore) {
204216
/*
205217
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.
@@ -211,23 +223,4 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() {
211223
.setMtlsAddress(mtlsS2AAddress)
212224
.build();
213225
}
214-
215-
private static String validateString(Map<String, Object> map, String key, String errorPrefix)
216-
throws IOException {
217-
Object value = map.get(S2A_JSON_KEY);
218-
if (value == null) {
219-
throw new IOException(
220-
String.format(OAuth2Utils.VALUE_NOT_FOUND_MESSAGE, errorPrefix, S2A_JSON_KEY));
221-
}
222-
if (!(value instanceof Map)) {
223-
throw new IOException(
224-
String.format(OAuth2Utils.VALUE_WRONG_TYPE_MESSAGE, errorPrefix, "Map", S2A_JSON_KEY));
225-
}
226-
Object address = ((Map<String, Object>) value).get(key);
227-
if (!(address instanceof String)) {
228-
throw new IOException(
229-
String.format(OAuth2Utils.VALUE_WRONG_TYPE_MESSAGE, errorPrefix, "string", key));
230-
}
231-
return (String) address;
232-
}
233226
}

0 commit comments

Comments
 (0)