Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic auth support for images #356

Merged
merged 4 commits into from
Jul 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/src/main/kotlin/com/github/gotify/CoilInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import coil.request.ImageRequest
import com.github.gotify.api.CertUtils
import com.github.gotify.client.model.Application
import java.io.IOException
import okhttp3.Authenticator
import okhttp3.Credentials
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import org.tinylog.kotlin.Logger

object CoilInstance {
Expand Down Expand Up @@ -69,7 +74,9 @@ object CoilInstance {
context: Context,
sslSettings: SSLSettings
): Pair<SSLSettings, ImageLoader> {
val builder = OkHttpClient.Builder()
val builder = OkHttpClient
.Builder()
.authenticator(BasicAuthAuthenticator())
cyb3rko marked this conversation as resolved.
Show resolved Hide resolved
CertUtils.applySslSettings(builder, sslSettings)
val loader = ImageLoader.Builder(context)
.okHttpClient(builder.build())
Expand All @@ -85,3 +92,22 @@ object CoilInstance {
return sslSettings to loader
}
}

private class BasicAuthAuthenticator : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
// If there's no username, skip the authenticator
if (response.request.url.username.isEmpty()) return null

val basicAuthString = "${response.request.url.username}:${response.request.url.password}@"
val url = response.request.url.toString().replace(basicAuthString, "")
return response
.request
.newBuilder()
.header(
"Authorization",
Credentials.basic(response.request.url.username, response.request.url.password)
)
.url(url)
.build()
}
}
Loading