-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ryosuke-wakaba/bugfix/include-jwt-vc-in-jw…
…t-vp Include credentials in `jwt_vc_json` format within `jwt_vp_json` VP i…
- Loading branch information
Showing
11 changed files
with
352 additions
and
115 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
app/src/androidTest/java/com/ownd_project/tw2023_wallet_android/JwtVpJsonGeneratorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.ownd_project.tw2023_wallet_android | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.ownd_project.tw2023_wallet_android.oid.HeaderOptions | ||
import com.ownd_project.tw2023_wallet_android.oid.JwtVpJsonPayloadOptions | ||
import com.ownd_project.tw2023_wallet_android.ui.shared.JwtVpJsonGeneratorImpl | ||
import com.ownd_project.tw2023_wallet_android.utils.KeyPairUtil | ||
import junit.framework.TestCase | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class JwtVpJsonGeneratorTest { | ||
|
||
private lateinit var jwtVpJsonGenerator: JwtVpJsonGeneratorImpl | ||
private val keyAlias = "testKeyAlias" | ||
|
||
@Before | ||
fun setUp() { | ||
jwtVpJsonGenerator = JwtVpJsonGeneratorImpl(keyAlias) | ||
if (!KeyPairUtil.isKeyPairExist(keyAlias)) { | ||
KeyPairUtil.generateSignVerifyKeyPair(keyAlias) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGenerateJwt() { | ||
val vcJwt = "testVcJwt" | ||
val headerOptions = HeaderOptions() | ||
val payloadOptions = JwtVpJsonPayloadOptions( | ||
iss = "issuer", | ||
jti = "testJti", | ||
aud = "testAud", | ||
nonce = "testNonce" | ||
) | ||
|
||
val vpToken = jwtVpJsonGenerator.generateJwt(vcJwt, headerOptions, payloadOptions) | ||
|
||
assert(!vpToken.isEmpty()) | ||
|
||
val decodedJwt = KeyPairUtil.decodeJwt(vpToken) | ||
val header = decodedJwt.first | ||
|
||
val jwk = header.get("jwk") as Map<String, String> | ||
val result = KeyPairUtil.verifyJwt(jwk, vpToken) | ||
TestCase.assertTrue(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
app/src/main/java/com/ownd_project/tw2023_wallet_android/oid/Presentation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.ownd_project.tw2023_wallet_android.oid | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
data class VpJwtPayload( | ||
val iss: String?, | ||
val jti: String?, | ||
val aud: String?, | ||
val nbf: Long?, | ||
val iat: Long?, | ||
val exp: Long?, | ||
val nonce: String?, | ||
val vp: Map<String, Any> | ||
) | ||
// https://www.rfc-editor.org/rfc/rfc7515.html | ||
data class HeaderOptions( | ||
val alg: String = "ES256", | ||
val typ: String = "JWT", | ||
val jwk: String? = null | ||
) | ||
|
||
data class JwtVpJsonPayloadOptions( | ||
val iss: String? = null, | ||
val jti: String? = null, | ||
val aud: String, | ||
val nbf: Long? = null, | ||
val iat: Long? = null, | ||
val exp: Long? = null, | ||
val nonce: String | ||
) | ||
|
||
object JwtVpJsonPresentation { | ||
fun genDescriptorMap( | ||
inputDescriptorId: String, | ||
pathIndex: Int? = -1, | ||
pathNestedIndex: Int? = 0 | ||
): DescriptorMap { | ||
|
||
/* | ||
a non-normative example of the content of the presentation_submission parameter: | ||
``` | ||
{ | ||
"definition_id": "example_jwt_vc", | ||
"id": "example_jwt_vc_presentation_submission", | ||
"descriptor_map": [ | ||
{ | ||
"id": "id_credential", | ||
"path": "$", | ||
"format": "jwt_vp_json", | ||
"path_nested": { | ||
"path": "$.vp.verifiableCredential[0]", | ||
"format": "jwt_vc_json" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
*/ | ||
return DescriptorMap( | ||
id = inputDescriptorId, | ||
path = if (pathIndex == -1) "$" else "$[${pathIndex}]", | ||
format = "jwt_vp_json", | ||
pathNested = Path( | ||
format = "jwt_vc_json", | ||
path = "$.vp.verifiableCredential[${pathNestedIndex}]" | ||
) | ||
) | ||
} | ||
} | ||
|
||
// https://openid.net/specs/openid-4-verifiable-presentations-1_0-20.html#name-presentation-response | ||
interface JwtVpJsonGenerator { | ||
/* | ||
a non-normative example of the payload of the Verifiable Presentation in the vp_token parameter | ||
``` | ||
{ | ||
"iss": "did:example:ebfeb1f712ebc6f1c276e12ec21", | ||
"jti": "urn:uuid:3978344f-8596-4c3a-a978-8fcaba3903c5", | ||
"aud": "https://client.example.org/cb", | ||
"nbf": 1541493724, | ||
"iat": 1541493724, | ||
"exp": 1573029723, | ||
"nonce": "n-0S6_WzA2Mj", | ||
"vp": { | ||
"@context": [ | ||
"https://www.w3.org/2018/credentials/v1" | ||
], | ||
"type": [ | ||
"VerifiablePresentation" | ||
], | ||
"verifiableCredential": [ | ||
"eyJhb...ssw5c" | ||
] | ||
} | ||
} | ||
``` | ||
Note: The VP's nonce claim contains the value of the nonce of the presentation request and the aud claim contains the Client Identifier of the Verifier. | ||
This allows the Verifier to detect replay of a Presentation as recommended in Section 12.1. | ||
*/ | ||
fun generateJwt( | ||
vcJwt: String, | ||
headerOptions: HeaderOptions, | ||
payloadOptions: JwtVpJsonPayloadOptions | ||
): String | ||
|
||
fun getJwk(): Map<String, String> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.