4545import java .io .InputStream ;
4646import java .util .Arrays ;
4747import java .util .HashSet ;
48+ import java .util .Map ;
4849import java .util .ServiceLoader ;
4950import java .util .Set ;
5051import javax .annotation .concurrent .ThreadSafe ;
5960 */
6061@ ThreadSafe
6162public 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}
0 commit comments