@@ -289,23 +289,40 @@ private static String verifyAnswer(String answer, JSONObject factor, String stat
289289 }
290290
291291 private static String handlePushPolling (JSONObject profile , JSONObject jsonObjResponse ) throws IOException , InterruptedException {
292- JSONObject links = jsonObjResponse .getJSONObject (LINKS );
293- JSONObject pollLink = links .getJSONObject ("poll" );
294- String pollUrl = pollLink .getString ("href" );
292+ String pollUrl = getPollURL (jsonObjResponse );
295293
296294 JSONObject pollResult = postAndGetJsonResponse (profile , pollUrl );
297295 String result = pollResult .getString (FACTOR_RESULT );
298296 while ("WAITING" .equals (result )) {
299297 System .err .println ("Waiting for you to approve the Okta push notification on your device..." );
300298 Thread .sleep (500 );
301299 pollResult = postAndGetJsonResponse (profile , pollUrl );
300+ String status = pollResult .getString (STATUS );
301+ if ("SUCCESS" .equals (status )) {
302+ return pollResult .getString (SESSION_TOKEN );
303+ }
302304 result = pollResult .getString (FACTOR_RESULT );
303305 }
304- if ("SUCCESS" .equals (result )) {
305- return pollResult .getString (SESSION_TOKEN );
306- } else {
307- return result ;
306+ return result ;
307+ }
308+
309+ private static String getPollURL (JSONObject jsonObjResponse ) throws RuntimeException {
310+ JSONObject linksObj = jsonObjResponse .getJSONObject (LINKS );
311+ JSONArray linkNames = linksObj .names ();
312+ JSONArray links = linksObj .toJSONArray (linkNames );
313+ JSONObject pollLink = null ;
314+ for (int i = 0 ; i < links .length (); i ++) {
315+ JSONObject link = links .getJSONObject (i );
316+ String linkName = link .getString ("name" );
317+ if (linkName .equals ("poll" )) {
318+ pollLink = link ;
319+ break ;
320+ }
321+ }
322+ if (pollLink == null ) {
323+ throw new IllegalStateException ("Could not determine URL for MFA polling" );
308324 }
325+ return pollLink .getString ("href" );
309326 }
310327
311328 private static JSONObject postAndGetJsonResponse (JSONObject profile , String url ) throws IOException {
0 commit comments