diff --git a/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ContainerRegistryApiImpl.kt b/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ContainerRegistryApiImpl.kt index 4b7ae95..3841cb3 100644 --- a/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ContainerRegistryApiImpl.kt +++ b/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ContainerRegistryApiImpl.kt @@ -5,7 +5,6 @@ import com.github.kittinunf.fuel.core.FuelManager import com.github.kittinunf.fuel.core.Headers import com.github.kittinunf.fuel.core.Parameters import com.github.kittinunf.fuel.core.awaitResponseResult -import com.github.kittinunf.fuel.core.awaitResult import com.github.kittinunf.fuel.core.deserializers.ByteArrayDeserializer import com.github.kittinunf.fuel.core.deserializers.EmptyDeserializer import com.github.kittinunf.result.Result @@ -37,7 +36,10 @@ internal class ContainerRegistryApiImpl(private val fuelManager: FuelManager, cr private val handler = ResponseRetryWithAuthentication(credentials, fuelManager) - override suspend fun ping(): Result<*, FuelError> = fuelManager.get("/").awaitResult(EmptyDeserializer) + override suspend fun ping(): Result<*, FuelError> = fuelManager.get("/") + .awaitResponseResult(EmptyDeserializer) + .let { responseResult -> handler.retryOnUnauthorized(responseResult, EmptyDeserializer) } + .third override suspend fun repositories(limit: Int?, last: Int?): Result { val parameter: Parameters = buildList { diff --git a/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ResponseRetryWithAuthentication.kt b/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ResponseRetryWithAuthentication.kt index c038d52..3f5f775 100644 --- a/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ResponseRetryWithAuthentication.kt +++ b/kirc-suspending/src/main/kotlin/de/cmdjulian/kirc/impl/ResponseRetryWithAuthentication.kt @@ -35,15 +35,9 @@ internal class ResponseRetryWithAuthentication( } private suspend fun retryRequest(header: String?, request: Request): Request? { - if (header == null) return null + val wwwAuth = header?.runCatching { HttpAuthCredentials.parse(this) }?.getOrNull() - val wwwAuth = try { - HttpAuthCredentials.parse(header) - } catch (e: Exception) { - return null - } - - return when (wwwAuth.scheme) { + return when (wwwAuth?.scheme) { "Basic" -> resolveBasicAuth(request) "Bearer" -> resolveTokenAuth(wwwAuth, request) else -> null @@ -58,12 +52,16 @@ internal class ResponseRetryWithAuthentication( private suspend fun resolveTokenAuth(wwwAuth: HttpAuthCredentials, request: Request): Request? { val realm = wwwAuth.singleValueParams["realm"]!!.replace("\"", "") - val service = wwwAuth.singleValueParams["service"]!!.replace("\"", "") - val scope = wwwAuth.singleValueParams["scope"]!!.replace("\"", "") + val scope = wwwAuth.singleValueParams["scope"]?.replace("\"", "") + val service = wwwAuth.singleValueParams["service"]?.replace("\"", "") class TokenResponse(val token: String) - val token = FuelManager.instance.get(realm, listOf("service" to service, "scope" to scope)) + val parameters = buildList { + if (scope != null) add("scope" to scope) + if (service != null) add("service" to service) + } + val token = FuelManager.instance.get(realm, parameters) .let { credentials?.run { AuthenticatedRequest(it).basic(username, password) } ?: it } .awaitResponseResult(jacksonDeserializer()) .third