Skip to content

Commit

Permalink
Modification of data structure to comply with ID1.
Browse files Browse the repository at this point in the history
  • Loading branch information
moratori committed Jan 14, 2025
1 parent 1cf5f72 commit 0b95ea7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ open class CredentialDataStore(
credentialDataStore.data

fun responseToSchema(
format: String,
credentialResponse: CredentialResponse,
credentialBasicInfo: Map<String, Any>,
credentialIssuerMetadata: String,
Expand All @@ -62,7 +63,7 @@ open class CredentialDataStore(
builder.setCNonce(it)
}

builder.setId(UUID.randomUUID().toString()).setFormat(credentialResponse.format)
builder.setId(UUID.randomUUID().toString()).setFormat(format)
.setCredential(credentialResponse.credential)
.setIss(credentialBasicInfo["iss"] as String).setIat(credentialBasicInfo["iat"] as Long)
.setExp(credentialBasicInfo["exp"] as Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import com.ownd_project.tw2023_wallet_android.ui.shared.Constants
import com.ownd_project.tw2023_wallet_android.utils.CredentialRequest
import com.ownd_project.tw2023_wallet_android.utils.CredentialRequestJwtVc
import com.ownd_project.tw2023_wallet_android.utils.CredentialRequestSdJwtVc
import com.ownd_project.tw2023_wallet_android.utils.InvalidCredentialResponseException
import com.ownd_project.tw2023_wallet_android.utils.KeyPairUtil
import com.ownd_project.tw2023_wallet_android.utils.Proof
import com.ownd_project.tw2023_wallet_android.utils.TokenErrorResponseException
import com.ownd_project.tw2023_wallet_android.utils.UnsupportedIssuanceFlow
import com.ownd_project.tw2023_wallet_android.utils.VCIClient
import com.ownd_project.tw2023_wallet_android.vci.CredentialIssuerMetadata
import com.ownd_project.tw2023_wallet_android.vci.CredentialOffer
Expand Down Expand Up @@ -277,16 +279,15 @@ class ConfirmationViewModel() :
if (_format.value == "vc+sd-jwt") {
credentialRequest = CredentialRequestSdJwtVc(
format = format.value!!,
credentialDefinition = mapOf("vct" to "${vct.value}"),
proof = proofJwt
vct = vct.value,
proof = proofJwt,
)
} else {
credentialRequest = CredentialRequestJwtVc(
format = format.value!!,
proof = proofJwt ,
credentialDefinition = mapOf(
"type" to listOf(vct.value),
"credentialSubject" to mapOf<String, Any>()
"type" to listOf(vct.value)
),
)
}
Expand All @@ -295,21 +296,25 @@ class ConfirmationViewModel() :
credentialEndpoint!!, credentialRequest, tokenResponse?.accessToken!!
)

val basicInfo = credentialResponse?.let {
if (credentialResponse.isDeferredIssuance() || credentialResponse.credential == null) {
throw UnsupportedIssuanceFlow("Deferred Issuance is not supported currently")
}

val basicInfo =
if (_format.value == "vc+sd-jwt")
extractSDJwtInfo(it.credential, format.value!!)
extractSDJwtInfo(credentialResponse.credential, format.value!!)
else
extractJwtVcJsonInfo(it.credential, format.value!!)
}
extractJwtVcJsonInfo(credentialResponse.credential, format.value!!)

val credentialIssuerMetadataStr =
jacksonObjectMapper().writeValueAsString(credentialIssuerMetadata.value)


// Protobufのスキーマを使用してCredentialDataを直接作成
val vcData = credentialDataStore?.responseToSchema(
credentialResponse = credentialResponse!!,
credentialBasicInfo = basicInfo!!,
format = format.value!!,
credentialResponse = credentialResponse,
credentialBasicInfo = basicInfo,
credentialIssuerMetadata = credentialIssuerMetadataStr
)

Expand All @@ -324,6 +329,11 @@ class ConfirmationViewModel() :
val res = e.errorResponse
_errorMessage.postValue("${res.error}, ${res.errorDescription} ")
// todo 内部的なエラー情報としてerror responseを渡して、フラグメント側でstring valuesに事前に用意したエラーメッセージと対応させて表示する
} catch(e: InvalidCredentialResponseException) {
println(e)
_errorMessage.postValue("サーバーの応答を解釈できません: ${e.message}")
}catch (e: UnsupportedIssuanceFlow){
_errorMessage.postValue("サポートしていない発行フローです")
} catch (e: IOException) {
// エラー時の処理
println(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,67 @@ data class TokenResponse(
)

data class CredentialResponse(
val format: String,
val credential: String,
val credential: String? = null,
val transactionId: String? = null,
val cNonce: String? = null,
val cNonceExpiresIn: Int? = null,
)
val notificationId: String?
){
init{
if (credential == null && transactionId == null) {
throw InvalidCredentialResponseException("Either credential or transactionId must be provided")
}
}

fun isDeferredIssuance(): Boolean{
return transactionId != null
}
}

class UnsupportedIssuanceFlow(
message: String
) : Exception(message)

class InvalidCredentialResponseException(
message: String
) : Exception(message)

abstract class CredentialRequest(
open val format: String,
open val proof: Proof? = null
)

class TokenErrorResponseException(val errorResponse: TokenErrorResponse) :
Exception("Error: ${errorResponse.error}, Description: ${errorResponse.errorDescription}")

data class CredentialRequestCredentialResponseEncryption(
val alg: String,
val enc: String
// TODO: Add the JWK property with the appropriate data type
// val jwk: ...
)

abstract class CredentialRequest(
open val format: String,
open val proof: Proof? = null,
open val credentialIdentifier: String? = null,
open val credentialResponseEncryption: CredentialRequestCredentialResponseEncryption? = null
)

@JsonInclude(JsonInclude.Include.NON_NULL)
data class CredentialRequestSdJwtVc(
override @JsonProperty("format")val format: String,
override @JsonProperty("proof") val proof: Proof?,
@JsonProperty("credential_definition")
// todo: Improve the precision of type definitions.
val credentialDefinition: Map<String, String>,
override @JsonProperty("credential_identifier")val credentialIdentifier: String? = null,
override @JsonProperty("credential_response_encryption")val credentialResponseEncryption: CredentialRequestCredentialResponseEncryption? = null,

val vct: String?,
val claims: Map<String, Any>? = null
): CredentialRequest(format, proof)

@JsonInclude(JsonInclude.Include.NON_NULL)
data class CredentialRequestJwtVc(
override val format: String,
override val proof: Proof? = null,
override @JsonProperty("credential_identifier")val credentialIdentifier: String? = null,
override @JsonProperty("credential_response_encryption")val credentialResponseEncryption: CredentialRequestCredentialResponseEncryption? = null,

@JsonProperty("credential_definition")
// todo: Improve the precision of type definitions.
val credentialDefinition: Map<String, Any>,
Expand Down Expand Up @@ -105,7 +138,7 @@ class VCIClient() {

fun postCredentialRequest(
url: String, credentialRequest: CredentialRequest, accessToken: String,
): CredentialResponse? {
): CredentialResponse {

val objectMapper = jacksonObjectMapper()
objectMapper.propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE
Expand Down Expand Up @@ -133,7 +166,7 @@ class VCIClient() {
return@use credentialResponse
} catch (e: JSONException) {
println("Failed to parse JSON: $e")
return@use null
throw InvalidCredentialResponseException("Failed to parse JSON as CredentialResponse: ${e.message}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class VCIClientTest {
val credentialResponse = vciClient.postCredentialRequest(
"http://localhost:${port}/credentials", credential, "accessToken"
)
assertEquals("jwt_vc_json", credentialResponse?.format)
assertEquals("example-credential", credentialResponse?.credential)
assertEquals("example-c-nonce", credentialResponse?.cNonce)
assertEquals(86400, credentialResponse?.cNonceExpiresIn)
Expand Down
1 change: 0 additions & 1 deletion app/src/test/resources/credential_response.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"format": "jwt_vc_json",
"credential": "example-credential",
"c_nonce": "example-c-nonce",
"c_nonce_expires_in": 86400
Expand Down

0 comments on commit 0b95ea7

Please sign in to comment.