Replace image basic auth authenticator with interceptor
This commit is contained in:
@@ -13,12 +13,10 @@ import coil.request.ImageRequest
|
|||||||
import com.github.gotify.api.CertUtils
|
import com.github.gotify.api.CertUtils
|
||||||
import com.github.gotify.client.model.Application
|
import com.github.gotify.client.model.Application
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import okhttp3.Authenticator
|
|
||||||
import okhttp3.Credentials
|
import okhttp3.Credentials
|
||||||
|
import okhttp3.Interceptor
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import okhttp3.Route
|
|
||||||
import org.tinylog.kotlin.Logger
|
import org.tinylog.kotlin.Logger
|
||||||
|
|
||||||
object CoilInstance {
|
object CoilInstance {
|
||||||
@@ -76,7 +74,7 @@ object CoilInstance {
|
|||||||
): Pair<SSLSettings, ImageLoader> {
|
): Pair<SSLSettings, ImageLoader> {
|
||||||
val builder = OkHttpClient
|
val builder = OkHttpClient
|
||||||
.Builder()
|
.Builder()
|
||||||
.authenticator(BasicAuthAuthenticator())
|
.addInterceptor(BasicAuthInterceptor())
|
||||||
CertUtils.applySslSettings(builder, sslSettings)
|
CertUtils.applySslSettings(builder, sslSettings)
|
||||||
val loader = ImageLoader.Builder(context)
|
val loader = ImageLoader.Builder(context)
|
||||||
.okHttpClient(builder.build())
|
.okHttpClient(builder.build())
|
||||||
@@ -93,21 +91,23 @@ object CoilInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BasicAuthAuthenticator : Authenticator {
|
private class BasicAuthInterceptor : Interceptor {
|
||||||
override fun authenticate(route: Route?, response: Response): Request? {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
// If there's no username, skip the authenticator
|
var request = chain.request()
|
||||||
if (response.request.url.username.isEmpty()) return null
|
|
||||||
|
|
||||||
val basicAuthString = "${response.request.url.username}:${response.request.url.password}@"
|
// If there's no username, skip the authentication
|
||||||
val url = response.request.url.toString().replace(basicAuthString, "")
|
if (request.url.username.isNotEmpty()) {
|
||||||
return response
|
val basicAuthString = "${request.url.username}:${request.url.password}@"
|
||||||
.request
|
val url = request.url.toString().replace(basicAuthString, "")
|
||||||
.newBuilder()
|
request = request
|
||||||
.header(
|
.newBuilder()
|
||||||
"Authorization",
|
.header(
|
||||||
Credentials.basic(response.request.url.username, response.request.url.password)
|
"Authorization",
|
||||||
)
|
Credentials.basic(request.url.username, request.url.password)
|
||||||
.url(url)
|
)
|
||||||
.build()
|
.url(url)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
return chain.proceed(request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user