Skip to content

Commit 57ab3c7

Browse files
committed
fix: JSON parsing of S2A addresses.
1 parent cc9826e commit 57ab3c7

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
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-
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.";
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.";
102102

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

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.io.InputStream;
4646
import java.util.Arrays;
4747
import java.util.HashSet;
48+
import java.util.Map;
4849
import java.util.ServiceLoader;
4950
import java.util.Set;
5051
import javax.annotation.concurrent.ThreadSafe;
@@ -59,6 +60,7 @@
5960
*/
6061
@ThreadSafe
6162
public class SecureSessionAgent {
63+
static final String S2A_JSON_KEY = "s2a";
6264
static final String S2A_PLAINTEXT_ADDRESS_JSON_KEY = "plaintext_address";
6365
static final String S2A_MTLS_ADDRESS_JSON_KEY = "mtls_address";
6466
static final String S2A_CONFIG_ENDPOINT_POSTFIX =
@@ -190,15 +192,14 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() {
190192
String mtlsS2AAddress = "";
191193
try {
192194
plaintextS2AAddress =
193-
OAuth2Utils.validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
195+
validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
194196
} catch (IOException ignore) {
195197
/*
196198
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.
197199
*/
198200
}
199201
try {
200-
mtlsS2AAddress =
201-
OAuth2Utils.validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
202+
mtlsS2AAddress = validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
202203
} catch (IOException ignore) {
203204
/*
204205
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.
@@ -210,4 +211,23 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() {
210211
.setMtlsAddress(mtlsS2AAddress)
211212
.build();
212213
}
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+
}
213233
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,7 @@ public LowLevelHttpResponse execute() throws IOException {
300300
GenericJson content = new GenericJson();
301301
content.setFactory(OAuth2Utils.JSON_FACTORY);
302302
if (requestStatusCode == 200) {
303-
for (Map.Entry<String, String> entrySet : s2aContentMap.entrySet()) {
304-
content.put(entrySet.getKey(), entrySet.getValue());
305-
}
303+
content.put("s2a", s2aContentMap);
306304
}
307305
String contentText = content.toPrettyString();
308306

0 commit comments

Comments
 (0)