-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialOfferRequestResolver.kt
More file actions
305 lines (260 loc) · 10.3 KB
/
CredentialOfferRequestResolver.kt
File metadata and controls
305 lines (260 loc) · 10.3 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
/*
* 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 eu.europa.ec.eudi.openid4vci.internal.DefaultCredentialOfferRequestResolver
import eu.europa.ec.eudi.openid4vci.internal.ensure
import io.ktor.client.*
import io.ktor.http.*
import java.io.Serializable
/**
* A Credential Offer.
*/
data class CredentialOffer(
val credentialIssuerIdentifier: CredentialIssuerId,
val credentialIssuerMetadata: CredentialIssuerMetadata,
val authorizationServerMetadata: CIAuthorizationServerMetadata,
val credentialConfigurationIdentifiers: List<CredentialConfigurationIdentifier>,
val grants: Grants? = null,
) : Serializable {
init {
require(credentialConfigurationIdentifiers.isNotEmpty()) { "credentials must not be empty" }
if (grants is Grants.AuthorizationCode) {
requireNotNull(authorizationServerMetadata.authorizationEndpointURI) {
"Credential Offer requires Authorization Code Grant, but the Authorization Server does not support it"
}
}
}
}
/**
* The Id of a Credential Issuer. An [HttpsUrl] that has no fragment or query parameters.
*/
@JvmInline
value class CredentialIssuerId private constructor(val value: HttpsUrl) {
override fun toString(): String =
value.value.toString()
companion object {
/**
* Parses the provided [value] as an [HttpsUrl] and tries to create a [CredentialIssuerId].
*/
operator fun invoke(value: String): Result<CredentialIssuerId> =
HttpsUrl(value)
.mapCatching {
require(it.value.toURI().fragment.isNullOrBlank()) { "CredentialIssuerId must not have a fragment" }
require(it.value.query.isNullOrBlank()) { "CredentialIssuerId must not have query parameters " }
CredentialIssuerId(it)
}
}
}
/**
* The Grant Types a Credential Issuer can process for a Credential Offer.
*/
sealed interface Grants : Serializable {
/**
* Data for an Authorization Code Grant. [issuerState], if provided, must not be blank.
*/
data class AuthorizationCode(
val issuerState: String? = null,
val authorizationServer: HttpsUrl? = null,
) : Grants {
init {
issuerState?.let {
require(issuerState.isNotBlank()) { "issuerState cannot be blank" }
}
}
}
/**
* Data for a Pre-Authorized Code Grant. [preAuthorizedCode] must not be blank.
*/
data class PreAuthorizedCode(
val preAuthorizedCode: String,
val txCode: TxCode? = null,
val authorizationServer: HttpsUrl? = null,
) : Grants {
init {
require(preAuthorizedCode.isNotBlank()) { "preAuthorizedCode cannot be blank" }
}
}
/**
* Data for either an Authorization Code Grant or a Pre-Authorized Code Grant.
*/
data class Both(
val authorizationCode: AuthorizationCode,
val preAuthorizedCode: PreAuthorizedCode,
) : Grants
fun authorizationCode(): AuthorizationCode? = when (this) {
is PreAuthorizedCode -> null
is Both -> authorizationCode
is AuthorizationCode -> this
}
fun preAuthorizedCode(): PreAuthorizedCode? = when (this) {
is PreAuthorizedCode -> this
is Both -> preAuthorizedCode
is AuthorizationCode -> null
}
}
data class TxCode(
val inputMode: TxCodeInputMode = TxCodeInputMode.NUMERIC,
val length: Int? = null,
val description: String? = null,
) {
init {
description?.let {
ensure(it.length <= DescriptionMaxSize) {
val er = IllegalArgumentException("Transaction code description over $DescriptionMaxSize characters")
CredentialOfferRequestValidationError.InvalidCredentials(er).toException()
}
}
}
companion object {
private const val DescriptionMaxSize = 300
}
}
enum class TxCodeInputMode {
NUMERIC, TEXT
}
/**
* Credential Offer request.
*/
sealed interface CredentialOfferRequest : Serializable {
/**
* A Credential Offer request that was passed using the 'credential_offer' query parameter.
*/
@JvmInline
value class PassByValue(val value: String) : CredentialOfferRequest
/**
* A Credential Offer request that must be resolved using the 'credential_offer_uri' parameter.
*/
@JvmInline
value class PassByReference(val value: HttpsUrl) : CredentialOfferRequest
companion object {
/**
* Parses a URL to a [CredentialOfferRequest].
*
* In case of [Result.Failure] a [CredentialOfferRequestException] is thrown.
*/
operator fun invoke(url: String): Result<CredentialOfferRequest> = runCatching {
val builder = runCatching {
URLBuilder(url)
}.getOrElse { CredentialOfferRequestError.NonParsableCredentialOfferEndpointUrl(it).raise() }
val parameters = builder.parameters
val maybeByValue = parameters["credential_offer"]
val maybeByReference = parameters["credential_offer_uri"]
when {
!maybeByValue.isNullOrBlank() && !maybeByReference.isNullOrBlank() ->
CredentialOfferRequestValidationError.OneOfCredentialOfferOrCredentialOfferUri.raise()
!maybeByValue.isNullOrBlank() -> PassByValue(maybeByValue)
!maybeByReference.isNullOrBlank() -> HttpsUrl(maybeByReference)
.map { PassByReference(it) }
.getOrElse { CredentialOfferRequestValidationError.InvalidCredentialOfferUri(it).raise() }
else -> CredentialOfferRequestValidationError.OneOfCredentialOfferOrCredentialOfferUri.raise()
}
}
}
}
/**
* Errors that can occur while trying to validate and resolve a [CredentialOfferRequest].
*/
sealed interface CredentialOfferRequestError : Serializable {
/**
* The Credential Offer Endpoint URL could not be parsed.
*/
data class NonParsableCredentialOfferEndpointUrl(val reason: Throwable) : CredentialOfferRequestError
/**
* The Credential Offer object could not be fetched.
*/
data class UnableToFetchCredentialOffer(val reason: Throwable) : CredentialOfferRequestError
/**
* The Credential Offer object could not be parsed.
*/
data class NonParseableCredentialOffer(val reason: Throwable) : CredentialOfferRequestError
/**
* The metadata of the Credential Issuer could not be resolved.
*/
data class UnableToResolveCredentialIssuerMetadata(val reason: Throwable) : CredentialOfferRequestError
/**
* The metadata of the Authorization Server could not be resolved.
*/
data class UnableToResolveAuthorizationServerMetadata(val reason: Throwable) : CredentialOfferRequestError
/**
* Wraps this [CredentialOfferRequestError] to a [CredentialOfferRequestException].
*/
fun toException(): CredentialOfferRequestException = CredentialOfferRequestException(this)
/**
* Wraps this [CredentialOfferRequestError] to a [CredentialOfferRequestException] and throws it.
*/
fun raise(): Nothing = throw toException()
}
/**
* Validation error that can occur while trying to validate a [CredentialOfferRequest].
*/
sealed interface CredentialOfferRequestValidationError : CredentialOfferRequestError {
/**
* The Credential Offer Endpoint URL either contained neither the 'credential_offer' nor the 'credential_offer_uri'
* parameter or contained both of them.
*/
data object OneOfCredentialOfferOrCredentialOfferUri : CredentialOfferRequestValidationError {
@Suppress("unused")
private fun readResolve(): Any = OneOfCredentialOfferOrCredentialOfferUri
}
/**
* The 'credentials_offer_uri' parameter contained in the Credential Offer Endpoint URL was not a valid [HttpsUrl].
*/
data class InvalidCredentialOfferUri(val reason: Throwable) : CredentialOfferRequestValidationError
/**
* The Id of the Credential Issuer is not valid.
*/
data class InvalidCredentialIssuerId(val reason: Throwable) : CredentialOfferRequestValidationError
/**
* The Credentials of a Credential Offer are not valid.
*/
data class InvalidCredentials(val reason: Throwable) : CredentialOfferRequestValidationError
/**
* The Grants of a Credential Offer are not valid.
*/
data class InvalidGrants(val reason: Throwable) : CredentialOfferRequestValidationError
}
/**
* An exception indicating a [CredentialOfferRequestError] occurred while trying to validate or resolve a [CredentialOfferRequest].
*/
data class CredentialOfferRequestException(val error: CredentialOfferRequestError) : Exception()
/**
* Service for parsing, extracting and validating a [CredentialOfferRequest].
*/
fun interface CredentialOfferRequestResolver {
/**
* Tries to parse a Credential Offer Endpoint [URL][uri], extract and validate a Credential Offer Request.
*/
suspend fun resolve(uri: String): Result<CredentialOffer> = runCatchingCancellable {
val request = CredentialOfferRequest(uri).getOrThrow()
resolve(request).getOrThrow()
}
/**
* Tries to validate and resolve a [Credential Offer Request][request].
*/
suspend fun resolve(request: CredentialOfferRequest): Result<CredentialOffer>
companion object {
/**
* Creates a new [CredentialOfferRequestResolver].
*/
operator fun invoke(
httpClient: HttpClient,
issuerMetadataPolicy: IssuerMetadataPolicy,
): CredentialOfferRequestResolver = CredentialOfferRequestResolver { request ->
val resolver = DefaultCredentialOfferRequestResolver(httpClient, issuerMetadataPolicy)
resolver.resolve(request)
}
}
}