-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClaimPath.kt
More file actions
191 lines (165 loc) · 6.14 KB
/
ClaimPath.kt
File metadata and controls
191 lines (165 loc) · 6.14 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
/*
* 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 eu.europa.ec.eudi.openid4vci.ClaimPathElement.*
import eu.europa.ec.eudi.openid4vci.internal.ClaimPathSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
//
// That's a copy from sd-jwt-kt lib
//
/**
* The path is a non-empty [list][value] of [elements][ClaimPathElement],
* null values, or non-negative integers.
* It is used to [select][SelectPath] a particular claim in the credential or a set of claims.
*
* It is [serialized][ClaimPathSerializer] as a [JsonArray] which may contain
* string, `null`, or integer elements
*/
@Serializable(with = ClaimPathSerializer::class)
@JvmInline
value class ClaimPath(val value: List<ClaimPathElement>) {
init {
require(value.isNotEmpty())
}
override fun toString(): String = value.toString()
operator fun plus(other: ClaimPathElement): ClaimPath = ClaimPath(this.value + other)
operator fun plus(other: ClaimPath): ClaimPath = ClaimPath(this.value + other.value)
operator fun contains(that: ClaimPath): Boolean = value.foldIndexed(this.value.size <= that.value.size) { index, acc, thisElement ->
fun comp() = that.value.getOrNull(index)?.let { thatElement -> thatElement in thisElement } == true
acc and comp()
}
/**
* Appends a wild-card indicator [ClaimPathElement.AllArrayElements]
*/
fun allArrayElements(): ClaimPath = this + AllArrayElements
/**
* Appends an indexed path [ClaimPathElement.ArrayElement]
*/
fun arrayElement(i: Int): ClaimPath = this + ArrayElement(i)
/**
* Appends a named path [ClaimPathElement.Claim]
*/
fun claim(name: String): ClaimPath = this + Claim(name)
/**
* Gets the ClaimPath of the parent element. Returns `null` to indicate the root element.
*/
fun parent(): ClaimPath? = value.dropLast(1)
.takeIf { it.isNotEmpty() }
?.let { ClaimPath(it) }
fun head(): ClaimPathElement = value.first()
fun tail(): ClaimPath? {
val tailElements = value.drop(1)
return if (tailElements.isEmpty()) return null
else ClaimPath(tailElements)
}
/**
* Gets the [head]
*/
operator fun component1(): ClaimPathElement = head()
/**
* Gets the [tail]
*/
operator fun component2(): ClaimPath? = tail()
companion object {
fun claim(name: String): ClaimPath = ClaimPath(listOf(Claim(name)))
}
}
/**
* Elements of a [ClaimPath]
* - [Claim] indicates that the respective [key][Claim.name] is to be selected
* - [AllArrayElements] indicates that all elements of the currently selected array(s) are to be selected, and
* - [ArrayElement] indicates that the respective [index][ArrayElement.index] in an array is to be selected
*/
sealed interface ClaimPathElement {
/**
* Indicates that all elements of the currently selected array(s) are to be selected
* It is serialized as a [JsonNull]
*/
data object AllArrayElements : ClaimPathElement {
override fun toString(): String = "null"
}
/**
* Indicates that the respective [index][index] in an array is to be selected.
* It is serialized as an [integer][JsonPrimitive]
* @param index Non-negative index
*/
@JvmInline
value class ArrayElement(val index: Int) : ClaimPathElement {
init {
require(index >= 0) { "Index should be non-negative" }
}
override fun toString(): String = index.toString()
}
/**
* Indicates that the respective [key][name] is to be selected.
* It is serialized as a [string][JsonPrimitive]
* @param name the attribute name
*/
@JvmInline
value class Claim(val name: String) : ClaimPathElement {
override fun toString(): String = name
}
/**
* Indication of whether the current instance contains the other.
* @param that the element to compare with
* @return in case that the two elements are of the same type, and if they are equal (including attribute),
* then true is being returned. Also, an [AllArrayElements] contains [ArrayElement].
* In all other cases, a false is being returned.
*/
operator fun contains(that: ClaimPathElement): Boolean = when (this) {
AllArrayElements -> when (that) {
AllArrayElements -> true
is ArrayElement -> true
is Claim -> false
}
is ArrayElement -> this == that
is Claim -> this == that
}
}
@OptIn(ExperimentalContracts::class)
inline fun <T> ClaimPathElement.fold(
ifAllArrayElements: () -> T,
ifArrayElement: (Int) -> T,
ifClaim: (String) -> T,
): T {
contract {
callsInPlace(ifAllArrayElements, InvocationKind.AT_MOST_ONCE)
callsInPlace(ifArrayElement, InvocationKind.AT_MOST_ONCE)
callsInPlace(ifClaim, InvocationKind.AT_MOST_ONCE)
}
return when (this) {
AllArrayElements -> ifAllArrayElements()
is ArrayElement -> ifArrayElement(index)
is ClaimPathElement.Claim -> ifClaim(name)
}
}
fun JsonArray.asClaimPath(): ClaimPath {
val elements = map {
require(it is JsonPrimitive)
it.asClaimPathElement()
}
return ClaimPath(elements)
}
fun JsonPrimitive.asClaimPathElement(): ClaimPathElement = when {
this is JsonNull -> AllArrayElements
isString -> Claim(content)
intOrNull != null -> ArrayElement(int)
else -> throw IllegalArgumentException("Only string, null, int can be used")
}