@@ -105,14 +105,14 @@ func isWildcardTil(tilList []string) (isWildcard bool, err error) {
105105
106106func verifyWithCredentialsConfig (verifiableCredential * verifiable.Credential , credentials []tir.Credential ) (result bool , err error ) {
107107
108- credentialsConfigMap := map [string ]tir.Credential {}
108+ credentialsConfigMap := map [string ][] tir.Credential {}
109109
110110 // format for better validation
111111 for _ , credential := range credentials {
112- credentialsConfigMap [credential .CredentialsType ] = credential
112+ credentialsConfigMap [credential .CredentialsType ] = append ( credentialsConfigMap [ credential . CredentialsType ], credential )
113113 }
114114
115- // initalize to true, since everything without a specific rule is considered to be allowed
115+ // initialize to true, since everything without a specific rule is considered to be allowed
116116 var subjectAllowed = true
117117
118118 // validate that the type(s) is allowed
@@ -134,39 +134,44 @@ func verifyWithCredentialsConfig(verifiableCredential *verifiable.Credential, cr
134134 return true , err
135135}
136136
137- func verifyForType (subjectToVerfiy verifiable.Subject , credentialConfig tir.Credential ) (result bool ) {
138- for _ , claim := range credentialConfig .Claims {
139-
140- if claim .Path != "" {
141- validClaim := verifyWithJsonPath (subjectToVerfiy , claim )
142- if validClaim {
143- logging .Log ().Debugf ("Claim with path %s is valid. Credential Subject %s" , claim .Path , logging .PrettyPrintObject (subjectToVerfiy ))
144- continue
137+ // verifyForType returns true if the subject satisfies at least one credential config (OR).
138+ // Each config is satisfied only if all its claims are valid (AND).
139+ func verifyForType (subjectToVerify verifiable.Subject , credentialConfig []tir.Credential ) bool {
140+ for _ , config := range credentialConfig {
141+ allClaimsValid := true
142+ for _ , claim := range config .Claims {
143+ if claim .Path != "" {
144+ if ! verifyWithJsonPath (subjectToVerify , claim ) {
145+ logging .Log ().Warnf ("Claim with path %s is not valid." , claim .Path )
146+ allClaimsValid = false
147+ break
148+ }
149+ logging .Log ().Debugf ("Claim with path %s is valid. Credential Subject %s" , claim .Path , logging .PrettyPrintObject (subjectToVerify ))
145150 } else {
146- logging .Log ().Warnf ("Claim with path %s is not valid." , claim .Path )
147- return false
148- }
149- } else {
150- // old name base logic
151- claimValue , exists := subjectToVerfiy .CustomFields [claim .Name ]
152- if ! exists {
153- logging .Log ().Debugf ("Restricted claim %s is not part of the subject %s." , claim .Name , logging .PrettyPrintObject (subjectToVerfiy ))
154- continue
155- }
156- isAllowed := contains (claim .AllowedValues , claimValue )
157- if ! isAllowed {
158- logging .Log ().Debugf ("The claim value %s is not allowed by the config %s." , logging .PrettyPrintObject (claimValue ), logging .PrettyPrintObject (credentialConfig ))
159- return false
151+ // legacy name-based validation
152+ claimValue , exists := subjectToVerify .CustomFields [claim .Name ]
153+ if ! exists {
154+ logging .Log ().Debugf ("Claim %s is not present in subject %s, skipping." , claim .Name , logging .PrettyPrintObject (subjectToVerify ))
155+ continue
156+ }
157+ if ! contains (claim .AllowedValues , claimValue ) {
158+ logging .Log ().Debugf ("Claim value %s is not allowed by config %s." , logging .PrettyPrintObject (claimValue ), logging .PrettyPrintObject (credentialConfig ))
159+ allClaimsValid = false
160+ break
161+ }
160162 }
161163 }
162-
164+ if allClaimsValid {
165+ logging .Log ().Debugf ("No forbidden claim found for subject %s. Checked config was %s." , logging .PrettyPrintObject (subjectToVerify ), logging .PrettyPrintObject (credentialConfig ))
166+ return true
167+ }
163168 }
164- logging .Log ().Debugf ("No forbidden claim found for subject %s. Checked config was %s." , logging .PrettyPrintObject (subjectToVerfiy ), logging .PrettyPrintObject (credentialConfig ))
165- return true
169+ logging .Log ().Debugf ("No credential config matched for subject %s. Config: %s." , logging .PrettyPrintObject (subjectToVerify ), logging .PrettyPrintObject (credentialConfig ))
170+ return false
166171}
167172
168- func verifyWithJsonPath (subjectToVerfiy verifiable.Subject , claim tir.Claim ) (result bool ) {
169- jsonSubject , _ := json .Marshal (subjectToVerfiy .CustomFields )
173+ func verifyWithJsonPath (subjectToVerify verifiable.Subject , claim tir.Claim ) (result bool ) {
174+ jsonSubject , _ := json .Marshal (subjectToVerify .CustomFields )
170175 var subjectAsMap map [string ]interface {}
171176 if err := json .Unmarshal (jsonSubject , & subjectAsMap ); err != nil {
172177 logging .Log ().Warnf ("Was not able to unmarshal the subject, set to invalid. Err: %v" , err )
@@ -194,7 +199,7 @@ func toSliceOfMaps(raw []interface{}) []map[string]interface{} {
194199 for _ , item := range raw {
195200 m , ok := item .(map [string ]interface {})
196201 if ! ok {
197- logging .Log ().Warnf ("Was not able to convert the allowed values, dont allow anything. V: %v" , item )
202+ logging .Log ().Warnf ("Was not able to convert the allowed values, don't allow anything. V: %v" , item )
198203 return []map [string ]interface {}{}
199204 }
200205 result = append (result , m )
@@ -241,7 +246,7 @@ func contains(interfaces []interface{}, interfaceToCheck interface{}) bool {
241246 for _ , i := range interfaces {
242247 jsonBytes , err := json .Marshal (i )
243248 if err != nil {
244- logging .Log ().Warn ("Not able to marshal one of the intefaces ." )
249+ logging .Log ().Warn ("Not able to marshal one of the interfaces ." )
245250 continue
246251 }
247252 if slices .Compare (jsonBytes , jsonBytesToCheck ) == 0 {
0 commit comments