Skip to content

Instantly share code, notes, and snippets.

@clonekim
Last active August 11, 2020 04:05
Show Gist options
  • Save clonekim/ba4dbaea6a5a8f3a7c296b90ebcd3a13 to your computer and use it in GitHub Desktop.
Save clonekim/ba4dbaea6a5a8f3a7c296b90ebcd3a13 to your computer and use it in GitHub Desktop.
OkHttp/soap consumer
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException
import java.io.InputStream
import java.security.KeyStore
import java.security.KeyStoreException
import java.security.NoSuchAlgorithmException
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.*
import javax.xml.stream.XMLEventReader
import javax.xml.stream.XMLInputFactory
import javax.xml.stream.XMLStreamException
class SimpleXMLReader {
companion object {
private const val RETURN: String = "return"
@Throws(XMLStreamException::class)
fun parse(input: InputStream): String? {
val xmlFactory: XMLInputFactory = XMLInputFactory.newFactory()
val reader: XMLEventReader = xmlFactory.createXMLEventReader(input)
while (reader.hasNext()) {
val event = reader.nextEvent()
if (event.isStartElement) {
if ( RETURN == event.asStartElement()!!.name.localPart) {
return (reader.nextEvent().asCharacters().data)
}
}
}
return null
}
}
}
fun main() {
val trustManager = object : X509TrustManager {
@Throws(CertificateException::class)
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String ) {
}
@Throws(CertificateException::class)
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String ) {
try {
val trustStore = KeyStore.getInstance("JKS")
trustStore.load( this.javaClass.classLoader.getResourceAsStream("keystore.jks")!!, "checkYourPassword".toCharArray())
val tms = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).run {
init(trustStore)
trustManagers[0] as X509TrustManager
}
tms.checkServerTrusted(chain, authType)
} catch (e: KeyStoreException) {
e.printStackTrace()
throw CertificateException()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
throw CertificateException()
} catch (e: IOException) {
e.printStackTrace()
throw CertificateException()
}
}
override fun getAcceptedIssuers(): Array<X509Certificate>? {
return arrayOf()
}
}
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, arrayOf(trustManager), null)
val sslSocketFactory = sslContext.socketFactory
val client: OkHttpClient = OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustManager)
.hostnameVerifier(hostnameVerifier = HostnameVerifier { _, _ -> true })
.build()
val password = "12345**"
val postBody = """
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:dat="http://www.safenet-inc.com/DataProtectionService">
<soap:Header/>
<soap:Body>
<dat:HmacStringAsHex>
<input>$password</input>
<context>INT_HMAC</context>
</dat:HmacStringAsHex>
</soap:Body>
</soap:Envelope>
""".trimIndent()
val request = Request.Builder()
.url("http://xxxx/SafeNetServiceAxis2/DataProtectionService")
.post(postBody.toRequestBody("application/soap+xml;charset=UTF-8".toMediaType()))
.build()
val response = client.newCall(request).execute().use { response ->
if(response.isSuccessful)
SimpleXMLReader.parse(response.body!!.byteStream())
else
null
}
println(response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment