Support invalidation in MealieAuthenticator

This commit is contained in:
Kirill Kamakin
2022-12-11 10:17:13 +01:00
parent ac20105b9b
commit 9c48f1b563
3 changed files with 40 additions and 6 deletions

View File

@@ -4,4 +4,5 @@ interface AuthenticationProvider {
suspend fun getAuthHeader(): String? suspend fun getAuthHeader(): String?
suspend fun invalidateAuthHeader()
} }

View File

@@ -18,16 +18,26 @@ 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
return if (supportsBearer && request.header(HEADER_NAME) == null) { val previousHeader = request.header(HEADER_NAME)
getAuthHeader()?.let { request.copyWithHeader(HEADER_NAME, it) } ?: return getAuthHeader()?.let { request.copyWithHeader(HEADER_NAME, it) }
} else {
null // Either Bearer is not supported or we've already tried to authenticate invalidateAuthHeader()
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"

View File

@@ -17,7 +17,7 @@ class MealieAuthenticatorTest : BaseUnitTest() {
private lateinit var subject: MealieAuthenticator private lateinit var subject: MealieAuthenticator
@MockK @MockK(relaxUnitFun = true)
lateinit var authenticationProvider: AuthenticationProvider lateinit var authenticationProvider: AuthenticationProvider
@Before @Before
@@ -48,11 +48,34 @@ class MealieAuthenticatorTest : BaseUnitTest() {
} }
@Test @Test
fun `when no auth header was set expect authenticate to return null`() { fun `when had auth header but can't invalidate expect authenticate 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"