-
Notifications
You must be signed in to change notification settings - Fork 36
/
Issuer.kt
157 lines (134 loc) · 4.82 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
package id.walt.credentials.issuance
import id.walt.credentials.utils.W3CDataMergeUtils.mergeWithMapping
import id.walt.credentials.utils.W3CVcUtils.overwrite
import id.walt.credentials.utils.W3CVcUtils.update
import id.walt.credentials.vc.vcs.W3CVC
import id.walt.crypto.keys.Key
import id.walt.sdjwt.SDMap
import kotlinx.datetime.Instant
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.seconds
object Issuer {
/**
* Manually set data and issue credential
*/
suspend fun W3CVC.baseIssue(
key: Key,
did: String,
subject: String,
dataOverwrites: Map<String, JsonElement>,
dataUpdates: Map<String, Map<String, JsonElement>>,
additionalJwtHeader: Map<String, String>,
additionalJwtOptions: Map<String, JsonElement>
): String {
val overwritten = overwrite(dataOverwrites)
var updated = overwritten
dataUpdates.forEach { (k, v) -> updated = updated.update(k, v) }
return signJws(
issuerKey = key,
issuerDid = did,
subjectDid = subject,
additionalJwtHeader = additionalJwtHeader,
additionalJwtOptions = additionalJwtOptions
)
}
suspend fun W3CVC.mergingJwtIssue(
issuerKey: Key,
issuerDid: String,
subjectDid: String,
mappings: JsonObject,
additionalJwtHeader: Map<String, String>,
additionalJwtOptions: Map<String, JsonElement>,
completeJwtWithDefaultCredentialData: Boolean = true,
) = mergingToVc(
issuerDid = issuerDid,
subjectDid = subjectDid,
mappings = mappings,
completeJwtWithDefaultCredentialData
).run {
w3cVc.signJws(
issuerKey = issuerKey,
issuerDid = issuerDid,
subjectDid = subjectDid,
additionalJwtHeader = additionalJwtHeader.toMutableMap().apply {
put("typ", "JWT")
},
additionalJwtOptions = additionalJwtOptions.toMutableMap().apply {
putAll(jwtOptions)
}
)
}
suspend fun W3CVC.mergingSdJwtIssue(
issuerKey: Key,
issuerDid: String,
subjectDid: String,
mappings: JsonObject,
additionalJwtHeader: Map<String, String>,
additionalJwtOptions: Map<String, JsonElement>,
completeJwtWithDefaultCredentialData: Boolean = true,
disclosureMap: SDMap
) = mergingToVc(
issuerDid = issuerDid,
subjectDid = subjectDid,
mappings = mappings,
completeJwtWithDefaultCredentialData
).run {
w3cVc.signSdJwt(
issuerKey = issuerKey,
issuerDid = issuerDid,
subjectDid = subjectDid,
disclosureMap = disclosureMap,
additionalJwtHeader = additionalJwtHeader.toMutableMap().apply {
put("typ", "JWT")
},
additionalJwtOptions = additionalJwtOptions.toMutableMap().apply {
putAll(jwtOptions)
}
)
}
data class IssuanceInformation(
val w3cVc: W3CVC,
val jwtOptions: Map<String, JsonElement>
)
/**
* Merge data with mappings and issue
*/
suspend fun W3CVC.mergingToVc(
issuerDid: String,
subjectDid: String,
mappings: JsonObject,
completeJwtWithDefaultCredentialData: Boolean = true,
): IssuanceInformation {
val context = mapOf(
"issuerDid" to JsonPrimitive(issuerDid),
"subjectDid" to JsonPrimitive(subjectDid)
)
val mapped = this.mergeWithMapping(mappings, context, dataFunctions)
val vc = mapped.vc
val jwtRes = mapped.results.mapKeys { it.key.removePrefix("jwt:") }.toMutableMap()
fun completeJwtAttributes(attribute: String, completer: () -> JsonElement?) {
if (attribute !in jwtRes) {
val completed = completer.invoke()
if (completed != null) {
jwtRes[attribute] = completed
}
}
}
if (completeJwtWithDefaultCredentialData) {
completeJwtAttributes("jti") { vc["id"] }
completeJwtAttributes("exp") {
vc["expirationDate"]?.let { Instant.parse(it.jsonPrimitive.content) }
?.epochSeconds?.let { JsonPrimitive(it) }
}
completeJwtAttributes("iat") {
vc["issuanceDate"]?.let { Instant.parse(it.jsonPrimitive.content) }
?.epochSeconds?.let { JsonPrimitive(it) }
}
completeJwtAttributes("nbf") {
vc["issuanceDate"]?.let { Instant.parse(it.jsonPrimitive.content) - 90.seconds }
?.epochSeconds?.let { JsonPrimitive(it) }
}
}
return IssuanceInformation(vc, jwtRes)
}
}