Move token invalidation to auth interceptor
This commit is contained in:
@@ -3,4 +3,6 @@ package gq.kirmanak.mealient.datasource
|
||||
interface AuthenticationProvider {
|
||||
|
||||
suspend fun getAuthHeader(): String?
|
||||
|
||||
suspend fun invalidateAuthHeader()
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package gq.kirmanak.mealient.datasource.impl
|
||||
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import gq.kirmanak.mealient.datasource.AuthenticationProvider
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import retrofit2.HttpException
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
import javax.inject.Singleton
|
||||
@@ -14,19 +16,43 @@ class AuthInterceptor @Inject constructor(
|
||||
) : Interceptor {
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val token = runBlocking { authenticationProvider.get().getAuthHeader() }
|
||||
val request = if (token == null) {
|
||||
chain.request()
|
||||
val token = getAuthHeader()
|
||||
return if (token == null) {
|
||||
proceedWithAuthHeader(chain)
|
||||
} else {
|
||||
chain.request()
|
||||
.newBuilder()
|
||||
.header(HEADER_NAME, token)
|
||||
.build()
|
||||
try {
|
||||
proceedWithAuthHeader(chain, token)
|
||||
} catch (e: HttpException) {
|
||||
if (e.code() in setOf(401, 403)) {
|
||||
invalidateAuthHeader()
|
||||
proceedWithAuthHeader(chain, getAuthHeader())
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun proceedWithAuthHeader(chain: Interceptor.Chain, token: String? = null): Response {
|
||||
val requestBuilder = chain.request().newBuilder()
|
||||
val request = if (token == null) {
|
||||
requestBuilder.removeHeader(HEADER_NAME).build()
|
||||
} else {
|
||||
requestBuilder.header(HEADER_NAME, token).build()
|
||||
}
|
||||
return chain.proceed(request)
|
||||
}
|
||||
|
||||
private fun getAuthHeader() = runBlocking {
|
||||
authenticationProvider.get().getAuthHeader()
|
||||
}
|
||||
|
||||
private fun invalidateAuthHeader() = runBlocking {
|
||||
authenticationProvider.get().invalidateAuthHeader()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val HEADER_NAME = "Authorization"
|
||||
@VisibleForTesting
|
||||
const val HEADER_NAME = "Authorization"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user