-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
3 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
41 changes: 41 additions & 0 deletions
41
core/api/src/main/java/ru/stersh/youamp/core/api/UnsafeOkHttpClient.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,41 @@ | ||
package ru.stersh.youamp.core.api | ||
|
||
import android.annotation.SuppressLint | ||
import okhttp3.OkHttpClient | ||
import java.security.SecureRandom | ||
import java.security.cert.X509Certificate | ||
import javax.net.ssl.SSLContext | ||
import javax.net.ssl.SSLSocketFactory | ||
import javax.net.ssl.TrustManager | ||
import javax.net.ssl.X509TrustManager | ||
|
||
@SuppressLint("CustomX509TrustManager,TrustAllX509TrustManager") | ||
internal fun getUnsafeOkHttpClient(): OkHttpClient.Builder { | ||
try { | ||
val trustAllCerts = arrayOf<TrustManager>( | ||
object : X509TrustManager { | ||
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) { | ||
} | ||
|
||
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) { | ||
} | ||
|
||
override fun getAcceptedIssuers(): Array<X509Certificate> { | ||
return arrayOf() | ||
} | ||
} | ||
) | ||
|
||
val sslContext = SSLContext.getInstance("SSL") | ||
sslContext.init(null, trustAllCerts, SecureRandom()) | ||
|
||
val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory | ||
|
||
val builder: OkHttpClient.Builder = OkHttpClient.Builder() | ||
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager) | ||
builder.hostnameVerifier { _, _ -> true } | ||
return builder | ||
} catch (e: Exception) { | ||
throw RuntimeException(e) | ||
} | ||
} |