Skip to content

Commit 9d5ebfe

Browse files
authoredDec 11, 2024··
fix: JSON parsing of S2A addresses. (#1589)
* fix: JSON parsing of S2A addresses. * extract extra parsing logic to calling method. * cast once + use defined constant.
1 parent dd1fda5 commit 9d5ebfe

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed
 

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

+12-2
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 =
@@ -188,17 +190,25 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() {
188190

189191
String plaintextS2AAddress = "";
190192
String mtlsS2AAddress = "";
193+
Map<String, Object> s2aAddressConfig = (Map<String, Object>) 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+
}
191200
try {
192201
plaintextS2AAddress =
193-
OAuth2Utils.validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
202+
OAuth2Utils.validateString(
203+
s2aAddressConfig, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
194204
} catch (IOException ignore) {
195205
/*
196206
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.
197207
*/
198208
}
199209
try {
200210
mtlsS2AAddress =
201-
OAuth2Utils.validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
211+
OAuth2Utils.validateString(s2aAddressConfig, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
202212
} catch (IOException ignore) {
203213
/*
204214
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.

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

+1-3
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(SecureSessionAgent.S2A_JSON_KEY, s2aContentMap);
306304
}
307305
String contentText = content.toPrettyString();
308306

0 commit comments

Comments
 (0)
Please sign in to comment.