This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Added support for client side certificate validation #8
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
68 changes: 68 additions & 0 deletions
68
asynkio/src/test/java/me/nikhilchaudhari/asynkioapp/NetworkCallTest.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,68 @@ | ||
package me.nikhilchaudhari.asynkioapp; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.security.KeyStore; | ||
import java.util.Collections; | ||
|
||
import javax.net.ssl.KeyManager; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.SSLContext; | ||
|
||
import me.nikhilchaudhari.asynkio.core.NetworkCall; | ||
import me.nikhilchaudhari.asynkio.helper.RawFiles; | ||
import me.nikhilchaudhari.asynkio.response.Response; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class NetworkCallTest { | ||
|
||
@Test | ||
public void executeGetRequestWithCertificateBasedClientAuthentication() throws Exception { | ||
SSLContext sslContext = createSslContextWithKeyMaterial(); | ||
|
||
Response response = NetworkCall.get("https://client.badssl.com/", | ||
Collections.<String, String>emptyMap(), | ||
Collections.<String, String>emptyMap(), | ||
null, | ||
null, | ||
null, | ||
2.0, | ||
true, | ||
false, | ||
Collections.<RawFiles>emptyList(), | ||
sslContext); | ||
|
||
int statusCode = response.getStatusCode(); | ||
|
||
if (statusCode == 400) { | ||
System.out.println("Please validate if certificate is not expired"); | ||
} else { | ||
assertThat(statusCode, is(200)); | ||
} | ||
} | ||
|
||
private SSLContext createSslContextWithKeyMaterial() throws Exception { | ||
String keyStorePath = "src/test/resources/badssl.com-client.jks"; | ||
char[] keyStorePassword = "badssl.com".toCharArray(); | ||
|
||
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
|
||
try(InputStream keyStoreInputStream = Files.newInputStream(Paths.get(keyStorePath));) { | ||
keyStore.load(keyStoreInputStream, keyStorePassword); | ||
} | ||
|
||
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | ||
keyManagerFactory.init(keyStore, keyStorePassword); | ||
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); | ||
|
||
SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); | ||
sslContext.init(keyManagers, null, null); | ||
return sslContext; | ||
} | ||
|
||
} |
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind writing this in kotlin.
you can add kotlin support for test folder as well in gradle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, just changed it to kotlin.
But what do you mean exactly with:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot.
I thought kotlin dependency is not added for the test folder 🤦 .
but it already there, sorry my bad ✋.