-
Notifications
You must be signed in to change notification settings - Fork 8
/
Issuer.kt
333 lines (301 loc) · 15.1 KB
/
Issuer.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
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
/*
* 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.oauth2.sdk.auth.ClientAuthenticationMethod
import eu.europa.ec.eudi.openid4vci.internal.*
import eu.europa.ec.eudi.openid4vci.internal.http.*
import io.ktor.client.*
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import java.net.URL
/**
* Entry point to the issuance library
*
* Provides the following capabilities
* - [AuthorizeIssuance]
* - [RequestIssuance]
* - [RequestBatchIssuance]
* - [QueryForDeferredCredential]
* - [NotifyIssuer]
*
* [Issuer] lifecycle is bound to serve a single [Issuer.credentialOffer]
*
* Typically, one of the factory methods found on the companion object can be used to get an instance of [Issuer].
*
*/
interface Issuer :
AuthorizeIssuance,
RequestIssuance,
RequestBatchIssuance,
QueryForDeferredCredential,
NotifyIssuer {
val credentialOffer: CredentialOffer
val dPoPJwtFactory: DPoPJwtFactory?
/**
* A convenient method for obtaining a [DeferredIssuanceContext], in case of a deferred issuance
* that wallet wants to query again at a later time, using [DeferredIssuer].
*
* Typically, wallet should store this [DeferredIssuanceContext] and [use it][DeferredIssuer] at a later time
*
* @receiver the state of issuance
* @param deferredCredential the transaction id returned by the issuer
* @return the context that would be needed to instantiate a [DeferredIssuer]
*/
fun AuthorizedRequest.deferredContext(deferredCredential: IssuedCredential.Deferred): DeferredIssuanceContext
companion object {
/**
* Fetches & validates the issuer's [CredentialIssuerMetadata] and list
* of [CIAuthorizationServerMetadata][OAUTH2 server(s) metadata] used by the issuer
*
* @param httpClient The client to fetch the metadata
* @param credentialIssuerId The id of the credential issuer.
*
* @return the issuer's [CredentialIssuerMetadata] and list
* of [CIAuthorizationServerMetadata][OAUTH2 server(s) metadata] used by the issuer
*/
suspend fun metaData(
httpClient: HttpClient,
credentialIssuerId: CredentialIssuerId,
): Pair<CredentialIssuerMetadata, List<CIAuthorizationServerMetadata>> = coroutineScope {
with(httpClient) {
val issuerMetadata = run {
val resolver = DefaultCredentialIssuerMetadataResolver(httpClient)
resolver.resolve(credentialIssuerId).getOrThrow()
}
val authorizationServersMetadata =
issuerMetadata.authorizationServers.distinct().map { authServerUrl ->
async {
val resolver = DefaultAuthorizationServerMetadataResolver(httpClient)
resolver.resolve(authServerUrl).getOrThrow()
}
}.awaitAll()
issuerMetadata to authorizationServersMetadata
}
}
/**
* Factory method for creating an instance of [Issuer] based on a resolved and validated credential offer.
*
* @param config wallet's configuration options
* @param credentialOffer the offer for which the issuer is being created
* @param ktorHttpClientFactory a factory for obtaining http clients, used while interacting with issuer
* @param responseEncryptionSpecFactory a factory method to generate the issuance response encryption
*
* @return if wallet's [config] can satisfy the requirements of [credentialOffer] an [Issuer] will be
* created. Otherwise, there would be a failed result
*/
fun make(
config: OpenId4VCIConfig,
credentialOffer: CredentialOffer,
ktorHttpClientFactory: KtorHttpClientFactory = DefaultHttpClientFactory,
responseEncryptionSpecFactory: ResponseEncryptionSpecFactory = DefaultResponseEncryptionSpecFactory,
): Result<Issuer> = runCatching {
config.client.ensureSupportedByAuthorizationServer(credentialOffer.authorizationServerMetadata)
val dPoPJwtFactory = config.dPoPSigner?.let { signer ->
DPoPJwtFactory.createForServer(
signer = signer,
clock = config.clock,
oauthServerMetadata = credentialOffer.authorizationServerMetadata,
).getOrThrow()
}
val authorizationEndpointClient =
AuthorizationEndpointClient(
credentialOffer.credentialIssuerIdentifier,
credentialOffer.authorizationServerMetadata,
config,
ktorHttpClientFactory,
)
val tokenEndpointClient =
TokenEndpointClient(
credentialOffer.authorizationServerMetadata,
config,
dPoPJwtFactory,
ktorHttpClientFactory,
)
val authorizeIssuance =
AuthorizeIssuanceImpl(
credentialOffer,
config,
authorizationEndpointClient,
tokenEndpointClient,
)
val responseEncryptionSpec =
responseEncryptionSpec(credentialOffer, config, responseEncryptionSpecFactory).getOrThrow()
val requestIssuance = run {
val credentialEndpointClient =
CredentialEndpointClient(
credentialOffer.credentialIssuerMetadata.credentialEndpoint,
dPoPJwtFactory,
ktorHttpClientFactory,
)
val batchEndPointClient =
credentialOffer.credentialIssuerMetadata.batchCredentialEndpoint?.let { batchEndPoint ->
BatchEndPointClient(batchEndPoint, dPoPJwtFactory, ktorHttpClientFactory)
}
RequestIssuanceImpl(
credentialOffer,
config,
credentialEndpointClient,
batchEndPointClient,
responseEncryptionSpec,
)
}
val queryForDeferredCredential =
when (val deferredEndpoint = credentialOffer.credentialIssuerMetadata.deferredCredentialEndpoint) {
null -> QueryForDeferredCredential.NotSupported
else -> {
val refreshAccessToken = RefreshAccessToken(config.clock, tokenEndpointClient)
val deferredEndPointClient =
DeferredEndPointClient(deferredEndpoint, dPoPJwtFactory, ktorHttpClientFactory)
QueryForDeferredCredential(refreshAccessToken, deferredEndPointClient, responseEncryptionSpec)
}
}
val notifyIssuer =
when (val notificationEndpoint = credentialOffer.credentialIssuerMetadata.notificationEndpoint) {
null -> NotifyIssuer.NoOp
else -> {
val notificationEndPointClient =
NotificationEndPointClient(notificationEndpoint, dPoPJwtFactory, ktorHttpClientFactory)
NotifyIssuer(notificationEndPointClient)
}
}
object :
Issuer,
AuthorizeIssuance by authorizeIssuance,
RequestIssuance by requestIssuance,
RequestBatchIssuance by requestIssuance,
QueryForDeferredCredential by queryForDeferredCredential,
NotifyIssuer by notifyIssuer {
override val credentialOffer: CredentialOffer
get() = credentialOffer
override val dPoPJwtFactory: DPoPJwtFactory?
get() = dPoPJwtFactory
override fun AuthorizedRequest.deferredContext(
deferredCredential: IssuedCredential.Deferred,
): DeferredIssuanceContext {
val credentialIssuerMetadata = credentialOffer.credentialIssuerMetadata
val authorizationServerMetadata = credentialOffer.authorizationServerMetadata
val deferredEndpoint =
checkNotNull(credentialIssuerMetadata.deferredCredentialEndpoint?.value) {
"Missing deferred credential endpoint"
}
val tokenEndpoint =
checkNotNull(authorizationServerMetadata.tokenEndpointURI?.toURL()) {
"Missing token endpoint"
}
return DeferredIssuanceContext(
DeferredIssuerConfig(
client = config.client,
deferredEndpoint = deferredEndpoint,
authServerId = URL(authorizationServerMetadata.issuer.value),
tokenEndpoint = tokenEndpoint,
dPoPSigner = dPoPJwtFactory?.signer,
clientAttestationPoPBuilder = config.clientAttestationPoPBuilder,
responseEncryptionSpec = responseEncryptionSpec,
clock = config.clock,
),
AuthorizedTransaction(this@deferredContext, deferredCredential.transactionId),
)
}
}
}
/**
* Factory method for creating an instance of [Issuer] based on a credential offer URI.
* Method will try to first resolve the [credentialOfferUri] into a [CredentialOffer]
* proceed with the creation of the [Issuer]
*
* @param config wallet's configuration options
* @param credentialOfferUri the credential offer uri to be resolved
* @param ktorHttpClientFactory a factory for obtaining http clients, used while interacting with issuer
* @param responseEncryptionSpecFactory a factory method to generate the issuance response encryption
*
* @return if wallet's [config] can satisfy the requirements of the resolved credentialOffer an [Issuer] will be
* created. Otherwise, there would be a failed result
*/
suspend fun make(
config: OpenId4VCIConfig,
credentialOfferUri: String,
ktorHttpClientFactory: KtorHttpClientFactory = DefaultHttpClientFactory,
responseEncryptionSpecFactory: ResponseEncryptionSpecFactory = DefaultResponseEncryptionSpecFactory,
): Result<Issuer> = runCatching {
val credentialOfferRequestResolver = CredentialOfferRequestResolver(ktorHttpClientFactory)
val credentialOffer = credentialOfferRequestResolver.resolve(credentialOfferUri).getOrThrow()
make(config, credentialOffer, ktorHttpClientFactory, responseEncryptionSpecFactory).getOrThrow()
}
/**
* Factory method for creating an instance of [Issuer] with a credential offer (Wallet initiated)
* This requires out-of-band knowledge of [issuer][credentialIssuerId] and one or more
* [credentialConfigurationIdentifiers].
*
* This is equivalent to instantiating a credential offer, using authorization code grant,
* without `issuer_state`.
*
* @param config wallet's configuration options
* @param credentialIssuerId the id of the credential issuer
* @param credentialConfigurationIdentifiers a list of credential configuration identifiers
* @param ktorHttpClientFactory a factory for obtaining http clients, used while interacting with issuer
* @param responseEncryptionSpecFactory a factory method to generate the issuance response encryption
*
* @return if wallet's [config] can satisfy the requirements of credential issuer, an [Issuer] will be
* created. Otherwise, there would be a failed result
*/
suspend fun makeWalletInitiated(
config: OpenId4VCIConfig,
credentialIssuerId: CredentialIssuerId,
credentialConfigurationIdentifiers: List<CredentialConfigurationIdentifier>,
ktorHttpClientFactory: KtorHttpClientFactory = DefaultHttpClientFactory,
responseEncryptionSpecFactory: ResponseEncryptionSpecFactory = DefaultResponseEncryptionSpecFactory,
): Result<Issuer> = runCatching {
require(credentialConfigurationIdentifiers.isNotEmpty()) {
"At least one credential configuration identifier must be specified"
}
val (credentialIssuerMetadata, authServersMetadata) =
ktorHttpClientFactory().use { httpClient -> metaData(httpClient, credentialIssuerId) }
val credentialOffer =
CredentialOffer(
credentialIssuerId,
credentialIssuerMetadata,
authServersMetadata.first(),
credentialConfigurationIdentifiers,
Grants.AuthorizationCode(issuerState = null),
)
make(config, credentialOffer, ktorHttpClientFactory, responseEncryptionSpecFactory).getOrThrow()
}
val DefaultResponseEncryptionSpecFactory: ResponseEncryptionSpecFactory =
{ supportedAlgorithmsAndMethods, keyGenerationConfig ->
val method = supportedAlgorithmsAndMethods.encryptionMethods[0]
supportedAlgorithmsAndMethods.algorithms.firstNotNullOfOrNull { alg ->
KeyGenerator.genKeyIfSupported(keyGenerationConfig, alg)?.let { jwk ->
IssuanceResponseEncryptionSpec(jwk, alg, method)
}
}
}
}
}
private const val ATTEST_JWT_CLIENT_AUTH = "attest_jwt_client_auth"
internal fun Client.ensureSupportedByAuthorizationServer(authorizationServerMetadata: CIAuthorizationServerMetadata) {
val tokenEndPointAuthMethods =
authorizationServerMetadata.tokenEndpointAuthMethods.orEmpty()
when (this) {
is Client.Attested -> {
val expectedMethod = ClientAuthenticationMethod(ATTEST_JWT_CLIENT_AUTH)
require(expectedMethod in tokenEndPointAuthMethods) {
"$ATTEST_JWT_CLIENT_AUTH not supported by authorization server"
}
}
else -> Unit
}
}