Skip to content

Commit

Permalink
Fix: Remove dependency on ktor-client from implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
acrusage-iaik committed Apr 2, 2024
1 parent d1d1d85 commit 03a0cdd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 43 deletions.
2 changes: 1 addition & 1 deletion vclib-openid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ kotlin {
dependencies {
api(project(":vclib"))
commonImplementationDependencies()
implementation(ktor("client-core"))
}
}

commonTest {
dependencies {
implementation(ktor("client-core"))
implementation(ktor("client-mock"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ import at.asitplus.wallet.lib.oidvci.encodeToParameters
import at.asitplus.wallet.lib.oidvci.formUrlEncode
import com.benasher44.uuid.uuid4
import io.github.aakira.napier.Napier
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpHeaders
import io.ktor.http.URLBuilder
import io.ktor.http.Url
import io.ktor.util.flattenEntries
Expand Down Expand Up @@ -73,28 +69,6 @@ class OidcSiopWallet(
) {
companion object {
fun newInstance(
holder: Holder,
cryptoService: CryptoService,
jwsService: JwsService = DefaultJwsService(cryptoService),
verifierJwsService: VerifierJwsService = DefaultVerifierJwsService(),
clock: Clock = Clock.System,
httpClient: HttpClient,
clientId: String = "https://wallet.a-sit.at/",
jwkSetRetriever: (String) -> JsonWebKeySet? = { null },
) = OidcSiopWallet(
holder = holder,
agentPublicKey = cryptoService.publicKey,
jwsService = jwsService,
verifierJwsService = verifierJwsService,
clock = clock,
clientId = clientId,
jwkSetRetriever = jwkSetRetriever,
requestObjectCandidateRetriever = httpClient.asRequestObjectCandidateRetriever,
)

// mark this as internal so it can be used for testing purposes
// the request object candidate retriever should usually be derived from a http client as in the other constructor
internal fun newInstance(
holder: Holder,
cryptoService: CryptoService,
jwsService: JwsService = DefaultJwsService(cryptoService),
Expand Down Expand Up @@ -466,16 +440,4 @@ class OidcSiopWallet(

}

typealias RequestObjectCandidateRetriever = suspend (Url) -> List<String>

private val HttpClient.asRequestObjectCandidateRetriever: RequestObjectCandidateRetriever
get() = {
// currently supported in order of priority:
// 1. use redirect location as new starting point if available
// 2. use resonse body as new starting point
val response = this.get(it)
listOfNotNull(
response.headers[HttpHeaders.Location],
response.bodyAsText(),
)
}
typealias RequestObjectCandidateRetriever = suspend (Url) -> List<String>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.respond
import io.ktor.client.engine.mock.respondBadRequest
import io.ktor.client.engine.mock.respondRedirect
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.URLBuilder
import io.ktor.http.Url
Expand Down Expand Up @@ -246,7 +249,7 @@ class OidcSiopProtocolTest : FreeSpec({
holderSiop = OidcSiopWallet.newInstance(
holder = holderAgent,
cryptoService = holderCryptoService,
httpClient = httpClient,
requestObjectCandidateRetriever = httpClient.asRequestObjectCandidateRetriever()
)

val authnResponse =
Expand Down Expand Up @@ -303,7 +306,7 @@ class OidcSiopProtocolTest : FreeSpec({
holderSiop = OidcSiopWallet.newInstance(
holder = holderAgent,
cryptoService = holderCryptoService,
httpClient = httpClient,
requestObjectCandidateRetriever = httpClient.asRequestObjectCandidateRetriever()
)

val authnResponse =
Expand Down Expand Up @@ -365,7 +368,7 @@ class OidcSiopProtocolTest : FreeSpec({
holderSiop = OidcSiopWallet.newInstance(
holder = holderAgent,
cryptoService = holderCryptoService,
httpClient = httpClient,
requestObjectCandidateRetriever = httpClient.asRequestObjectCandidateRetriever()
)

val authnResponse =
Expand Down Expand Up @@ -393,4 +396,15 @@ private suspend fun verifySecondProtocolRun(
(authnResponse.getOrThrow() as OidcSiopWallet.AuthenticationResponseResult.Redirect).url
)
validation.shouldBeInstanceOf<OidcSiopVerifier.AuthnResponseResult.Success>()
}

private fun HttpClient.asRequestObjectCandidateRetriever(): RequestObjectCandidateRetriever = {
// currently supported in order of priority:
// 1. use redirect location as new starting point if available
// 2. use resonse body as new starting point
val response = this.get(it)
listOfNotNull(
response.headers[HttpHeaders.Location],
response.bodyAsText(),
)
}

0 comments on commit 03a0cdd

Please sign in to comment.