forked from oktadev/okta-aws-cli-assume-role
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Cover OKTA_MFA_CHOICE cases and failures - Separate user interaction effects from code under test (somewhat) - Trivial happy path coverage for more of the changed code
- Loading branch information
Showing
20 changed files
with
802 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/okta/tools/authentication/OktaAuthnClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2019 Okta | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.okta.tools.authentication; | ||
|
||
import com.okta.tools.models.AuthResult; | ||
|
||
import java.io.IOException; | ||
|
||
public interface OktaAuthnClient { | ||
/** | ||
* Perform primary authentication against Okta | ||
* | ||
* @param username The username of the user | ||
* @param password The password of the user | ||
* @param oktaOrg The org to perform auth against | ||
* @return The authentication result | ||
* @throws IOException If an error occurs during the api call or during the processing of the result. | ||
*/ | ||
AuthResult primaryAuthentication(String username, String password, String oktaOrg) throws IOException; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/com/okta/tools/authentication/OktaAuthnClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2019 Okta | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.okta.tools.authentication; | ||
|
||
import com.okta.tools.helpers.HttpHelper; | ||
import com.okta.tools.models.AuthResult; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.json.JSONObject; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class OktaAuthnClientImpl implements OktaAuthnClient { | ||
private static final Logger logger = LogManager.getLogger(OktaAuthnClientImpl.class); | ||
|
||
@Override | ||
public AuthResult primaryAuthentication(String username, String password, String oktaOrg) throws IOException { | ||
// Okta authn API docs: https://developer.okta.com/docs/api/resources/authn#primary-authentication | ||
HttpPost httpPost = new HttpPost("https://" + oktaOrg + "/api/v1/authn"); | ||
|
||
httpPost.addHeader("Accept", "application/json"); | ||
httpPost.addHeader("Content-Type", "application/json"); | ||
httpPost.addHeader("Cache-Control", "no-cache"); | ||
|
||
JSONObject authnRequest = new JSONObject(); | ||
authnRequest.put("username", username); | ||
authnRequest.put("password", password); | ||
|
||
StringEntity entity = new StringEntity(authnRequest.toString(), StandardCharsets.UTF_8); | ||
entity.setContentType("application/json"); | ||
httpPost.setEntity(entity); | ||
|
||
logger.debug("Calling okta authn service at " + httpPost.getURI()); | ||
try (CloseableHttpClient httpClient = HttpHelper.createClient()) { | ||
CloseableHttpResponse authnResponse = httpClient.execute(httpPost); | ||
|
||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(65536); | ||
authnResponse.getEntity().writeTo(byteArrayOutputStream); | ||
|
||
return new AuthResult(authnResponse.getStatusLine(), byteArrayOutputStream.toString()); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/okta/tools/authentication/OktaFactorSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2019 Okta | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.okta.tools.authentication; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public interface OktaFactorSelector { | ||
/** | ||
* Handles selection of a factor from multiple choices | ||
* | ||
* @param primaryAuthResponse The response from Primary Authentication | ||
* @return A {@link JSONObject} representing the selected factor. | ||
* @throws JSONException if a network or protocol error occurs | ||
*/ | ||
JSONObject selectFactor(JSONObject primaryAuthResponse) throws JSONException; | ||
} |
Oops, something went wrong.