-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeferredIssuerExtensions.kt
186 lines (168 loc) · 7.34 KB
/
DeferredIssuerExtensions.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
/*
* 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.examples
import com.nimbusds.jose.EncryptionMethod
import com.nimbusds.jose.JWEAlgorithm
import com.nimbusds.jose.jwk.JWK
import eu.europa.ec.eudi.openid4vci.*
import kotlinx.serialization.Required
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import java.net.URL
import java.time.Clock
import java.time.Duration
import java.time.Instant
suspend fun DeferredIssuer.Companion.queryForDeferredCredential(
clock: Clock = Clock.systemDefaultZone(),
ctxTO: DeferredIssuanceStoredContextTO,
recreatePopSigner: ((String) -> PopSigner.Jwt)? = null,
ktorHttpClientFactory: KtorHttpClientFactory = DefaultHttpClientFactory,
): Result<Pair<DeferredIssuanceStoredContextTO?, DeferredCredentialQueryOutcome>> = runCatching {
val ctx = ctxTO.toDeferredIssuanceStoredContext(clock, recreatePopSigner)
val (newCtx, outcome) = queryForDeferredCredential(ctx, ktorHttpClientFactory).getOrThrow()
val newCtxTO = newCtx?.let { DeferredIssuanceStoredContextTO.from(it, ctxTO.dPoPSignerKid) }
newCtxTO to outcome
}
//
// Serialization
//
@Serializable
data class RefreshTokenTO(
@Required @SerialName("refresh_token") val refreshToken: String,
@SerialName("expires_in") val expiresIn: Long? = null,
) {
fun toRefreshToken(): RefreshToken {
val exp = expiresIn?.let { Duration.ofSeconds(it) }
return RefreshToken(refreshToken, exp)
}
companion object {
fun from(refreshToken: RefreshToken): RefreshTokenTO =
RefreshTokenTO(refreshToken.refreshToken, refreshToken.expiresIn?.toSeconds())
}
}
@Serializable
enum class AccessTokenTypeTO {
DPoP, Bearer
}
@Serializable
data class AccessTokenTO(
@Required @SerialName("type") val type: AccessTokenTypeTO,
@Required @SerialName("access_token") val accessToken: String,
@SerialName("expires_in") val expiresIn: Long? = null,
) {
fun toAccessToken(): AccessToken {
val exp = expiresIn?.let { Duration.ofSeconds(it) }
return when (type) {
AccessTokenTypeTO.DPoP -> AccessToken.DPoP(accessToken, exp)
AccessTokenTypeTO.Bearer -> AccessToken.Bearer(accessToken, exp)
}
}
companion object {
fun from(accessToken: AccessToken): AccessTokenTO {
return AccessTokenTO(
type = when (accessToken) {
is AccessToken.DPoP -> AccessTokenTypeTO.DPoP
is AccessToken.Bearer -> AccessTokenTypeTO.Bearer
},
accessToken = accessToken.accessToken,
expiresIn = accessToken.expiresIn?.toSeconds(),
)
}
}
}
@Serializable
data class DeferredIssuanceStoredContextTO(
@Required @SerialName("client_id") val clientId: String,
@Required @SerialName("deferred_endpoint") val deferredEndpoint: String,
@Required @SerialName("token_endpoint") val tokenEndpoint: String,
@SerialName("dpop_key_id") val dPoPSignerKid: String? = null,
@SerialName("credential_response_encryption_spec") val responseEncryptionSpec: JsonObject? = null,
@SerialName("transaction_id") val transactionId: String,
@SerialName("access_token") val accessToken: AccessTokenTO,
@SerialName("refresh_token") val refreshToken: RefreshTokenTO? = null,
@SerialName("authorization_timestamp") val authorizationTimestamp: Long,
) {
fun toDeferredIssuanceStoredContext(
clock: Clock,
recreatePopSigner: ((String) -> PopSigner.Jwt)?,
): DeferredIssuanceContext {
return DeferredIssuanceContext(
config = DeferredIssuerConfig(
clock = clock,
clientId = clientId,
deferredEndpoint = URL(deferredEndpoint),
tokenEndpoint = URL(tokenEndpoint),
dPoPSigner = dPoPSignerKid?.let { requireNotNull(recreatePopSigner).invoke(it) },
responseEncryptionSpec = responseEncryptionSpec?.let { responseEncryption(it) },
),
authorizedTransaction = AuthorizedTransaction(
authorizedRequest = AuthorizedRequest.NoProofRequired(
accessToken = accessToken.toAccessToken(),
refreshToken = refreshToken?.toRefreshToken(),
credentialIdentifiers = emptyMap(),
timestamp = Instant.ofEpochSecond(authorizationTimestamp),
),
transactionId = TransactionId(transactionId),
),
)
}
companion object {
fun from(
dCtx: DeferredIssuanceContext,
dPoPSignerKid: String?,
): DeferredIssuanceStoredContextTO {
val authorizedTransaction = dCtx.authorizedTransaction
return DeferredIssuanceStoredContextTO(
clientId = dCtx.config.clientId,
deferredEndpoint = dCtx.config.deferredEndpoint.toString(),
tokenEndpoint = dCtx.config.tokenEndpoint.toString(),
dPoPSignerKid = dPoPSignerKid,
responseEncryptionSpec = dCtx.config.responseEncryptionSpec?.let { responseEncryptionSpecTO(it) },
transactionId = authorizedTransaction.transactionId.value,
accessToken = AccessTokenTO.from(authorizedTransaction.authorizedRequest.accessToken),
refreshToken = authorizedTransaction.authorizedRequest.refreshToken?.let { RefreshTokenTO.from(it) },
authorizationTimestamp = authorizedTransaction.authorizedRequest.timestamp.epochSecond,
)
}
private fun responseEncryptionSpecTO(spec: IssuanceResponseEncryptionSpec): JsonObject {
val jwkJson = Json.parseToJsonElement(spec.jwk.toJSONString())
return buildJsonObject {
put("jwk", jwkJson)
put("algorithm", spec.algorithm.toString())
put("encryption_method", spec.encryptionMethod.toString())
}
}
private fun responseEncryption(specTO: JsonObject): IssuanceResponseEncryptionSpec =
IssuanceResponseEncryptionSpec(
jwk = run {
val element = specTO["jwk"]
require(element is JsonObject)
JWK.parse(element.toString())
},
algorithm = run {
val element = specTO["algorithm"]
require(element is JsonPrimitive)
JWEAlgorithm.parse(requireNotNull(element.contentOrNull))
},
encryptionMethod = run {
val element = specTO["encryption_method"]
require(element is JsonPrimitive)
EncryptionMethod.parse(requireNotNull(element.contentOrNull))
},
)
}
}