From 7106c706d2506fbbf478ee1b15f215ccd31323b8 Mon Sep 17 00:00:00 2001 From: vkanellopoulos Date: Tue, 23 Jul 2024 18:46:15 +0300 Subject: [PATCH] fix IssuerAuthorization usage in DefaultOpenId4VciManager; IssuerAuthorization is no longer Closable; Add document name and docType in DeferredIssueResult.DocumentFailed --- .../eu/europa/ec/eudi/wallet/EudiWallet.kt | 12 ++++-- .../openid4vci/DefaultOpenId4VciManager.kt | 11 ++++- .../issue/openid4vci/DeferredIssueResult.kt | 31 ++++++++++++-- .../issue/openid4vci/IssuerAuthorization.kt | 8 ++-- .../openid4vci/ProcessDeferredOutcome.kt | 41 ++++++++++++------- .../openid4vci/IssuerAuthorizationTest.kt | 26 ++---------- 6 files changed, 80 insertions(+), 49 deletions(-) diff --git a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/EudiWallet.kt b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/EudiWallet.kt index 8aaa9332..2febc0c9 100644 --- a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/EudiWallet.kt +++ b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/EudiWallet.kt @@ -362,8 +362,10 @@ object EudiWallet { else -> (executor ?: context.mainExecutor()).execute { onResult( DeferredIssueResult.DocumentFailed( - documentId, - IllegalStateException("Document is not deferred") + documentId = documentId, + name = document?.name ?: "", + docType = document?.docType ?: "", + cause = IllegalStateException("Document is not deferred") ) ) } @@ -374,8 +376,10 @@ object EudiWallet { (executor ?: context.mainExecutor()).execute { onResult( DeferredIssueResult.DocumentFailed( - documentId, - IllegalStateException("OpenId4Vci config is not set in configuration") + documentId = documentId, + name = "", + docType = "", + cause = IllegalStateException("OpenId4Vci config is not set in configuration") ) ) } diff --git a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DefaultOpenId4VciManager.kt b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DefaultOpenId4VciManager.kt index 74c58b04..8f223169 100644 --- a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DefaultOpenId4VciManager.kt +++ b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DefaultOpenId4VciManager.kt @@ -137,7 +137,14 @@ internal class DefaultOpenId4VciManager( ).process(deferredDocument, outcome) } catch (e: Throwable) { - callback(DeferredIssueResult.DocumentFailed(deferredDocument.id, e)) + callback( + DeferredIssueResult.DocumentFailed( + documentId = deferredDocument.id, + name = deferredDocument.name, + docType = deferredDocument.docType, + cause = e + ) + ) coroutineScope.cancel("issueDeferredDocument failed", e) } } @@ -194,7 +201,7 @@ internal class DefaultOpenId4VciManager( ) { offer as DefaultOffer val issuer = issuerCreator.createIssuer(offer) - var authorizedRequest = issuerAuthorization.use { it.authorize(issuer, txCode) } + var authorizedRequest = issuerAuthorization.authorize(issuer, txCode) listener(IssueEvent.Started(offer.offeredDocuments.size)) val issuedDocumentIds = mutableListOf() val requestMap = offer.offeredDocuments.associateBy { offeredDocument -> diff --git a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DeferredIssueResult.kt b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DeferredIssueResult.kt index 63f34062..79f440c7 100644 --- a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DeferredIssueResult.kt +++ b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/DeferredIssueResult.kt @@ -18,7 +18,18 @@ package eu.europa.ec.eudi.wallet.issue.openid4vci import eu.europa.ec.eudi.wallet.document.DocumentId +/** + * Result of a deferred document issuance. + * @property documentId the id of the document + * @property name the name of the document + * @property docType the document type + */ sealed interface DeferredIssueResult : OpenId4VciResult { + + val documentId: DocumentId + val name: String + val docType: String + /** * Document issued successfully. * @property documentId the id of the issued document @@ -26,15 +37,25 @@ sealed interface DeferredIssueResult : OpenId4VciResult { * @property docType the document type * @see[DocumentId] for the document id */ - data class DocumentIssued(val documentId: DocumentId, val name: String, val docType: String) : DeferredIssueResult + data class DocumentIssued( + override val documentId: DocumentId, + override val name: String, + override val docType: String, + ) : DeferredIssueResult /** * Document issuance failed. + * @property documentId the id of the failed document * @property name the name of the document * @property docType the document type * @property cause the error that caused the failure */ - data class DocumentFailed(val documentId: DocumentId, override val cause: Throwable) : DeferredIssueResult, + data class DocumentFailed( + override val documentId: DocumentId, + override val name: String, + override val docType: String, + override val cause: Throwable, + ) : DeferredIssueResult, OpenId4VciResult.Erroneous /** @@ -43,5 +64,9 @@ sealed interface DeferredIssueResult : OpenId4VciResult { * @property name the name of the document * @property docType the document type */ - data class DocumentNotReady(val documentId: DocumentId, val name: String, val docType: String) : DeferredIssueResult + data class DocumentNotReady( + override val documentId: DocumentId, + override val name: String, + override val docType: String, + ) : DeferredIssueResult } diff --git a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorization.kt b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorization.kt index e04d26ec..34c8d5cc 100644 --- a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorization.kt +++ b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorization.kt @@ -28,7 +28,6 @@ import eu.europa.ec.eudi.wallet.issue.openid4vci.OpenId4VciManager.Companion.TAG import eu.europa.ec.eudi.wallet.logging.Logger import kotlinx.coroutines.CancellableContinuation import kotlinx.coroutines.suspendCancellableCoroutine -import java.io.Closeable import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.resume @@ -39,7 +38,7 @@ import kotlin.coroutines.resume internal class IssuerAuthorization( private val context: Context, private val logger: Logger? = null, -) : Closeable { +) { var continuation: CancellableContinuation>? = null @@ -101,7 +100,10 @@ internal class IssuerAuthorization( } - override fun close() { + /** + * Cancels the continuation. + */ + fun close() { continuation?.cancel(CancellationException("Authorization was cancelled")) continuation = null } diff --git a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/ProcessDeferredOutcome.kt b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/ProcessDeferredOutcome.kt index da59975b..b60ac6f2 100644 --- a/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/ProcessDeferredOutcome.kt +++ b/wallet-core/src/main/java/eu/europa/ec/eudi/wallet/issue/openid4vci/ProcessDeferredOutcome.kt @@ -39,8 +39,10 @@ internal class ProcessDeferredOutcome( is DeferredCredentialQueryOutcome.Errored -> { callback( DeferredIssueResult.DocumentFailed( - deferredDocument.id, - IllegalStateException(outcome.error) + documentId = deferredDocument.id, + name = deferredDocument.name, + docType = deferredDocument.docType, + cause = IllegalStateException(outcome.error) ) ) } @@ -51,9 +53,9 @@ internal class ProcessDeferredOutcome( .notifyListener(deferredDocument, true) } ?: callback( DeferredIssueResult.DocumentNotReady( - deferredDocument.id, - deferredDocument.name, - deferredDocument.docType + documentId = deferredDocument.id, + name = deferredDocument.name, + docType = deferredDocument.docType ) ) } @@ -66,8 +68,14 @@ internal class ProcessDeferredOutcome( } } } catch (e: Throwable) { - documentManager.deleteDocumentById(deferredDocument.id) - callback(DeferredIssueResult.DocumentFailed(deferredDocument.id, e)) + callback( + DeferredIssueResult.DocumentFailed( + documentId = deferredDocument.id, + name = deferredDocument.name, + docType = deferredDocument.docType, + cause = e + ) + ) } } @@ -78,17 +86,17 @@ internal class ProcessDeferredOutcome( if (isDeferred) { callback( DeferredIssueResult.DocumentNotReady( - documentId, - deferredDocument.name, - deferredDocument.docType + documentId = documentId, + name = deferredDocument.name, + docType = deferredDocument.docType ) ) } else { callback( DeferredIssueResult.DocumentIssued( - documentId, - deferredDocument.name, - deferredDocument.docType + documentId = documentId, + name = deferredDocument.name, + docType = deferredDocument.docType ) ) } @@ -97,7 +105,12 @@ internal class ProcessDeferredOutcome( is StoreDocumentResult.Failure -> { documentManager.deleteDocumentById(deferredDocument.id) callback( - DeferredIssueResult.DocumentFailed(deferredDocument.id, throwable) + DeferredIssueResult.DocumentFailed( + documentId = deferredDocument.id, + name = deferredDocument.name, + docType = deferredDocument.docType, + cause = throwable + ) ) } } diff --git a/wallet-core/src/test/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorizationTest.kt b/wallet-core/src/test/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorizationTest.kt index fe3b4116..c696f900 100644 --- a/wallet-core/src/test/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorizationTest.kt +++ b/wallet-core/src/test/java/eu/europa/ec/eudi/wallet/issue/openid4vci/IssuerAuthorizationTest.kt @@ -152,7 +152,7 @@ class IssuerAuthorizationTest { launch { delay(500.milliseconds) - issuerAuthorization.use { it.resumeFromUri(uri) } + issuerAuthorization.resumeFromUri(uri) } } assertNotNull(result, "Result is not null") @@ -179,7 +179,7 @@ class IssuerAuthorizationTest { launch { delay(500.milliseconds) - issuerAuthorization.use { it.resumeFromUri(uri) } + issuerAuthorization.resumeFromUri(uri) } } assertNotNull(result, "Result is not null") @@ -205,7 +205,7 @@ class IssuerAuthorizationTest { launch(Dispatchers.Default) { delay(500.milliseconds) - issuerAuthorization.use { it.resumeFromUri(uri) } + issuerAuthorization.resumeFromUri(uri) } } assertNotNull(result, "Result is not null") @@ -214,24 +214,4 @@ class IssuerAuthorizationTest { issuerAuthorization.resumeFromUri(uri) } } - - @Test - fun `close cancels the continuation`() { - val issuerAuthorization: IssuerAuthorization = spyk(IssuerAuthorization(context, logger)) - var result: Result? = null - runTest { - launch { - result = issuerAuthorization.openBrowserForAuthorization(preparedAuthorizationRequest) - } - - launch { - delay(500.milliseconds) - issuerAuthorization.close() - - } - } - assertNull(result, "Result is null") - verify(exactly = 1) { issuerAuthorization.close() } - assertNull(issuerAuthorization.continuation, "Continuation is removed") - } } \ No newline at end of file