Skip to content

Commit

Permalink
encode/decode DeferredState
Browse files Browse the repository at this point in the history
  • Loading branch information
vkanellopoulos committed Jun 26, 2024
1 parent d18aa55 commit 0f6b949
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import eu.europa.ec.eudi.openid4vci.CredentialIssuerMetadata
import eu.europa.ec.eudi.openid4vci.IssuedCredential
import eu.europa.ec.eudi.openid4vci.Issuer
import eu.europa.ec.eudi.wallet.transfer.openid4vp.ClientId
import java.io.*
import java.net.URI

internal data class DeferredState(
Expand All @@ -29,7 +30,7 @@ internal data class DeferredState(
val credentialIssuerMetadata: CredentialIssuerMetadata,
val authorizedRequest: AuthorizedRequest,
val deferredCredential: IssuedCredential.Deferred? = null,
) {
) : Serializable {

val version = VERSION

Expand All @@ -42,15 +43,26 @@ internal data class DeferredState(
)

fun encode(): ByteArray {
return byteArrayOf()
return ByteArrayOutputStream().use { baos ->
ObjectOutputStream(baos).use { oos ->
oos.writeObject(this)
oos.flush()
baos.toByteArray()
}
}
}


companion object {
private val VERSION = 1L
const val VERSION = 1L
fun decode(encoded: ByteArray): DeferredState {
TODO()
return ByteArrayInputStream(encoded).use { bais ->
ObjectInputStream(bais).use { ois ->
val obj = ois.readObject()
if (obj is DeferredState) obj
else throw IllegalArgumentException("Invalid object type")
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,95 @@

package eu.europa.ec.eudi.wallet.issue.openid4vci

import com.nimbusds.jose.JWSAlgorithm
import eu.europa.ec.eudi.openid4vci.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.net.URI
import java.time.Instant
import java.util.*

class DeferredStateTest {

@Test
fun `test encode and decode methods`() {
val deferredCredential = IssuedCredential.Deferred(
transactionId = TransactionId("transactionId"),
)
val credentialIdentifiers = mapOf(
CredentialConfigurationIdentifier("credentialConfigurationId") to listOf(
CredentialIdentifier("credentialId")
)
)
val authorizedRequest = AuthorizedRequest.ProofRequired(
accessToken = AccessToken(accessToken = "accessToken", expiresInSec = 3600, useDPoP = false),
refreshToken = RefreshToken("refreshToken", expiresInSec = 3600),
cNonce = CNonce(value = "cNonce"),
credentialIdentifiers = credentialIdentifiers,
timestamp = Instant.now()
)

val credentialIssuerMetadata = CredentialIssuerMetadata(
credentialIssuerIdentifier = CredentialIssuerId("https://localhost:8080").getOrThrow(),
authorizationServers = listOf(HttpsUrl("https://localhost:8080").getOrThrow()),
credentialEndpoint = CredentialIssuerEndpoint("https://localhost:8080").getOrThrow(),
batchCredentialEndpoint = CredentialIssuerEndpoint("https://localhost:8080").getOrThrow(),
deferredCredentialEndpoint = CredentialIssuerEndpoint("https://localhost:8080").getOrThrow(),
notificationEndpoint = CredentialIssuerEndpoint("https://localhost:8080").getOrThrow(),
credentialResponseEncryption = CredentialResponseEncryption.NotSupported,
credentialIdentifiersSupported = true,
credentialConfigurationsSupported = mapOf(
CredentialConfigurationIdentifier("credentialConfigurationId") to MsoMdocCredential(
credentialSigningAlgorithmsSupported = listOf("alg"),
isoCredentialSigningAlgorithmsSupported = listOf(CoseAlgorithm.ES256),
isoCredentialCurvesSupported = listOf(CoseCurve.P_256),
proofTypesSupported = ProofTypesSupported(
setOf(
ProofTypeMeta.Jwt(listOf(JWSAlgorithm.ES256)),
ProofTypeMeta.Cwt(listOf(CoseAlgorithm.ES256), listOf(CoseCurve.P_256))
)
),
display = listOf(
Display(
name = "name",
locale = Locale.US,
logo = Display.Logo(URI("http://localhost:8080"), "image/png"),
textColor = "#444444"
)
),
claims = mapOf(
"namespace" to mapOf(
"claim" to Claim(
true, "claim", listOf(
Claim.Display("name", Locale.US)
)
)
)
),
order = listOf("claim"),
docType = "docType",
isoPolicy = MsoMdocPolicy(true, 10),
scope = "scope",
cryptographicBindingMethodsSupported = listOf(CryptographicBindingMethod.JWK),
)
),
display = listOf(
CredentialIssuerMetadata.Display(name = "name", locale = "el_GR")
),
)
val state = DeferredState(
clientId = "clientId",
tokenEndpoint = URI("http://localhost:8080"),
credentialIssuerMetadata = credentialIssuerMetadata,
authorizedRequest = authorizedRequest,
deferredCredential = deferredCredential
)

val encoded = state.encode()

val decoded = DeferredState.decode(encoded)

assertEquals(state, decoded)

}
}

0 comments on commit 0f6b949

Please sign in to comment.