Replace Authenticator with Interceptor
This commit is contained in:
@@ -6,8 +6,9 @@ import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import dagger.multibindings.IntoSet
|
||||
import gq.kirmanak.mealient.datasource.impl.AuthInterceptor
|
||||
import gq.kirmanak.mealient.datasource.impl.CacheBuilderImpl
|
||||
import gq.kirmanak.mealient.datasource.impl.MealieAuthenticator
|
||||
import gq.kirmanak.mealient.datasource.impl.NetworkRequestWrapperImpl
|
||||
import gq.kirmanak.mealient.datasource.impl.OkHttpBuilderImpl
|
||||
import gq.kirmanak.mealient.datasource.impl.RetrofitBuilder
|
||||
@@ -19,7 +20,7 @@ import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1Impl
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieServiceV1
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.Authenticator
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Converter
|
||||
@@ -90,5 +91,6 @@ interface DataSourceModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun bindAuthenticator(mealieAuthenticator: MealieAuthenticator): Authenticator
|
||||
@IntoSet
|
||||
fun bindAuthInterceptor(authInterceptor: AuthInterceptor): Interceptor
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package gq.kirmanak.mealient.datasource.impl
|
||||
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import gq.kirmanak.mealient.datasource.AuthenticationProvider
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class AuthInterceptor @Inject constructor(
|
||||
private val logger: Logger,
|
||||
private val authenticationProviderProvider: Provider<AuthenticationProvider>,
|
||||
) : Interceptor {
|
||||
|
||||
private val authenticationProvider: AuthenticationProvider
|
||||
get() = authenticationProviderProvider.get()
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
logger.v { "intercept() was called" }
|
||||
val header = getAuthHeader()
|
||||
val request = chain.request().let {
|
||||
if (header == null) it else it.newBuilder().header(HEADER_NAME, header).build()
|
||||
}
|
||||
logger.d { "Sending header $HEADER_NAME=${request.header(HEADER_NAME)}" }
|
||||
return chain.proceed(request).also {
|
||||
logger.v { "Response code is ${it.code}" }
|
||||
if (it.code == 401 && header != null) logout()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getAuthHeader() = runBlocking { authenticationProvider.getAuthHeader() }
|
||||
|
||||
private fun logout() = runBlocking { authenticationProvider.logout() }
|
||||
|
||||
companion object {
|
||||
@VisibleForTesting
|
||||
const val HEADER_NAME = "Authorization"
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.impl
|
||||
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import gq.kirmanak.mealient.datasource.AuthenticationProvider
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.Authenticator
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import okhttp3.Route
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
// TODO has to be interceptor, otherwise only public recipes are visible
|
||||
class MealieAuthenticator @Inject constructor(
|
||||
private val authenticationProviderProvider: Provider<AuthenticationProvider>,
|
||||
) : Authenticator {
|
||||
|
||||
private val authenticationProvider: AuthenticationProvider
|
||||
get() = authenticationProviderProvider.get()
|
||||
|
||||
override fun authenticate(route: Route?, response: Response): Request? {
|
||||
val supportsBearer = response.challenges().any { it.scheme == BEARER_SCHEME }
|
||||
val request = response.request
|
||||
return when {
|
||||
request.header(HEADER_NAME) != null -> {
|
||||
logout()
|
||||
null
|
||||
}
|
||||
supportsBearer -> getAuthHeader()?.let { request.copyWithHeader(HEADER_NAME, it) }
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private fun getAuthHeader() = runBlocking { authenticationProvider.getAuthHeader() }
|
||||
|
||||
private fun logout() = runBlocking { authenticationProvider.logout() }
|
||||
|
||||
companion object {
|
||||
@VisibleForTesting
|
||||
const val HEADER_NAME = "Authorization"
|
||||
private const val BEARER_SCHEME = "Bearer"
|
||||
}
|
||||
}
|
||||
|
||||
private fun Request.copyWithHeader(name: String, value: String): Request {
|
||||
return newBuilder().header(name, value).build()
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package gq.kirmanak.mealient.datasource.impl
|
||||
|
||||
import gq.kirmanak.mealient.datasource.CacheBuilder
|
||||
import gq.kirmanak.mealient.datasource.OkHttpBuilder
|
||||
import okhttp3.Authenticator
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Inject
|
||||
@@ -13,12 +12,10 @@ class OkHttpBuilderImpl @Inject constructor(
|
||||
private val cacheBuilder: CacheBuilder,
|
||||
// Use @JvmSuppressWildcards because otherwise dagger can't inject it (https://stackoverflow.com/a/43149382)
|
||||
private val interceptors: Set<@JvmSuppressWildcards Interceptor>,
|
||||
private val authenticator: Authenticator,
|
||||
) : OkHttpBuilder {
|
||||
|
||||
override fun buildOkHttp(): OkHttpClient = OkHttpClient.Builder()
|
||||
.apply { interceptors.forEach(::addNetworkInterceptor) }
|
||||
.cache(cacheBuilder.buildCache())
|
||||
.authenticator(authenticator)
|
||||
.build()
|
||||
}
|
||||
Reference in New Issue
Block a user