Skip to content

Commit

Permalink
fix IssuerAuthorization usage in DefaultOpenId4VciManager; IssuerAuth…
Browse files Browse the repository at this point in the history
…orization is no longer Closable; Add document name and docType in DeferredIssueResult.DocumentFailed
  • Loading branch information
vkanellopoulos committed Jul 23, 2024
1 parent 533450a commit 7106c70
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 49 deletions.
12 changes: 8 additions & 4 deletions wallet-core/src/main/java/eu/europa/ec/eudi/wallet/EudiWallet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
)
}
Expand All @@ -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")
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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<DocumentId>()
val requestMap = offer.offeredDocuments.associateBy { offeredDocument ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,44 @@ 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
* @property name the name of the document
* @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

/**
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -39,7 +38,7 @@ import kotlin.coroutines.resume
internal class IssuerAuthorization(
private val context: Context,
private val logger: Logger? = null,
) : Closeable {
) {

var continuation: CancellableContinuation<Result<Response>>? = null

Expand Down Expand Up @@ -101,7 +100,10 @@ internal class IssuerAuthorization(

}

override fun close() {
/**
* Cancels the continuation.
*/
fun close() {
continuation?.cancel(CancellationException("Authorization was cancelled"))
continuation = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
}
Expand All @@ -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
)
)
}
Expand All @@ -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
)
)
}
}

Expand All @@ -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
)
)
}
Expand All @@ -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
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class IssuerAuthorizationTest {

launch {
delay(500.milliseconds)
issuerAuthorization.use { it.resumeFromUri(uri) }
issuerAuthorization.resumeFromUri(uri)
}
}
assertNotNull(result, "Result is not null")
Expand All @@ -179,7 +179,7 @@ class IssuerAuthorizationTest {

launch {
delay(500.milliseconds)
issuerAuthorization.use { it.resumeFromUri(uri) }
issuerAuthorization.resumeFromUri(uri)
}
}
assertNotNull(result, "Result is not null")
Expand All @@ -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")
Expand All @@ -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<IssuerAuthorization.Response>? = 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")
}
}

0 comments on commit 7106c70

Please sign in to comment.