-
Notifications
You must be signed in to change notification settings - Fork 10
/
Debugging.kt
202 lines (164 loc) · 7.61 KB
/
Debugging.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.sd_jwt
import com.nimbusds.jose.JOSEObjectType
import com.nimbusds.jose.jwk.Curve
import com.nimbusds.jose.jwk.OctetKeyPair
import com.nimbusds.jose.jwk.gen.ECKeyGenerator
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.junit.jupiter.api.Test
import java.time.Instant
import java.util.*
internal class Debugging {
private val verifier = "http://verifier.example.com"
private val issuer = "http://issuer.example.com"
private val issuerKeyJson =
"""{"kty":"OKP","d":"Pp1foKt6rJAvx0igrBEfOgrT0dgMVQDHmgJZbm2h518","crv":"Ed25519","kid":"IssuerKey","x":"1NYF4EFS2Ov9hqt35fVt2J-dktLV29hs8UFjxbOXnho"}"""
private val issuerKey = OctetKeyPair.parse(issuerKeyJson)
private val issuerSigner = KeyBasedSdJwtSigner(issuerKey)
private val holderKeyJson =
"""{"kty":"OKP","d":"8G6whDz1owU1k7-TqtP3xEMasdI3t3j2AvpvXVwwrHQ","crv":"Ed25519","kid":"HolderKey","x":"s6gVLINLcCGhGEDTf_v1zMluLZcXj4GOXAfQlOWZM9Q"}"""
private val holderKey = OctetKeyPair.parse(holderKeyJson)
private val holderSigner = KeyBasedSdJwtSigner(holderKey)
private val trustedIssuers = mutableMapOf<String, String>(issuer to issuerKey.toPublicJWK().toJSONString())
private val nonce = "12345"
@Serializable
private data class Address(
@SerialName("street_address") val streetAddress: String? = null,
val locality: String? = null,
val region: String? = null,
val country: String? = null,
@SerialName("zip_code") val zipCode: Int? = null
)
@Serializable
private data class IdCredential(
val iss: String,
@SerialName("given_name") val givenName: String? = null,
@SerialName("family_name") val familyName: String? = null,
val email: String? = null,
val birthday: String? = null,
val nicknames: Set<String>? = null,
@SerialName (HIDE_NAME + "address") val address: Address? = null,
@SerialName("secret_club_membership") val secretClubMembership: String? = null
)
@Test
fun debugging() {
val claims = IdCredential(
iss = issuer,
givenName = "Alice",
familyName = "Wonderland",
email = "[email protected]",
birthday = "1950-01-01",
nicknames = setOf("A", "B"),
address = Address(
streetAddress = "123 Main St",
locality = "Anytown",
region = "Anystate",
country = "US",
zipCode = 123456
),
secretClubMembership = "SecretClub"
)
val discloseStructure = IdCredential(iss = "", address = Address())
val holderPubKey = holderKey?.toPublicJWK()
val credentialGen = createCredential(claims, issuerSigner, holderPubKey, discloseStructure)
println("====================== Issuer ======================")
println("Generated credential: $credentialGen")
val releaseClaims = IdCredential(
iss = "",
givenName = "",
email = "",
address = Address(streetAddress = "", zipCode = 0),
secretClubMembership = ""
)
val presentationGen = createPresentation(credentialGen, releaseClaims, verifier, nonce, holderSigner)
println("====================== Wallet ======================")
println("Generated presentation: $presentationGen")
val verifiedCredentialGen =
verifyPresentation<IdCredential>(presentationGen, TrustedIssuersSdJwtVerifier(trustedIssuers), nonce, verifier, true)
println("===================== Verifier =====================")
println("Verified credential: $verifiedCredentialGen\n")
}
@Serializable
private data class SimpleTestCredential(
val iss: String,
@SerialName("given_name") val givenName: String? = null,
@SerialName("family_name") val familyName: String? = null,
val email: String? = null,
val b: Boolean? = null,
val age: Int? = null
)
@Test
fun minimal() {
val claims = SimpleTestCredential(iss = issuer, "Alice", "Wonderland", "[email protected]", false, 21)
val discloseStructure = SimpleTestCredential(iss = "")
val credential = createCredential(claims, issuerSigner, discloseStructure = discloseStructure)
println("====================== Issuer ======================")
println("Credential: $credential")
val releaseClaims =
SimpleTestCredential(iss = "", givenName = "", email = "", age = 0) // Non-null claims will be revealed
val presentation = createPresentation(credential, releaseClaims)
println("====================== Wallet ======================")
println("Presentation: $presentation")
val verifiedSimpleTestCredential =
verifyPresentation<SimpleTestCredential>(presentation, TrustedIssuersSdJwtVerifier(trustedIssuers), verifyHolderBinding = false)
println("===================== Verifier =====================")
println("Verified credential: $verifiedSimpleTestCredential\n")
}
@Serializable
data class CredentialSubject(
@SerialName("given_name") val givenName: String? = null,
@SerialName("family_name") val familyName: String? = null,
val email: String? = null
)
@Serializable
data class EmailCredential(
val type: String,
val iat: Long,
val exp: Long,
val iss: String,
@SerialName(HIDE_NAME + "credentialSubject") val credentialSubject: CredentialSubject? = null
)
@Test
fun nextcloudLoginCredential() {
val issuerKey = ECKeyGenerator(Curve.P_256)
.keyID("Issuer")
.generate()
val header = SdJwtHeader(JOSEObjectType("vc+sd-jwt"), "credential-claims-set+json")
val signer = KeyBasedSdJwtSigner(issuerKey, sdJwtHeader = header)
val holderKey = ECKeyGenerator(Curve.P_256)
.keyID("Holder")
.generate()
val issuer = "did:jwk:${b64Encoder(issuerKey.toPublicJWK().toJSONString())}"
val trustedIssuers = mapOf<String, String>(issuer to issuerKey.toPublicJWK().toJSONString())
val userClaims = EmailCredential(
type = "VerifiedEMail",
iat = Date.from(Instant.now()).time / 1000,
exp = Date.from(Instant.now().plusSeconds(3600 * 48)).time / 1000,
iss = issuer,
credentialSubject = CredentialSubject(
givenName = "Alice",
familyName = "Wonderland",
email = "[email protected]"
)
)
val discloseStructure =
EmailCredential(type = "", iat = 0, exp = 0, iss = "", credentialSubject = CredentialSubject())
val credential = createCredential(userClaims, signer, holderKey.toPublicJWK(), discloseStructure)
println("Credential: $credential")
println()
val releaseClaims = EmailCredential(type = "", iat = 0, exp = 0, iss = "", credentialSubject = CredentialSubject(email = "", givenName = "", familyName = ""))
val holderSignerNextcloud = KeyBasedSdJwtSigner(holderKey)
val presentation =
createPresentation(credential, releaseClaims, "https://nextcloud.example.com", "1234", holderSignerNextcloud)
println("Presentation: $presentation")
println()
val verifiedEmailCredential = verifyPresentation<EmailCredential>(
presentation,
TrustedIssuersSdJwtVerifier(trustedIssuers),
"1234",
"https://nextcloud.example.com",
true
)
println(verifiedEmailCredential)
}
}