Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: JSON parsing of S2A addresses. #1589

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions oauth2_http/java/com/google/auth/oauth2/SecureSessionAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import javax.annotation.concurrent.ThreadSafe;
Expand All @@ -59,6 +60,7 @@
*/
@ThreadSafe
public class SecureSessionAgent {
static final String S2A_JSON_KEY = "s2a";
static final String S2A_PLAINTEXT_ADDRESS_JSON_KEY = "plaintext_address";
static final String S2A_MTLS_ADDRESS_JSON_KEY = "mtls_address";
static final String S2A_CONFIG_ENDPOINT_POSTFIX =
Expand Down Expand Up @@ -188,17 +190,25 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() {

String plaintextS2AAddress = "";
String mtlsS2AAddress = "";
Map<String, Object> s2aAddressConfig = (Map<String, Object>) responseData.get(S2A_JSON_KEY);
if (s2aAddressConfig == null) {
/*
* Return empty addresses in {@link SecureSessionAgentConfig} if endpoint doesn't return anything.
*/
return SecureSessionAgentConfig.createBuilder().build();
}
try {
plaintextS2AAddress =
OAuth2Utils.validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
OAuth2Utils.validateString(
s2aAddressConfig, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
} catch (IOException ignore) {
/*
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.
*/
}
try {
mtlsS2AAddress =
OAuth2Utils.validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
OAuth2Utils.validateString(s2aAddressConfig, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A);
} catch (IOException ignore) {
/*
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,7 @@ public LowLevelHttpResponse execute() throws IOException {
GenericJson content = new GenericJson();
content.setFactory(OAuth2Utils.JSON_FACTORY);
if (requestStatusCode == 200) {
for (Map.Entry<String, String> entrySet : s2aContentMap.entrySet()) {
content.put(entrySet.getKey(), entrySet.getValue());
}
content.put(SecureSessionAgent.S2A_JSON_KEY, s2aContentMap);
}
String contentText = content.toPrettyString();

Expand Down
Loading