Skip to content

Commit

Permalink
fixed auth on ping endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdjulian committed Sep 15, 2023
1 parent 0c6d19d commit dc34653
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Catalog, FuelError> {
val parameter: Parameters = buildList {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<TokenResponse>())
.third
Expand Down

0 comments on commit dc34653

Please sign in to comment.