Skip to content

Commit

Permalink
Rename PresentationId to TransactionId (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
babisRoutis authored Jan 11, 2024
1 parent 5c1e6e7 commit f9c7c50
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import eu.europa.ec.eudi.verifier.endpoint.adapter.input.timer.ScheduleTimeoutPr
import eu.europa.ec.eudi.verifier.endpoint.adapter.input.web.StaticContent
import eu.europa.ec.eudi.verifier.endpoint.adapter.input.web.VerifierApi
import eu.europa.ec.eudi.verifier.endpoint.adapter.input.web.WalletApi
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.cfg.GeneratePresentationIdNimbus
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.cfg.GenerateRequestIdNimbus
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.cfg.GenerateTransactionIdNimbus
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.jose.GenerateEphemeralEncryptionKeyPairNimbus
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.jose.ParseJarmOptionNimbus
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.jose.SignRequestObjectNimbus
Expand Down Expand Up @@ -64,7 +64,7 @@ internal fun beans(clock: Clock) = beans {
//
// Persistence
//
bean { GeneratePresentationIdNimbus(64) }
bean { GenerateTransactionIdNimbus(64) }
bean { GenerateRequestIdNimbus(64) }
with(PresentationInMemoryRepo()) {
bean { loadPresentationById }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package eu.europa.ec.eudi.verifier.endpoint.adapter.input.web

import arrow.core.raise.either
import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.RequestId
import eu.europa.ec.eudi.verifier.endpoint.domain.ResponseCode
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId
import eu.europa.ec.eudi.verifier.endpoint.port.input.*
import kotlinx.serialization.SerializationException
import org.slf4j.Logger
Expand Down Expand Up @@ -49,7 +49,10 @@ class VerifierApi(
val input = req.awaitBody<InitTransactionTO>()
logger.info("Handling InitTransaction nonce=${input.nonce} ... ")
either { initTransaction(input) }.fold(
ifRight = { ok().json().bodyValueAndAwait(it) },
ifRight = { it ->
logger.info("Initiated transaction ${it.transactionId}")
ok().json().bodyValueAndAwait(it)
},
ifLeft = { it.asBadRequest() },
)
} catch (t: SerializationException) {
Expand All @@ -64,11 +67,11 @@ class VerifierApi(
private suspend fun handleGetWalletResponse(req: ServerRequest): ServerResponse {
suspend fun found(walletResponse: WalletResponseTO) = ok().json().bodyValueAndAwait(walletResponse)

val presentationId = req.presentationId()
val transactionId = req.transactionId()
val responseCode = req.queryParam("response_code").getOrNull()?.let { ResponseCode(it) }

logger.info("Handling GetWalletResponse for $presentationId and response_code: $responseCode ...")
return when (val result = getWalletResponse(presentationId, responseCode)) {
logger.info("Handling GetWalletResponse for $transactionId and response_code: $responseCode ...")
return when (val result = getWalletResponse(transactionId, responseCode)) {
is QueryResponse.NotFound -> ServerResponse.notFound().buildAndAwait()
is QueryResponse.InvalidState -> badRequest().buildAndAwait()
is QueryResponse.Found -> found(result.value)
Expand All @@ -77,12 +80,12 @@ class VerifierApi(

companion object {
const val INIT_TRANSACTION_PATH = "/ui/presentations"
const val WALLET_RESPONSE_PATH = "/ui/presentations/{presentationId}"
const val WALLET_RESPONSE_PATH = "/ui/presentations/{transactionId}"

/**
* Extracts from the request the [RequestId]
*/
private fun ServerRequest.presentationId() = PresentationId(pathVariable("presentationId"))
private fun ServerRequest.transactionId() = TransactionId(pathVariable("transactionId"))
private suspend fun ValidationError.asBadRequest() =
badRequest().json().bodyValueAndAwait(mapOf("error" to this))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
package eu.europa.ec.eudi.verifier.endpoint.adapter.out.cfg

import com.nimbusds.oauth2.sdk.id.Identifier
import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GeneratePresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GenerateTransactionId

class GeneratePresentationIdNimbus(private val byteLength: Int) : GeneratePresentationId {
class GenerateTransactionIdNimbus(private val byteLength: Int) : GenerateTransactionId {

init {
require(byteLength >= 32) { "Value should be greater or equal to 32" }
}
override suspend fun invoke(): PresentationId = PresentationId(Identifier(byteLength).value)

override suspend fun invoke(): TransactionId = TransactionId(Identifier(byteLength).value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package eu.europa.ec.eudi.verifier.endpoint.adapter.out.persistence

import eu.europa.ec.eudi.verifier.endpoint.domain.Presentation
import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId
import eu.europa.ec.eudi.verifier.endpoint.domain.isExpired
import eu.europa.ec.eudi.verifier.endpoint.port.out.persistence.LoadIncompletePresentationsOlderThan
import eu.europa.ec.eudi.verifier.endpoint.port.out.persistence.LoadPresentationById
Expand All @@ -28,7 +28,7 @@ import java.util.concurrent.ConcurrentHashMap
* An input-memory repository for storing [presentations][Presentation]
*/
class PresentationInMemoryRepo(
private val presentations: ConcurrentHashMap<PresentationId, Presentation> = ConcurrentHashMap(),
private val presentations: ConcurrentHashMap<TransactionId, Presentation> = ConcurrentHashMap(),
) {

val loadPresentationById: LoadPresentationById by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.time.Clock
import java.time.Instant

@JvmInline
value class PresentationId(val value: String) {
value class TransactionId(val value: String) {
init {
require(value.isNotBlank())
}
Expand Down Expand Up @@ -130,15 +130,15 @@ sealed interface GetWalletResponseMethod {
* The entity that represents the presentation process
*/
sealed interface Presentation {
val id: PresentationId
val id: TransactionId
val initiatedAt: Instant
val type: PresentationType

/**
* A presentation process that has been just requested
*/
class Requested(
override val id: PresentationId,
override val id: TransactionId,
override val initiatedAt: Instant,
override val type: PresentationType,
val requestId: RequestId,
Expand All @@ -156,7 +156,7 @@ sealed interface Presentation {
* or later on (when using request_uri JAR parameter)
*/
class RequestObjectRetrieved private constructor(
override val id: PresentationId,
override val id: TransactionId,
override val initiatedAt: Instant,
override val type: PresentationType,
val requestId: RequestId,
Expand Down Expand Up @@ -192,7 +192,7 @@ sealed interface Presentation {
* A presentation process that has been just submitted by the wallet to the verifier backend
*/
class Submitted private constructor(
override val id: PresentationId,
override val id: TransactionId,
override val initiatedAt: Instant,
override val type: PresentationType,
val requestId: RequestId,
Expand Down Expand Up @@ -232,7 +232,7 @@ sealed interface Presentation {
}

class TimedOut private constructor(
override val id: PresentationId,
override val id: TransactionId,
override val initiatedAt: Instant,
override val type: PresentationType,
val requestObjectRetrievedAt: Instant? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ private fun WalletResponse.toTO(): WalletResponseTO {
* Given a [PresentationId] and a [Nonce] returns the [WalletResponse]
*/
interface GetWalletResponse {
suspend operator fun invoke(presentationId: PresentationId, responseCode: ResponseCode?): QueryResponse<WalletResponseTO>
suspend operator fun invoke(presentationId: TransactionId, responseCode: ResponseCode?): QueryResponse<WalletResponseTO>
}

class GetWalletResponseLive(
private val loadPresentationById: LoadPresentationById,
) : GetWalletResponse {
override suspend fun invoke(presentationId: PresentationId, responseCode: ResponseCode?): QueryResponse<WalletResponseTO> {
override suspend fun invoke(presentationId: TransactionId, responseCode: ResponseCode?): QueryResponse<WalletResponseTO> {
return when (val presentation = loadPresentationById(presentationId)) {
null -> NotFound
is Presentation.Submitted ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalSerializationApi::class)

package eu.europa.ec.eudi.verifier.endpoint.port.input

import arrow.core.raise.Raise
Expand All @@ -21,11 +23,12 @@ import arrow.core.raise.ensureNotNull
import eu.europa.ec.eudi.prex.PresentationDefinition
import eu.europa.ec.eudi.verifier.endpoint.domain.*
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.CreateQueryWalletResponseRedirectUri
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GeneratePresentationId
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GenerateRequestId
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GenerateTransactionId
import eu.europa.ec.eudi.verifier.endpoint.port.out.jose.GenerateEphemeralEncryptionKeyPair
import eu.europa.ec.eudi.verifier.endpoint.port.out.jose.SignRequestObject
import eu.europa.ec.eudi.verifier.endpoint.port.out.persistence.StorePresentation
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Required
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -112,7 +115,7 @@ enum class ValidationError {
*/
@Serializable
data class JwtSecuredAuthorizationRequestTO(
@Required @SerialName("presentation_id") val presentationId: String,
@Required @SerialName("presentation_id") val transactionId: String,
@Required @SerialName("client_id") val clientId: String,
@SerialName("request") val request: String? = null,
@SerialName("request_uri") val requestUri: String?,
Expand All @@ -136,7 +139,7 @@ fun interface InitTransaction {
* The default implementation of the use case
*/
class InitTransactionLive(
private val generatePresentationId: GeneratePresentationId,
private val generateTransactionId: GenerateTransactionId,
private val generateRequestId: GenerateRequestId,
private val storePresentation: StorePresentation,
private val signRequestObject: SignRequestObject,
Expand All @@ -161,7 +164,7 @@ class InitTransactionLive(

// Initialize presentation
val requestedPresentation = Presentation.Requested(
id = generatePresentationId(),
id = generateTransactionId(),
initiatedAt = clock.instant(),
requestId = generateRequestId(),
type = type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package eu.europa.ec.eudi.verifier.endpoint.port.input

import eu.europa.ec.eudi.verifier.endpoint.domain.Presentation
import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId
import eu.europa.ec.eudi.verifier.endpoint.domain.timedOut
import eu.europa.ec.eudi.verifier.endpoint.port.out.persistence.LoadIncompletePresentationsOlderThan
import eu.europa.ec.eudi.verifier.endpoint.port.out.persistence.StorePresentation
Expand All @@ -25,7 +25,7 @@ import java.time.Duration

fun interface TimeoutPresentations {

suspend operator fun invoke(): List<PresentationId>
suspend operator fun invoke(): List<TransactionId>
}

class TimeoutPresentationsLive(
Expand All @@ -34,7 +34,7 @@ class TimeoutPresentationsLive(
private val maxAge: Duration,
private val clock: Clock,
) : TimeoutPresentations {
override suspend operator fun invoke(): List<PresentationId> {
override suspend operator fun invoke(): List<TransactionId> {
val expireBefore = clock.instant().minusSeconds(maxAge.toSeconds())
return loadIncompletePresentationsOlderThan(expireBefore).mapNotNull { timeout(it)?.id }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/
package eu.europa.ec.eudi.verifier.endpoint.port.out.cfg

import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId

/**
* A port for generating [PresentationId]
* A port for generating [TransactionId]
*/
fun interface GeneratePresentationId {
suspend operator fun invoke(): PresentationId
fun interface GenerateTransactionId {
suspend operator fun invoke(): TransactionId

companion object {

/**
* Fixed generator, useful input tests
*/
fun fixed(id: PresentationId): GeneratePresentationId = GeneratePresentationId { id }
fun fixed(id: TransactionId): GenerateTransactionId = GenerateTransactionId { id }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package eu.europa.ec.eudi.verifier.endpoint.port.out.persistence

import eu.europa.ec.eudi.verifier.endpoint.domain.Presentation
import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId

/**
* Loads a [Presentation] from a storage
*/
fun interface LoadPresentationById {
suspend operator fun invoke(presentationProcessById: PresentationId): Presentation?
suspend operator fun invoke(presentationProcessById: TransactionId): Presentation?
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import eu.europa.ec.eudi.verifier.endpoint.port.input.GetRequestObjectLive
import eu.europa.ec.eudi.verifier.endpoint.port.input.InitTransaction
import eu.europa.ec.eudi.verifier.endpoint.port.input.InitTransactionLive
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.CreateQueryWalletResponseRedirectUri
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GeneratePresentationId
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GenerateRequestId
import eu.europa.ec.eudi.verifier.endpoint.port.out.cfg.GenerateTransactionId
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.annotation.Configuration
Expand All @@ -49,13 +49,13 @@ import java.util.*
import kotlin.reflect.KClass

object TestContext {
val testDate = LocalDate.of(1974, 11, 2).atTime(10, 5, 33)
private val testDate = LocalDate.of(1974, 11, 2).atTime(10, 5, 33)
val testClock = Clock.fixed(testDate.toInstant(ZoneOffset.UTC), ZoneOffset.UTC)
val testPresentationId = PresentationId("SamplePresentationId")
val generatedPresentationId = GeneratePresentationId.fixed(testPresentationId)
val testTransactionId = TransactionId("SampleTxId")
private val generatedTransactionId = GenerateTransactionId.fixed(testTransactionId)
val testRequestId = RequestId("SampleRequestId")
val generateRequestId = GenerateRequestId.fixed(testRequestId)
val rsaJwk = RSAKeyGenerator(2048)
private val generateRequestId = GenerateRequestId.fixed(testRequestId)
private val rsaJwk = RSAKeyGenerator(2048)
.keyUse(KeyUse.SIGNATURE) // indicate the intended use of the key (optional)
.keyID(UUID.randomUUID().toString()) // give the key a unique ID (optional)
.issueTime(Date()) // issued-at timestamp (optional)
Expand Down Expand Up @@ -84,7 +84,7 @@ object TestContext {
presentationDefinitionByReference: EmbedOption.ByReference<RequestId>,
): InitTransaction =
InitTransactionLive(
generatedPresentationId,
generatedTransactionId,
generateRequestId,
storePresentation,
singRequestObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package eu.europa.ec.eudi.verifier.endpoint.adapter.input.web

import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.ResponseCode
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId
import eu.europa.ec.eudi.verifier.endpoint.port.input.InitTransactionTO
import eu.europa.ec.eudi.verifier.endpoint.port.input.JwtSecuredAuthorizationRequestTO
import eu.europa.ec.eudi.verifier.endpoint.port.input.WalletResponseTO
Expand Down Expand Up @@ -62,9 +62,9 @@ object VerifierApiClient {
* - (request) mdocVerification application Internet frontend to Internet Web Service, flow "18 HTTPs POST to response_uri [section B.3.2.2]
* - (response) Internet Web Service to mdocVerification application Internet frontend, flow "20 return status and conditionally return data"
*/
fun getWalletResponse(client: WebTestClient, presentationId: PresentationId, responseCode: ResponseCode? = null): WalletResponseTO? {
fun getWalletResponse(client: WebTestClient, presentationId: TransactionId, responseCode: ResponseCode? = null): WalletResponseTO? {
val walletResponseUri =
VerifierApi.WALLET_RESPONSE_PATH.replace("{presentationId}", presentationId.value) +
VerifierApi.WALLET_RESPONSE_PATH.replace("{transactionId}", presentationId.value) +
(responseCode?.let { "?response_code=${it.value}" } ?: "")

// when
Expand All @@ -85,11 +85,11 @@ object VerifierApiClient {

fun getWalletResponseNoValidation(
client: WebTestClient,
presentationId: PresentationId,
transactionId: TransactionId,
responseCode: ResponseCode? = null,
): EntityExchangeResult<WalletResponseTO> {
val walletResponseUri =
VerifierApi.WALLET_RESPONSE_PATH.replace("{presentationId}", presentationId.value) +
VerifierApi.WALLET_RESPONSE_PATH.replace("{transactionId}", transactionId.value) +
(responseCode?.let { "?response_code=${it.value}" } ?: "")

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import eu.europa.ec.eudi.verifier.endpoint.VerifierApplicationTest
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.jose.nimbusEnc
import eu.europa.ec.eudi.verifier.endpoint.adapter.out.jose.nimbusJWSAlgorithm
import eu.europa.ec.eudi.verifier.endpoint.domain.JarmOption
import eu.europa.ec.eudi.verifier.endpoint.domain.PresentationId
import eu.europa.ec.eudi.verifier.endpoint.domain.RequestId
import eu.europa.ec.eudi.verifier.endpoint.domain.TransactionId
import eu.europa.ec.eudi.verifier.endpoint.port.input.ResponseModeTO
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -135,7 +135,7 @@ internal class WalletResponseDirectPostJwtTest {
// when
val response = VerifierApiClient.getWalletResponse(
client,
PresentationId(transactionInitialized.presentationId),
TransactionId(transactionInitialized.transactionId),
)
// then
assertNotNull(response, "response is null")
Expand Down
Loading

0 comments on commit f9c7c50

Please sign in to comment.