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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for client side certificate validation (#8)
* Added support for client side certificate validation * Added unit test with client side certificate validation * Changed unit test from java to kotlin * Added new line at the end
- Loading branch information
Showing
6 changed files
with
84 additions
and
18 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
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
48 changes: 48 additions & 0 deletions
48
asynkio/src/test/java/me/nikhilchaudhari/asynkioapp/NetworkCallTest.kt
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,48 @@ | ||
package me.nikhilchaudhari.asynkioapp | ||
|
||
import me.nikhilchaudhari.asynkio.core.get | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.junit.Assert.assertThat | ||
import org.junit.Test | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.security.KeyStore | ||
import javax.net.ssl.KeyManagerFactory | ||
import javax.net.ssl.SSLContext | ||
|
||
class NetworkCallTest { | ||
|
||
@Test | ||
fun executeGetRequestWithCertificateBasedClientAuthentication() { | ||
val sslContext = createSslContextWithKeyMaterial() | ||
val response = get( | ||
url = "https://client.badssl.com/", | ||
sslContext = sslContext | ||
) | ||
val statusCode = response.statusCode | ||
if (statusCode == 400) { | ||
println("Please validate if certificate is not expired") | ||
} else { | ||
assertThat(statusCode, equalTo(200)) | ||
} | ||
} | ||
|
||
private fun createSslContextWithKeyMaterial(): SSLContext { | ||
val keyStorePath = "src/test/resources/badssl.com-client.jks" | ||
val keyStorePassword = "badssl.com".toCharArray() | ||
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType()) | ||
|
||
Files.newInputStream(Paths.get(keyStorePath)) | ||
.use { keyStoreInputStream -> keyStore.load(keyStoreInputStream, keyStorePassword) } | ||
|
||
val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()) | ||
keyManagerFactory.init(keyStore, keyStorePassword) | ||
|
||
val keyManagers = keyManagerFactory.keyManagers | ||
|
||
val sslContext = SSLContext.getInstance("TLSv1.2") | ||
sslContext.init(keyManagers, null, null) | ||
|
||
return sslContext | ||
} | ||
} |
Binary file not shown.