Skip to content

Commit

Permalink
Added secure http client
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Nov 1, 2023
1 parent aac7da0 commit d1940fb
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions app/src/main/java/org/sirekanyan/outline/api/OutlineApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.sirekanyan.outline.api
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.engine.okhttp.OkHttpConfig
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.request
Expand All @@ -25,6 +26,16 @@ import org.sirekanyan.outline.ext.logDebug
import java.security.SecureRandom
import javax.net.ssl.SSLContext

private fun createOkHttpClient(block: OkHttpConfig.() -> Unit = {}): HttpClient =
HttpClient(OkHttp) {
install(ContentNegotiation) {
json(Json { ignoreUnknownKeys = true })
}
engine {
block()
}
}

private fun setInsecureHttp(builder: OkHttpClient.Builder) {
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, arrayOf(InsecureTrustManager), SecureRandom())
Expand All @@ -34,14 +45,10 @@ private fun setInsecureHttp(builder: OkHttpClient.Builder) {

class OutlineApi {

private val httpClient = HttpClient(OkHttp) {
install(ContentNegotiation) {
json(Json { ignoreUnknownKeys = true })
}
engine {
config {
setInsecureHttp(this) // TODO: remove insecure http
}
private val httpClient = createOkHttpClient()
private val insecureHttpClient = createOkHttpClient {
config {
setInsecureHttp(this)
}
}

Expand All @@ -51,7 +58,8 @@ class OutlineApi {
path: String,
block: HttpRequestBuilder.() -> Unit = {},
): HttpResponse {
return httpClient.request(apiUrl.id + '/' + path) { method = httpMethod; block() }
val client = if (apiUrl.insecure) insecureHttpClient else httpClient
return client.request(apiUrl.id + '/' + path) { method = httpMethod; block() }
}

suspend fun getServer(apiUrl: ApiUrl): Server {
Expand Down

0 comments on commit d1940fb

Please sign in to comment.