-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialConfiguration.kt
More file actions
317 lines (277 loc) · 10 KB
/
CredentialConfiguration.kt
File metadata and controls
317 lines (277 loc) · 10 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.europa.ec.eudi.openid4vci
import com.nimbusds.jose.JWSAlgorithm
import eu.europa.ec.eudi.openid4vci.internal.LocaleSerializer
import kotlinx.serialization.SerialName
import java.io.Serializable
import java.net.URI
import java.net.URL
import java.util.*
/**
* Cryptographic Binding Methods for issued Credentials.
*/
sealed interface CryptographicBindingMethod : Serializable {
/**
* JWK format.
*/
data object JWK : CryptographicBindingMethod {
private fun readResolve(): Any = JWK
}
/**
* COSE Key object.
*/
data object COSE : CryptographicBindingMethod {
private fun readResolve(): Any = COSE
}
/**
* DID method.
*/
data class DID(val method: String) : CryptographicBindingMethod
/**
* Other format
*/
data class Other(val value: String) : CryptographicBindingMethod
}
/**
* Proof types supported by a Credential Issuer.
*/
enum class ProofType : Serializable {
JWT,
DI_VP,
ATTESTATION,
}
sealed interface ProofTypeMeta : Serializable {
data class Jwt(
val algorithms: List<JWSAlgorithm>,
override val keyAttestationRequirement: KeyAttestationRequirement,
) : ProofTypeMeta, HasKeyAttestationRequirement {
init {
require(algorithms.isNotEmpty()) { "Supported algorithms in case of JWT cannot be empty" }
}
}
data object DiVp : ProofTypeMeta {
private fun readResolve(): Any = DiVp
}
data class Attestation(
val algorithms: List<JWSAlgorithm>,
override val keyAttestationRequirement: KeyAttestationRequirement.Required,
) : ProofTypeMeta, HasKeyAttestationRequirement {
init {
require(algorithms.isNotEmpty()) { "Supported algorithms in case of Attestation cannot be empty" }
}
}
data class Unsupported(val type: String) : ProofTypeMeta
}
interface HasKeyAttestationRequirement {
val keyAttestationRequirement: KeyAttestationRequirement
}
sealed interface KeyAttestationRequirement {
data object NotRequired : KeyAttestationRequirement {
private fun readResolve(): Any = NotRequired
}
data class Required(
val keyStorageConstraints: List<String>?,
val userAuthenticationConstraints: List<String>?,
) : KeyAttestationRequirement {
init {
if (keyStorageConstraints != null) {
require(keyStorageConstraints.isNotEmpty()) {
"Key storage constraints, if provided, should be non-empty"
}
}
if (userAuthenticationConstraints != null) {
require(userAuthenticationConstraints.isNotEmpty()) {
"User authentication constraints, if provided, should be non-empty"
}
}
}
val hasConstrains: Boolean
get() = keyStorageConstraints != null || userAuthenticationConstraints != null
companion object
}
companion object {
val RequiredNoConstraints: Required = Required(null, null)
}
}
fun ProofTypeMeta.type(): ProofType? = when (this) {
is ProofTypeMeta.Jwt -> ProofType.JWT
is ProofTypeMeta.DiVp -> ProofType.DI_VP
is ProofTypeMeta.Attestation -> ProofType.ATTESTATION
is ProofTypeMeta.Unsupported -> null
}
fun ProofTypeMeta.algorithms(): List<JWSAlgorithm> = when (this) {
is ProofTypeMeta.Jwt -> algorithms
is ProofTypeMeta.Attestation -> algorithms
is ProofTypeMeta.DiVp -> emptyList()
is ProofTypeMeta.Unsupported -> emptyList()
}
@JvmInline
value class ProofTypesSupported private constructor(val values: Set<ProofTypeMeta>) {
operator fun get(type: ProofType): ProofTypeMeta? = values.firstOrNull { it.type() == type }
companion object {
val Empty: ProofTypesSupported = ProofTypesSupported(emptySet())
operator fun invoke(values: Set<ProofTypeMeta>): ProofTypesSupported {
require(values.groupBy(ProofTypeMeta::type).all { (_, instances) -> instances.size == 1 }) {
"Multiple instance of the same proof type are not allowed"
}
return ProofTypesSupported(values)
}
}
}
typealias CssColor = String
/**
* Display properties of a supported credential type for a certain language.
*/
data class Display(
val name: String,
val locale: Locale? = null,
val logo: Logo? = null,
val description: String? = null,
val backgroundColor: CssColor? = null,
val backgroundImage: URI? = null,
val textColor: CssColor? = null,
) : Serializable {
/**
* Logo information.
*/
data class Logo(
val uri: URI? = null,
val alternativeText: String? = null,
) : Serializable
}
data class CredentialMetadata(
val display: List<Display>? = emptyList(),
val claims: List<Claim>? = emptyList(),
)
/**
* Credentials supported by an Issuer.
*/
sealed interface CredentialConfiguration : Serializable {
val scope: String?
val cryptographicBindingMethodsSupported: List<CryptographicBindingMethod>
val proofTypesSupported: ProofTypesSupported
val credentialMetadata: CredentialMetadata?
}
/**
* The details of a Claim.
*/
@kotlinx.serialization.Serializable
data class Claim(
@SerialName("path") val path: ClaimPath,
@SerialName("mandatory") val mandatory: Boolean? = false,
@SerialName("display") val display: List<Display> = emptyList(),
) : Serializable {
/**
* Display properties of a Claim.
*/
@kotlinx.serialization.Serializable
data class Display(
@SerialName("name") val name: String? = null,
@kotlinx.serialization.Serializable(LocaleSerializer::class)
@SerialName("locale") val locale: Locale? = null,
) : Serializable
}
/**
* COSE Algorithm value.
*
* @see <a href="https://www.iana.org/assignments/cose/cose.xhtml">CBOR Object Signing and Encryption (COSE)</a>
*/
@JvmInline
value class CoseAlgorithm(val value: Int) : Serializable {
override fun toString(): String = value.toString()
}
/**
* The data of a Verifiable Credentials issued as an ISO MDOC.
*/
data class MsoMdocCredential(
override val scope: String? = null,
override val cryptographicBindingMethodsSupported: List<CryptographicBindingMethod> = emptyList(),
val credentialSigningAlgorithmsSupported: List<CoseAlgorithm> = emptyList(),
override val proofTypesSupported: ProofTypesSupported = ProofTypesSupported.Empty,
override val credentialMetadata: CredentialMetadata?,
val docType: String,
) : CredentialConfiguration
/**
* JWS Algorithm Name
*
* @see <a href="https://www.iana.org/assignments/jose/jose.xhtml">JSON Object Signing and Encryption (JOSE)</a>
*/
@JvmInline
value class JwsAlgorithm(val name: String) : Serializable {
override fun toString(): String = name
}
/**
* The data of a Verifiable Credentials issued as an SD-JWT VC.
*/
data class SdJwtVcCredential(
override val scope: String? = null,
override val cryptographicBindingMethodsSupported: List<CryptographicBindingMethod> = emptyList(),
val credentialSigningAlgorithmsSupported: List<JwsAlgorithm> = emptyList(),
override val proofTypesSupported: ProofTypesSupported = ProofTypesSupported.Empty,
override val credentialMetadata: CredentialMetadata?,
val type: String,
) : CredentialConfiguration
data class W3CJsonLdCredentialDefinition(
val context: List<URL>,
val type: List<String>,
)
/**
* Linked Data Algorithm Identifier
*
* @see <a href="https://w3c-ccg.github.io/ld-cryptosuite-registry/">Linked Data Cryptographic Suite Registry</a>
*/
@JvmInline
value class LinkedDataAlgorithm(val identifier: String) : Serializable {
override fun toString(): String = identifier
}
/**
* The data of a W3C Verifiable Credential issued using Data Integrity and JSON-LD.
*/
data class W3CJsonLdDataIntegrityCredential(
override val scope: String? = null,
override val cryptographicBindingMethodsSupported: List<CryptographicBindingMethod> = emptyList(),
val credentialSigningAlgorithmsSupported: List<LinkedDataAlgorithm> = emptyList(),
override val proofTypesSupported: ProofTypesSupported = ProofTypesSupported.Empty,
override val credentialMetadata: CredentialMetadata?,
val credentialDefinition: W3CJsonLdCredentialDefinition,
) : CredentialConfiguration
/**
* The data of a W3C Verifiable Credential issued as a signed JWT using JSON-LD.
*/
data class W3CJsonLdSignedJwtCredential(
override val scope: String? = null,
override val cryptographicBindingMethodsSupported: List<CryptographicBindingMethod> = emptyList(),
val credentialSigningAlgorithmsSupported: List<LinkedDataAlgorithm> = emptyList(),
override val proofTypesSupported: ProofTypesSupported = ProofTypesSupported.Empty,
override val credentialMetadata: CredentialMetadata?,
val credentialDefinition: W3CJsonLdCredentialDefinition,
) : CredentialConfiguration
/**
* The data of a W3C Verifiable Credential issued as a signed JWT, not using JSON-LD.
*/
data class W3CSignedJwtCredential(
override val scope: String? = null,
override val cryptographicBindingMethodsSupported: List<CryptographicBindingMethod> = emptyList(),
val credentialSigningAlgorithmsSupported: List<JwsAlgorithm> = emptyList(),
override val proofTypesSupported: ProofTypesSupported = ProofTypesSupported.Empty,
override val credentialMetadata: CredentialMetadata?,
val credentialDefinition: CredentialDefinition,
) : CredentialConfiguration {
data class CredentialDefinition(
val type: List<String>,
)
}