Skip to content

Commit

Permalink
Fix Okta Push polling exception
Browse files Browse the repository at this point in the history
A v2.0 code refactor / cleanup broke a few JSON traversals needed to support Okta MFA - Push. This reverts some of the broken logic.

Fixes oktadev#316
  • Loading branch information
johnhammerlund committed Aug 15, 2019
1 parent d4e90f0 commit 0187368
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/main/java/com/okta/tools/authentication/OktaMFA.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,23 +289,40 @@ 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);
while ("WAITING".equals(result)) {
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 {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/okta/tools/helpers/HttpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0187368

Please sign in to comment.