Revert "Support invalidation in MealieAuthenticator"
This reverts commit 9c48f1b563.
This commit is contained in:
@@ -4,5 +4,4 @@ interface AuthenticationProvider {
|
|||||||
|
|
||||||
suspend fun getAuthHeader(): String?
|
suspend fun getAuthHeader(): String?
|
||||||
|
|
||||||
suspend fun invalidateAuthHeader()
|
|
||||||
}
|
}
|
||||||
@@ -18,26 +18,16 @@ class MealieAuthenticator @Inject constructor(
|
|||||||
|
|
||||||
override fun authenticate(route: Route?, response: Response): Request? {
|
override fun authenticate(route: Route?, response: Response): Request? {
|
||||||
val supportsBearer = response.challenges().any { it.scheme == BEARER_SCHEME }
|
val supportsBearer = response.challenges().any { it.scheme == BEARER_SCHEME }
|
||||||
if (!supportsBearer) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
val request = response.request
|
val request = response.request
|
||||||
val previousHeader = request.header(HEADER_NAME)
|
return if (supportsBearer && request.header(HEADER_NAME) == null) {
|
||||||
?: return getAuthHeader()?.let { request.copyWithHeader(HEADER_NAME, it) }
|
getAuthHeader()?.let { request.copyWithHeader(HEADER_NAME, it) }
|
||||||
|
} else {
|
||||||
invalidateAuthHeader()
|
null // Either Bearer is not supported or we've already tried to authenticate
|
||||||
return getAuthHeader()?.takeUnless { it == previousHeader }?.let {
|
|
||||||
request.copyWithHeader(HEADER_NAME, it)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getAuthHeader() = runBlocking { authenticationProvider.get().getAuthHeader() }
|
private fun getAuthHeader() = runBlocking { authenticationProvider.get().getAuthHeader() }
|
||||||
|
|
||||||
private fun invalidateAuthHeader() {
|
|
||||||
runBlocking { authenticationProvider.get().invalidateAuthHeader() }
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
const val HEADER_NAME = "Authorization"
|
const val HEADER_NAME = "Authorization"
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class MealieAuthenticatorTest : BaseUnitTest() {
|
|||||||
|
|
||||||
private lateinit var subject: MealieAuthenticator
|
private lateinit var subject: MealieAuthenticator
|
||||||
|
|
||||||
@MockK(relaxUnitFun = true)
|
@MockK
|
||||||
lateinit var authenticationProvider: AuthenticationProvider
|
lateinit var authenticationProvider: AuthenticationProvider
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -48,34 +48,11 @@ class MealieAuthenticatorTest : BaseUnitTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when had auth header but can't invalidate expect authenticate return null`() {
|
fun `when no auth header was set expect authenticate to return null`() {
|
||||||
coEvery { authenticationProvider.getAuthHeader() } returns null
|
|
||||||
val response = buildResponse(authHeader = "token")
|
val response = buildResponse(authHeader = "token")
|
||||||
assertThat(subject.authenticate(null, response)).isNull()
|
assertThat(subject.authenticate(null, response)).isNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when had auth header and invalidate doesn't change it expect authenticate return null`() {
|
|
||||||
coEvery { authenticationProvider.getAuthHeader() } returns "token"
|
|
||||||
val response = buildResponse(authHeader = "token")
|
|
||||||
assertThat(subject.authenticate(null, response)).isNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when had auth header and invalidate succeeds expect authenticate return new`() {
|
|
||||||
coEvery { authenticationProvider.getAuthHeader() } returns "newToken"
|
|
||||||
val response = buildResponse(authHeader = "token")
|
|
||||||
assertThat(subject.authenticate(null, response)?.header(HEADER_NAME)).isEqualTo("newToken")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when had auth header expect authenticate to invalidate it`() {
|
|
||||||
coEvery { authenticationProvider.getAuthHeader() } returns null
|
|
||||||
val response = buildResponse(authHeader = "token")
|
|
||||||
subject.authenticate(null, response)
|
|
||||||
coVerify { authenticationProvider.invalidateAuthHeader() }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when auth header exists expect authenticate to return request`() {
|
fun `when auth header exists expect authenticate to return request`() {
|
||||||
coEvery { authenticationProvider.getAuthHeader() } returns "token"
|
coEvery { authenticationProvider.getAuthHeader() } returns "token"
|
||||||
|
|||||||
Reference in New Issue
Block a user