diff --git a/src/main/java/com/okta/tools/authentication/OktaMFA.java b/src/main/java/com/okta/tools/authentication/OktaMFA.java index b959b2a..946278f 100644 --- a/src/main/java/com/okta/tools/authentication/OktaMFA.java +++ b/src/main/java/com/okta/tools/authentication/OktaMFA.java @@ -289,9 +289,7 @@ private static String verifyAnswer(String answer, JSONObject factor, String stat } private static String handlePushPolling(JSONObject profile, JSONObject jsonObjResponse) throws IOException, InterruptedException { - JSONObject links = jsonObjResponse.getJSONObject(LINKS); - JSONObject pollLink = links.getJSONObject("poll"); - String pollUrl = pollLink.getString("href"); + String pollUrl = getPollURL(jsonObjResponse); JSONObject pollResult = postAndGetJsonResponse(profile, pollUrl); String result = pollResult.getString(FACTOR_RESULT); @@ -299,13 +297,32 @@ private static String handlePushPolling(JSONObject profile, JSONObject jsonObjRe System.err.println("Waiting for you to approve the Okta push notification on your device..."); Thread.sleep(500); pollResult = postAndGetJsonResponse(profile, pollUrl); + String status = pollResult.getString(STATUS); + if ("SUCCESS".equals(status)) { + return pollResult.getString(SESSION_TOKEN); + } result = pollResult.getString(FACTOR_RESULT); } - if ("SUCCESS".equals(result)) { - return pollResult.getString(SESSION_TOKEN); - } else { - return result; + return result; + } + + private static String getPollURL(JSONObject jsonObjResponse) throws RuntimeException { + JSONObject linksObj = jsonObjResponse.getJSONObject(LINKS); + JSONArray linkNames = linksObj.names(); + JSONArray links = linksObj.toJSONArray(linkNames); + JSONObject pollLink = null; + for (int i = 0; i < links.length(); i++) { + JSONObject link = links.getJSONObject(i); + String linkName = link.getString("name"); + if (linkName.equals("poll")) { + pollLink = link; + break; + } + } + if (pollLink == null) { + throw new IllegalStateException("Could not determine URL for MFA polling"); } + return pollLink.getString("href"); } private static JSONObject postAndGetJsonResponse(JSONObject profile, String url) throws IOException { diff --git a/src/main/java/com/okta/tools/helpers/HttpHelper.java b/src/main/java/com/okta/tools/helpers/HttpHelper.java index 2dd1373..817f463 100644 --- a/src/main/java/com/okta/tools/helpers/HttpHelper.java +++ b/src/main/java/com/okta/tools/helpers/HttpHelper.java @@ -15,7 +15,9 @@ */ package com.okta.tools.helpers; +import com.amazonaws.RequestConfig; import org.apache.http.HttpHost; +import org.apache.http.client.config.CookieSpecs; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory;