-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialIssuerMetadataResolver.kt
More file actions
365 lines (312 loc) · 13.9 KB
/
CredentialIssuerMetadataResolver.kt
File metadata and controls
365 lines (312 loc) · 13.9 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (c) 2023-2026 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.CompressionAlgorithm
import com.nimbusds.jose.EncryptionMethod
import com.nimbusds.jose.JWEAlgorithm
import com.nimbusds.jose.jwk.JWKSet
import com.nimbusds.jose.jwk.KeyType
import com.nimbusds.jose.jwk.KeyUse
import eu.europa.ec.eudi.openid4vci.internal.DefaultCredentialIssuerMetadataResolver
import eu.europa.ec.eudi.openid4vci.internal.ensure
import io.ktor.client.*
import java.io.Serializable
import java.net.URL
sealed interface CredentialRequestEncryption : Serializable {
data object NotSupported : CredentialRequestEncryption {
@Suppress("unused")
private fun readResolve(): Any = NotSupported
}
data class SupportedNotRequired(
val encryptionParameters: SupportedRequestEncryptionParameters,
) : CredentialRequestEncryption
data class Required(
val encryptionParameters: SupportedRequestEncryptionParameters,
) : CredentialRequestEncryption
}
sealed interface CredentialResponseEncryption : Serializable {
data object NotSupported : CredentialResponseEncryption {
@Suppress("unused")
private fun readResolve(): Any = NotSupported
}
data class SupportedNotRequired(
val encryptionParameters: SupportedResponseEncryptionParameters,
) : CredentialResponseEncryption
data class Required(
val encryptionParameters: SupportedResponseEncryptionParameters,
) : CredentialResponseEncryption
}
sealed interface PayloadCompression : Serializable {
data object NotSupported : PayloadCompression {
@Suppress("unused")
private fun readResolve(): Any = NotSupported
}
data class Supported(
val algorithms: List<CompressionAlgorithm>,
) : PayloadCompression {
init {
require(algorithms.isNotEmpty()) { "Compression algorithms must be specified" }
}
}
companion object {
operator fun invoke(algorithms: List<CompressionAlgorithm>?) = when {
algorithms != null && algorithms.isNotEmpty() -> Supported(algorithms)
else -> NotSupported
}
}
}
data class SupportedResponseEncryptionParameters(
val algorithms: List<JWEAlgorithm>,
val encryptionMethods: List<EncryptionMethod>,
val payloadCompression: PayloadCompression = PayloadCompression.NotSupported,
) {
init {
require(encryptionMethods.isNotEmpty()) { "encryptionMethods cannot be empty" }
if (algorithms.isEmpty()) {
throw CredentialIssuerMetadataValidationError.CredentialResponseEncryptionAlgorithmsRequired()
}
val allAreAsymmetricAlgorithms = algorithms.all {
JWEAlgorithm.Family.ASYMMETRIC.contains(it)
}
if (!allAreAsymmetricAlgorithms) {
throw CredentialIssuerMetadataValidationError.CredentialResponseAsymmetricEncryptionAlgorithmsRequired()
}
}
}
data class SupportedRequestEncryptionParameters(
val encryptionKeys: JWKSet,
val encryptionMethods: List<EncryptionMethod>,
val payloadCompression: PayloadCompression = PayloadCompression.NotSupported,
) {
init {
require(encryptionMethods.isNotEmpty()) { "encryptionMethods cannot be empty" }
if (encryptionKeys.isEmpty()) {
throw CredentialIssuerMetadataValidationError.CredentialRequestEncryptionKeysRequired()
}
encryptionKeys.keys.forEach {
ensure(it.keyID != null) {
CredentialIssuerMetadataValidationError.CredentialRequestEncryptionKeysMustHaveKeyId()
}
ensure(it.algorithm != null) {
CredentialIssuerMetadataValidationError.CredentialRequestEncryptionKeysMustHaveAlgorithm()
}
ensure(JWEAlgorithm.Family.ASYMMETRIC.contains(it.algorithm)) {
CredentialIssuerMetadataValidationError.CredentialRequestEncryptionKeysMustHaveAsymmetricAlgorithm()
}
ensure(!it.isPrivate) {
CredentialIssuerMetadataValidationError.CredentialRequestEncryptionKeysMustBePublic()
}
ensure(it.keyType == KeyType.forAlgorithm(it.algorithm)) {
CredentialIssuerMetadataValidationError.CredentialRequestEncryptionKeyWrongKeyType()
}
ensure(it.keyUse == KeyUse.ENCRYPTION) {
CredentialIssuerMetadataValidationError.CredentialRequestEncryptionKeysMustHaveEncryptionUsage()
}
}
}
}
sealed interface BatchCredentialIssuance : Serializable {
data object NotSupported : BatchCredentialIssuance {
@Suppress("unused")
private fun readResolve(): Any = NotSupported
}
data class Supported(val batchSize: Int) : BatchCredentialIssuance {
init {
require(batchSize > 0) { "batchSize must be greater than 0" }
}
}
}
/**
* The metadata of a Credential Issuer.
*/
data class CredentialIssuerMetadata(
val credentialIssuerIdentifier: CredentialIssuerId,
val authorizationServers: List<HttpsUrl> = listOf(credentialIssuerIdentifier.value),
val credentialEndpoint: CredentialIssuerEndpoint,
val nonceEndpoint: CredentialIssuerEndpoint? = null,
val deferredCredentialEndpoint: CredentialIssuerEndpoint? = null,
val notificationEndpoint: CredentialIssuerEndpoint? = null,
val credentialRequestEncryption: CredentialRequestEncryption = CredentialRequestEncryption.NotSupported,
val credentialResponseEncryption: CredentialResponseEncryption = CredentialResponseEncryption.NotSupported,
val batchCredentialIssuance: BatchCredentialIssuance = BatchCredentialIssuance.NotSupported,
val credentialConfigurationsSupported: Map<CredentialConfigurationIdentifier, CredentialConfiguration>,
val display: List<Display> = emptyList(),
) : Serializable {
init {
require(credentialConfigurationsSupported.isNotEmpty()) { "credentialConfigurationsSupported must not be empty" }
}
inline fun <reified T : CredentialConfiguration> findByFormat(predicate: (T) -> Boolean): Map<CredentialConfigurationIdentifier, T> {
return credentialConfigurationsSupported.mapNotNull { (k, v) -> if (v is T && predicate(v)) k to v else null }
.toMap()
}
}
@Suppress("unused")
fun CredentialIssuerMetadata.findMsoMdoc(docType: String): MsoMdocCredential? =
findByFormat<MsoMdocCredential> { it.docType == docType }.values.firstOrNull()
/**
* An endpoint of a Credential Issuer. It's an [HttpsUrl] that must not have a fragment.
*/
@JvmInline
value class CredentialIssuerEndpoint(val value: URL) {
init {
require(value.toURI().fragment.isNullOrBlank()) { "CredentialIssuerEndpoint must not have a fragment" }
}
override fun toString(): String = value.toString()
companion object {
/**
* Parses the provided [value] as an [HttpsUrl] and tries to create a [CredentialIssuerEndpoint].
*/
operator fun invoke(value: String): Result<CredentialIssuerEndpoint> =
HttpsUrl(value).mapCatching { CredentialIssuerEndpoint(it.value) }
}
}
/**
* Errors that can occur while trying to fetch and validate the metadata of a Credential Issuer.
*/
sealed class CredentialIssuerMetadataError(cause: Throwable) : Throwable(cause), Serializable {
/**
* Indicates the Credential Issuer metadata could not be fetched.
*/
class UnableToFetchCredentialIssuerMetadata(cause: Throwable) : CredentialIssuerMetadataError(cause)
/**
* Indicates the Credential Issuer metadata could not be parsed.
*/
class NonParseableCredentialIssuerMetadata(cause: Throwable) : CredentialIssuerMetadataError(cause)
/**
* Indicates the Credential Issuer does not provide signed metadata.
*/
class MissingSignedMetadata() : CredentialIssuerMetadataError(IllegalArgumentException("missing signed_metadata"))
/**
* Indicates the signed metadata of the Credential Issuer are not valid.
*/
class InvalidSignedMetadata(cause: Throwable) : CredentialIssuerMetadataError(cause)
}
/**
* Errors that can occur while trying to validate the metadata of a Credential Issuer.
*/
sealed class CredentialIssuerMetadataValidationError(cause: Throwable) : CredentialIssuerMetadataError(cause) {
/**
* The Id of the Credential Issuer is not valid.
*/
class InvalidCredentialIssuerId(cause: Throwable) : CredentialIssuerMetadataValidationError(cause)
/**
* The URL of the Authorization Server is not valid.
*/
class InvalidAuthorizationServer(cause: Throwable) : CredentialIssuerMetadataValidationError(cause)
/**
* The URL of the Credential Endpoint is not valid.
*/
class InvalidCredentialEndpoint(cause: Throwable) : CredentialIssuerMetadataValidationError(cause)
/**
* The URL of the Nonce Endpoint is not valid.
*/
class InvalidNonceEndpoint(cause: Throwable) : CredentialIssuerMetadataValidationError(cause)
/**
* The URL of the Deferred Credential Endpoint is not valid.
*/
class InvalidDeferredCredentialEndpoint(cause: Throwable) : CredentialIssuerMetadataValidationError(cause)
/**
* The URL of the Notification Endpoint is not valid.
*/
class InvalidNotificationEndpoint(cause: Throwable) : CredentialIssuerMetadataValidationError(cause)
/**
* Credential Encryption Algorithms are required.
*/
class CredentialResponseEncryptionAlgorithmsRequired :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Credential ResponseEncryption Algorithms Required"))
/**
* Credential Encryption Algorithms must be of asymmetric encryption family.
*/
class CredentialResponseAsymmetricEncryptionAlgorithmsRequired :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Asymmetric ResponseEncryption Algorithms Required"))
/**
* Credential Request Encryption keys are required.
*/
class CredentialRequestEncryptionKeysRequired :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Credential Request Encryption Keys are required"))
/**
* Credential Request Encryption Keys must have a key id.
*/
class CredentialRequestEncryptionKeysMustHaveKeyId :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Credential Request Encryption Keys must have a key id"))
/**
* Credential Request Encryption Keys must have algorithm.
*/
class CredentialRequestEncryptionKeysMustHaveAlgorithm :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Credential Request Encryption Keys must have algorithm"))
/**
* Credential Request Encryption Keys must have an asymmetric algorithm.
*/
class CredentialRequestEncryptionKeysMustHaveAsymmetricAlgorithm :
CredentialIssuerMetadataValidationError(
IllegalArgumentException("Provided encryption algorithm is not an asymmetric encryption algorithm"),
)
/**
* A Credential Request Encryption Key type don't match with algorithm.
*/
class CredentialRequestEncryptionKeyWrongKeyType :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Encryption key type and encryption algorithm do not match"))
/**
* Credential Request Encryption Keys must all be for encryption use.
*/
class CredentialRequestEncryptionKeysMustHaveEncryptionUsage :
CredentialIssuerMetadataValidationError(IllegalArgumentException("All encryption keys must have use 'enc'"))
/**
* Credential Request Encryption Keys must be public.
*/
class CredentialRequestEncryptionKeysMustBePublic :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Credential Request Encryption Keys must be public."))
/**
* The supported Credentials not valid.
*/
class InvalidCredentialsSupported(cause: Throwable) : CredentialIssuerMetadataValidationError(cause)
/**
* Supported Credentials are required.
*/
class CredentialsSupportedRequired :
CredentialIssuerMetadataValidationError(IllegalArgumentException("Credentials Supported Required"))
/**
* Credential Request Encryption must be supported by Issuer when Credential Response Encryption is supported.
*/
class CredentialRequestEncryptionMustExistIfCredentialResponseEncryptionExists :
CredentialIssuerMetadataValidationError(
IllegalArgumentException(
"Issuer must specify Credential Request Encryption if Credential Response Encryption is specified",
),
)
class InvalidBatchSize :
CredentialIssuerMetadataValidationError(IllegalArgumentException("batch_size should be greater than zero"))
}
/**
* Service for fetching, parsing, and validating the metadata of a Credential Issuer.
*/
fun interface CredentialIssuerMetadataResolver {
/**
* Tries to fetch and validate the metadata of a Credential Issuer.
*/
suspend fun resolve(issuer: CredentialIssuerId, policy: IssuerMetadataPolicy): Result<CredentialIssuerMetadata>
companion object {
/**
* Creates a new [CredentialIssuerMetadataResolver] instance.
*/
operator fun invoke(
httpClient: HttpClient,
): CredentialIssuerMetadataResolver = CredentialIssuerMetadataResolver { issuerId, policy ->
val resolver = DefaultCredentialIssuerMetadataResolver(httpClient)
resolver.resolve(issuerId, policy)
}
}
}