Skip to content

Instantly share code, notes, and snippets.

@maiconhellmann
Last active August 26, 2024 13:06
Show Gist options
  • Select an option

  • Save maiconhellmann/c61a533eca6d41880fd2b3f8459c07f7 to your computer and use it in GitHub Desktop.

Select an option

Save maiconhellmann/c61a533eca6d41880fd2b3f8459c07f7 to your computer and use it in GitHub Desktop.

Revisions

  1. maiconhellmann revised this gist May 1, 2024. 2 changed files with 47 additions and 44 deletions.
    44 changes: 0 additions & 44 deletions UnsafeHttpClient.kt
    Original file line number Diff line number Diff line change
    @@ -1,44 +0,0 @@
    import okhttp3.OkHttpClient
    import java.security.cert.CertificateException
    import javax.net.ssl.SSLContext
    import javax.net.ssl.TrustManager
    import javax.net.ssl.X509TrustManager
    import javax.net.ssl.HostnameVerifier

    class UnsafeOkHttpClient {
    companion object {
    fun getUnsafeOkHttpClient(): OkHttpClient.Builder {
    try {
    // Create a trust manager that does not validate certificate chains
    val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
    @Throws(CertificateException::class)
    override fun checkClientTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
    }

    @Throws(CertificateException::class)
    override fun checkServerTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
    }

    override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> {
    return arrayOf()
    }
    })

    // Install the all-trusting trust manager
    val sslContext = SSLContext.getInstance("SSL")
    sslContext.init(null, trustAllCerts, java.security.SecureRandom())
    // Create an ssl socket factory with our all-trusting manager
    val sslSocketFactory = sslContext.socketFactory

    val builder = OkHttpClient.Builder()
    builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
    // builder.hostnameVerifier { _, _ -> true }
    builder.hostnameVerifier ( hostnameVerifier = HostnameVerifier{ _, _ -> true })

    return builder
    } catch (e: Exception) {
    throw RuntimeException(e)
    }
    }
    }
    }
    47 changes: 47 additions & 0 deletions UnsafeOkHttpClient.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    import android.annotation.SuppressLint
    import okhttp3.OkHttpClient
    import java.security.cert.CertificateException
    import javax.net.ssl.SSLContext
    import javax.net.ssl.TrustManager
    import javax.net.ssl.X509TrustManager

    object UnsafeOkHttpClient {
    fun Builder(): OkHttpClient.Builder {
    try {
    // Create a trust manager that does not validate certificate chains
    val trustAllCerts = arrayOf<TrustManager>(@SuppressLint("CustomX509TrustManager")
    object : X509TrustManager {
    @SuppressLint("TrustAllX509TrustManager")
    @Throws(CertificateException::class)
    override fun checkClientTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
    // N / A
    }

    @SuppressLint("TrustAllX509TrustManager")
    @Throws(CertificateException::class)
    override fun checkServerTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
    // N / A
    }

    override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> {
    return arrayOf()
    }
    })

    // Install the all-trusting trust manager
    val sslContext = SSLContext.getInstance("SSL")
    sslContext.init(null, trustAllCerts, java.security.SecureRandom())
    // Create an ssl socket factory with our all-trusting manager
    val sslSocketFactory = sslContext.socketFactory

    val builder = OkHttpClient.Builder()
    builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
    // builder.hostnameVerifier { _, _ -> true }
    builder.hostnameVerifier(hostnameVerifier = { _, _ -> true })

    return builder
    } catch (e: Exception) {
    throw RuntimeException(e)
    }
    }
    }
  2. maiconhellmann revised this gist Aug 11, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions UnsafeHttpClient.kt
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@ import java.security.cert.CertificateException
    import javax.net.ssl.SSLContext
    import javax.net.ssl.TrustManager
    import javax.net.ssl.X509TrustManager
    import javax.net.ssl.HostnameVerifier

    class UnsafeOkHttpClient {
    companion object {
  3. maiconhellmann revised this gist Aug 25, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion UnsafeHttpClient.kt
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,8 @@ class UnsafeOkHttpClient {

    val builder = OkHttpClient.Builder()
    builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
    builder.hostnameVerifier { _, _ -> true }
    // builder.hostnameVerifier { _, _ -> true }
    builder.hostnameVerifier ( hostnameVerifier = HostnameVerifier{ _, _ -> true })

    return builder
    } catch (e: Exception) {
  4. maiconhellmann created this gist Nov 21, 2017.
    42 changes: 42 additions & 0 deletions UnsafeHttpClient.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    import okhttp3.OkHttpClient
    import java.security.cert.CertificateException
    import javax.net.ssl.SSLContext
    import javax.net.ssl.TrustManager
    import javax.net.ssl.X509TrustManager

    class UnsafeOkHttpClient {
    companion object {
    fun getUnsafeOkHttpClient(): OkHttpClient.Builder {
    try {
    // Create a trust manager that does not validate certificate chains
    val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
    @Throws(CertificateException::class)
    override fun checkClientTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
    }

    @Throws(CertificateException::class)
    override fun checkServerTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
    }

    override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> {
    return arrayOf()
    }
    })

    // Install the all-trusting trust manager
    val sslContext = SSLContext.getInstance("SSL")
    sslContext.init(null, trustAllCerts, java.security.SecureRandom())
    // Create an ssl socket factory with our all-trusting manager
    val sslSocketFactory = sslContext.socketFactory

    val builder = OkHttpClient.Builder()
    builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
    builder.hostnameVerifier { _, _ -> true }

    return builder
    } catch (e: Exception) {
    throw RuntimeException(e)
    }
    }
    }
    }