Skip to content

Commit

Permalink
fix overriding ktorHttpClientFactory in EudiWallet object for openId4…
Browse files Browse the repository at this point in the history
…Vci and openId4Vp; fix don't delete deferred documents if deferred issuing fails
  • Loading branch information
vkanellopoulos committed Jul 15, 2024
1 parent 327c006 commit 9dc119d
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 121 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ file.

```groovy
dependencies {
implementation "eu.europa.ec.eudi:eudi-lib-android-wallet-core:0.10.1-SNAPSHOT"
implementation "eu.europa.ec.eudi:eudi-lib-android-wallet-core:0.10.2-SNAPSHOT"
implementation "androidx.biometric:biometric-ktx:1.2.0-alpha05"
}
```
Expand Down Expand Up @@ -159,8 +159,11 @@ val verifierApiUri = "https://verifier-api-uri"
val config = EudiWalletConfig.Builder(applicationContext)
.logLevel(Logger.LEVEL_DEBUG)
.ktorHttpClientFactory {
// provide your own Ktor HttpClient
// this will be used for OpenId4VCI and OpenId4VP communication
// Provide your own Ktor HttpClient.
// This will be used for OpenId4VCI and OpenId4VP communication.
// If a httpClient is not provided, the library will create a default one.
// For every httpClientFactory, the result httpClient will be configured
// for logging (when logLevel is set to debug) and with json content negotiation
}
.bleTransferMode(
EudiWalletConfig.BLE_SERVER_PERIPHERAL_MODE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ androidJvm

## Functions

| Name | Summary |
|--------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [build](build.md) | [androidJvm]<br>fun [build](build.md)(): [OpenId4VciManager](../index.md)<br>Build the [OpenId4VciManager](../index.md) |
| [config](config.md) | [androidJvm]<br>fun [config](config.md)(config: [OpenId4VciManager.Config](../-config/index.md)): [OpenId4VciManager.Builder](index.md)<br>Set the [Config](../-config/index.md) to use |
| [documentManager](document-manager.md) | [androidJvm]<br>fun [documentManager](document-manager.md)(documentManager: DocumentManager): [OpenId4VciManager.Builder](index.md)<br>Set the DocumentManager to use |
| [ktHttpClientFactory](kt-http-client-factory.md) | [androidJvm]<br>fun [ktHttpClientFactory](kt-http-client-factory.md)(factory: () -&gt; HttpClient): [OpenId4VciManager.Builder](index.md)<br>Override the Ktor HTTP client factory |
| [logger](logger.md) | [androidJvm]<br>fun [logger](logger.md)(logger: [Logger](../../../eu.europa.ec.eudi.wallet.logging/-logger/index.md)): [OpenId4VciManager.Builder](index.md) |
| Name | Summary |
|------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [build](build.md) | [androidJvm]<br>fun [build](build.md)(): [OpenId4VciManager](../index.md)<br>Build the [OpenId4VciManager](../index.md) |
| [config](config.md) | [androidJvm]<br>fun [config](config.md)(config: [OpenId4VciManager.Config](../-config/index.md)): [OpenId4VciManager.Builder](index.md)<br>Set the [Config](../-config/index.md) to use |
| [documentManager](document-manager.md) | [androidJvm]<br>fun [documentManager](document-manager.md)(documentManager: DocumentManager): [OpenId4VciManager.Builder](index.md)<br>Set the DocumentManager to use |
| [ktorHttpClientFactory](ktor-http-client-factory.md) | [androidJvm]<br>fun [ktorHttpClientFactory](ktor-http-client-factory.md)(factory: () -&gt; HttpClient): [OpenId4VciManager.Builder](index.md)<br>Override the Ktor HTTP client factory |
| [logger](logger.md) | [androidJvm]<br>fun [logger](logger.md)(logger: [Logger](../../../eu.europa.ec.eudi.wallet.logging/-logger/index.md)): [OpenId4VciManager.Builder](index.md) |

## Properties

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@

# ktorHttpClientFactory

[androidJvm]\
fun [ktorHttpClientFactory](ktor-http-client-factory.md)(factory: () -&gt;
HttpClient): [OpenId4VciManager.Builder](index.md)

Override the Ktor HTTP client factory

[androidJvm]\
var [ktorHttpClientFactory](ktor-http-client-factory.md): () -&gt; HttpClient?
2 changes: 1 addition & 1 deletion docs/wallet-core/package-list
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ $dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Buil
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/config/#eu.europa.ec.eudi.wallet.issue.openid4vci.OpenId4VciManager.Config/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/config.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/documentManager/#/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/document-manager.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/documentManager/#eu.europa.ec.eudi.wallet.document.DocumentManager/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/document-manager.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/ktHttpClientFactory/#kotlin.Function0[io.ktor.client.HttpClient]/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/kt-http-client-factory.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/ktorHttpClientFactory/#/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/ktor-http-client-factory.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/ktorHttpClientFactory/#kotlin.Function0[io.ktor.client.HttpClient]/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/ktor-http-client-factory.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/logger/#/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/logger.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Builder/logger/#eu.europa.ec.eudi.wallet.logging.Logger/PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-builder/logger.md
$dokka.location:eu.europa.ec.eudi.wallet.issue.openid4vci/OpenId4VciManager.Companion///PointingToDeclaration/wallet-core/eu.europa.ec.eudi.wallet.issue.openid4vci/-open-id4-vci-manager/-companion/index.md
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ systemProp.sonar.host.url=https://sonarcloud.io
systemProp.sonar.gradle.skipCompile=true
systemProp.sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/testDebugUnitTestCoverage/testDebugUnitTestCoverage.xml,build/reports/jacoco/testReleaseUnitTestCoverage/testReleaseUnitTestCoverage.xml
systemProp.sonar.projectName=eudi-lib-android-wallet-core
VERSION_NAME=0.10.1-SNAPSHOT
VERSION_NAME=0.10.2-SNAPSHOT

SONATYPE_HOST=S01
SONATYPE_AUTOMATIC_RELEASE=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ object EudiWallet {
openId4VpConfig,
openId4VpCBORResponseGenerator,
).apply {
_config.ktorHttpClientFactory?.let { ktorHttpClientFactory = it }
logger = this@EudiWallet.logger
_config.trustedReaderCertificates?.let {
setReaderTrustStore(ReaderTrustStore.getDefault(it))
Expand Down Expand Up @@ -255,6 +256,7 @@ object EudiWallet {
documentManager(this@EudiWallet.documentManager)
config(config)
logger = this@EudiWallet.logger
ktorHttpClientFactory = _config.ktorHttpClientFactory
}.also { it.issueDocumentByDocType(docType, txCode, executor, onEvent) }
} ?: run {
(executor ?: context.mainExecutor()).execute {
Expand Down Expand Up @@ -289,6 +291,7 @@ object EudiWallet {
documentManager(this@EudiWallet.documentManager)
config(config)
logger = this@EudiWallet.logger
ktorHttpClientFactory = _config.ktorHttpClientFactory
}.also { it.issueDocumentByOffer(offer, txCode, executor, onEvent) }
} ?: run {
(executor ?: context.mainExecutor()).execute {
Expand Down Expand Up @@ -322,6 +325,7 @@ object EudiWallet {
documentManager(this@EudiWallet.documentManager)
config(config)
logger = this@EudiWallet.logger
ktorHttpClientFactory = _config.ktorHttpClientFactory
}.also { it.issueDocumentByOfferUri(offerUri, txCode, executor, onEvent) }
} ?: run {
(executor ?: context.mainExecutor()).execute {
Expand Down Expand Up @@ -351,6 +355,7 @@ object EudiWallet {
documentManager(this@EudiWallet.documentManager)
config(config)
logger = this@EudiWallet.logger
ktorHttpClientFactory = _config.ktorHttpClientFactory
}.also {
when (val document = documentManager.getDocumentById(documentId)) {
is DeferredDocument -> it.issueDeferredDocument(document, executor, onResult)
Expand Down Expand Up @@ -399,6 +404,7 @@ object EudiWallet {
documentManager(this@EudiWallet.documentManager)
config(config)
logger = this@EudiWallet.logger
ktorHttpClientFactory = _config.ktorHttpClientFactory
}.also { it.resolveDocumentOffer(offerUri, executor, onResult) }
} ?: run {
(executor ?: context.mainExecutor()).execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import eu.europa.ec.eudi.wallet.document.*
import eu.europa.ec.eudi.wallet.internal.mainExecutor
import eu.europa.ec.eudi.wallet.issue.openid4vci.IssueEvent.Companion.failure
import eu.europa.ec.eudi.wallet.logging.Logger
import eu.europa.ec.eudi.wallet.util.wrappedWithContentNegotiation
import eu.europa.ec.eudi.wallet.util.wrappedWithLogging
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
import kotlinx.coroutines.*
Expand All @@ -51,7 +53,7 @@ internal class DefaultOpenId4VciManager(

var logger: Logger? = null
var ktorHttpClientFactory: () -> HttpClient = DefaultHttpClientFactory
get() = field.wrappedWithLogging(logger)
get() = field.wrappedWithLogging(logger).wrappedWithContentNegotiation()

private val offerCreator: OfferCreator by lazy {
OfferCreator(config, ktorHttpClientFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ interface OpenId4VciManager {
/**
* Override the Ktor HTTP client factory
*/
fun ktHttpClientFactory(factory: () -> HttpClient) = apply {
fun ktorHttpClientFactory(factory: () -> HttpClient) = apply {
this.ktorHttpClientFactory = factory
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ internal class ProcessDeferredOutcome(
try {
when (outcome) {
is DeferredCredentialQueryOutcome.Errored -> {
documentManager.deleteDocumentById(deferredDocument.id)
callback(
DeferredIssueResult.DocumentFailed(
deferredDocument.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import eu.europa.ec.eudi.wallet.document.DocumentManager
import eu.europa.ec.eudi.wallet.document.UnsignedDocument
import eu.europa.ec.eudi.wallet.issue.openid4vci.OpenId4VciManager.Companion.TAG
import eu.europa.ec.eudi.wallet.logging.Logger
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
import org.bouncycastle.util.io.pem.PemObject
import org.bouncycastle.util.io.pem.PemWriter
import java.io.StringWriter
Expand Down Expand Up @@ -136,31 +134,4 @@ internal inline fun <reified V : OpenId4VciResult> OpenId4VciManager.OnResult<V>
}
}

/**
* Wraps the [HttpClient] with a logging interceptor.
* @receiver the [HttpClient] factory
* @param libraryLogger the logger
* @return the wrapped [HttpClient] factory
*/
@JvmSynthetic
internal fun (() -> HttpClient).wrappedWithLogging(libraryLogger: Logger?): (() -> HttpClient) {
return if (libraryLogger != null) {
{
val ktorLogger = object : io.ktor.client.plugins.logging.Logger {
override fun log(message: String) {
libraryLogger.d(TAG, message)
}
}
this().let { client ->
client.config {
install(Logging) {
logger = ktorLogger
level = LogLevel.ALL
}
}
}
}
} else this
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 European Commission
* Copyright (c) 2023-2024 European Commission
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,11 +30,11 @@ import eu.europa.ec.eudi.prex.JsonPath
import eu.europa.ec.eudi.prex.PresentationSubmission
import eu.europa.ec.eudi.wallet.internal.Openid4VpUtils
import eu.europa.ec.eudi.wallet.internal.mainExecutor
import eu.europa.ec.eudi.wallet.issue.openid4vci.OpenId4VciManager
import eu.europa.ec.eudi.wallet.logging.Logger
import eu.europa.ec.eudi.wallet.util.CBOR
import eu.europa.ec.eudi.wallet.util.wrappedWithContentNegotiation
import eu.europa.ec.eudi.wallet.util.wrappedWithLogging
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -45,7 +45,6 @@ import java.net.URLDecoder
import java.nio.charset.StandardCharsets
import java.util.*
import java.util.concurrent.Executor
import io.ktor.client.plugins.logging.Logger as KtorLogger

/**
* OpenId4vp manager. This class is used to manage the OpenId4vp transfer method. It is used to resolve the request uri and send the response.
Expand Down Expand Up @@ -131,22 +130,7 @@ class OpenId4vpManager(
var logger: Logger? = null

var ktorHttpClientFactory: () -> HttpClient = DefaultHttpClientFactory
get() = {
logger?.let { l ->
field().let { client ->
client.config {
install(Logging) {
logger = object : KtorLogger {
override fun log(message: String) {
l.d(OpenId4VciManager.TAG, message)
}
}
level = LogLevel.ALL
}
}
}
} ?: field()
}
get() = field.wrappedWithLogging(logger).wrappedWithContentNegotiation()

private val appContext = context.applicationContext
private val ioScope = CoroutineScope(Job() + Dispatchers.IO)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 European Commission
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.europa.ec.eudi.wallet.util

import eu.europa.ec.eudi.wallet.issue.openid4vci.OpenId4VciManager.Companion.TAG
import eu.europa.ec.eudi.wallet.logging.Logger
import io.ktor.client.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json

/**
* Wraps the [HttpClient] with a logging interceptor.
* @receiver the [HttpClient] factory
* @param libraryLogger the logger
* @return the wrapped [HttpClient] factory
*/
@JvmSynthetic
internal fun (() -> HttpClient).wrappedWithLogging(libraryLogger: Logger?): (() -> HttpClient) {
return if (libraryLogger != null) {
{
val ktorLogger = object : io.ktor.client.plugins.logging.Logger {
override fun log(message: String) {
libraryLogger.d(TAG, message)
}
}
this().let { client ->
client.config {
install(Logging) {
logger = ktorLogger
level = LogLevel.ALL
}
}
}
}
} else this
}

@JvmSynthetic
internal fun (() -> HttpClient).wrappedWithContentNegotiation(): (() -> HttpClient) {
return {
val jsonSupport = Json {
ignoreUnknownKeys = true
prettyPrint = true
}
this().let { client ->
client.config {
install(ContentNegotiation) {
json(
json = jsonSupport,
)
}
}
}
}
}
Loading

0 comments on commit 9dc119d

Please sign in to comment.