@@ -2,32 +2,48 @@ package id.walt.auditor
2
2
3
3
import com.beust.klaxon.JsonObject
4
4
import com.beust.klaxon.Klaxon
5
+ import com.beust.klaxon.KlaxonException
5
6
import id.walt.auditor.dynamic.DynamicPolicy
6
7
import id.walt.auditor.dynamic.DynamicPolicyArg
7
8
import id.walt.common.deepMerge
8
9
import id.walt.common.resolveContent
9
10
import id.walt.model.dif.PresentationDefinition
10
11
import id.walt.services.context.ContextManager
11
12
import id.walt.services.hkvstore.HKVKey
13
+ import mu.KotlinLogging
12
14
import java.io.StringReader
15
+ import java.lang.reflect.InvocationTargetException
13
16
import kotlin.reflect.KClass
14
17
import kotlin.reflect.full.createInstance
15
18
import kotlin.reflect.full.primaryConstructor
16
19
20
+ private val log = KotlinLogging .logger {}
21
+
17
22
open class PolicyFactory <P : VerificationPolicy , A : Any >(
18
23
val policyType : KClass <P >,
19
24
val argType : KClass <A >? ,
20
25
val name : String ,
21
- val description : String? = null
26
+ val description : String? = null ,
27
+ val optionalArgument : Boolean = false
22
28
) {
23
29
open fun create (argument : Any? = null): P {
24
- return argType?.let {
25
- policyType.primaryConstructor!! .call(argument)
30
+ try {
31
+ return argType?.let {
32
+ if (optionalArgument) {
33
+ argument?.let {
34
+ return policyType.primaryConstructor!! .call(it)
35
+ }
36
+ } else {
37
+ return policyType.primaryConstructor!! .call(argument)
38
+ }
39
+ } ? : policyType.createInstance()
40
+ } catch (e: KlaxonException ) {
41
+ throw IllegalArgumentException (" Provided argument was of wrong type." , e)
42
+ } catch (e: InvocationTargetException ) {
43
+ throw IllegalArgumentException (" No argument was provided." , e)
26
44
}
27
- ? : policyType.createInstance()
28
45
}
29
46
30
-
31
47
val requiredArgumentType = when (argType) {
32
48
null -> " None"
33
49
else -> argType.simpleName!!
@@ -75,8 +91,9 @@ object PolicyRegistry {
75
91
fun <P : ParameterizedVerificationPolicy <A >, A : Any > register (
76
92
policy : KClass <P >,
77
93
argType : KClass <A >,
78
- description : String? = null
79
- ) = policies.put(policy.simpleName!! , PolicyFactory (policy, argType, policy.simpleName!! , description))
94
+ description : String? = null,
95
+ optionalArgument : Boolean = false
96
+ ) = policies.put(policy.simpleName!! , PolicyFactory (policy, argType, policy.simpleName!! , description, optionalArgument))
80
97
81
98
fun <P : SimpleVerificationPolicy > register (policy : KClass <P >, description : String? = null) =
82
99
policies.put(policy.simpleName!! , PolicyFactory <P , Unit >(policy, null , policy.simpleName!! , description))
@@ -173,7 +190,12 @@ object PolicyRegistry {
173
190
// register(JsonSchemaPolicy::class, "Verify by JSON schema")
174
191
register(TrustedSchemaRegistryPolicy ::class , " Verify by EBSI Trusted Schema Registry" )
175
192
register(TrustedIssuerDidPolicy ::class , " Verify by trusted issuer did" )
176
- register(TrustedIssuerRegistryPolicy ::class , " Verify by trusted EBSI Trusted Issuer Registry record" )
193
+ register(
194
+ TrustedIssuerRegistryPolicy ::class ,
195
+ TrustedIssuerRegistryPolicyArg ::class ,
196
+ " Verify by an EBSI Trusted Issuers Registry compliant api." ,
197
+ true
198
+ )
177
199
register(TrustedSubjectDidPolicy ::class , " Verify by trusted subject did" )
178
200
register(IssuedDateBeforePolicy ::class , " Verify by issuance date" )
179
201
register(ValidFromBeforePolicy ::class , " Verify by valid from" )
0 commit comments