@@ -18,6 +18,7 @@ import (
1818
1919 "golang.org/x/exp/slices"
2020
21+ "github.com/PaesslerAG/jsonpath"
2122 common "github.com/fiware/VCVerifier/common"
2223 configModel "github.com/fiware/VCVerifier/config"
2324 "github.com/fiware/VCVerifier/gaiax"
@@ -682,14 +683,31 @@ func buildInclusion(credential *verifiable.Credential, inclusionConfig configMod
682683
683684 inclusion = make (map [string ]interface {})
684685 for _ , claim := range inclusionConfig .ClaimsToInclude {
685- pathParts := strings .Split (claim .OriginalKey , "." )
686- if val , ok := getValueFromPath (credential .ToRawJSON (), pathParts ); ok {
686+ if strings .HasPrefix (claim .OriginalKey , "$" ) {
687+ logging .Log ().Debugf ("Claim uses json path: %s from %s" , claim .OriginalKey , logging .PrettyPrintObject (credential .ToRawJSON ()))
688+ claimValues , err := jsonpath .Get (claim .OriginalKey , credential .ToRawJSON ())
689+
690+ if err != nil {
691+ logging .Log ().Warnf ("Was not able to evaluate path %s" , claim .OriginalKey )
692+ continue
693+ }
694+
687695 if claim .NewKey != "" {
688- setValueAtPath (inclusion , strings .Split (claim .NewKey , "." ), val )
696+ setValueAtPath (inclusion , strings .Split (claim .NewKey , "." ), claimValues )
689697 } else {
690- setValueAtPath (inclusion , strings .Split (claim .OriginalKey , "." ), val )
698+ setValueAtPath (inclusion , strings .Split (claim .OriginalKey , "." ), claimValues )
699+ }
700+ } else {
701+ pathParts := strings .Split (claim .OriginalKey , "." )
702+ if val , ok := getValueFromPath (credential .ToRawJSON (), pathParts ); ok {
703+ if claim .NewKey != "" {
704+ setValueAtPath (inclusion , strings .Split (claim .NewKey , "." ), val )
705+ } else {
706+ setValueAtPath (inclusion , strings .Split (claim .OriginalKey , "." ), val )
707+ }
691708 }
692709 }
710+
693711 }
694712 return inclusion
695713}
@@ -813,9 +831,11 @@ func (v *CredentialVerifier) AuthenticationResponse(state string, verifiablePres
813831 }
814832 loginSession := loginSessionInterface .(loginSession )
815833
816- // TODO extract into separate policy
834+ credentialsByType , _ := extractCredentialTypes ( verifiablePresentation )
817835 trustedChain , _ := verifyChain (verifiablePresentation .Credentials ())
836+ var credentialsToBeIncluded []map [string ]interface {}
818837
838+ flatClaims := false
819839 for _ , credential := range verifiablePresentation .Credentials () {
820840
821841 verificationContext , err := v .getTrustRegistriesValidationContext (loginSession .clientId , credential .Contents ().Types , loginSession .scope )
@@ -845,21 +865,27 @@ func (v *CredentialVerifier) AuthenticationResponse(state string, verifiablePres
845865 logging .Log ().Infof ("VC %s is not valid." , logging .PrettyPrintObject (credential ))
846866 return sameDevice , ErrorInvalidVC
847867 }
868+ shouldBeIncluded , inclusionConfig := v .shouldBeIncluded (loginSession .clientId , loginSession .scope , credential .Contents ().Types )
869+ if shouldBeIncluded {
870+ credentialsToBeIncluded = append (credentialsToBeIncluded , buildInclusion (credential , inclusionConfig ))
871+ }
872+ flatClaims , _ = v .credentialsConfig .GetFlatClaims (loginSession .clientId , loginSession .scope )
848873 }
849874 }
850875
851876 // we ignore the error here, since the only consequence is that sub will be empty.
852877 hostname , _ := getHostName (loginSession .callback )
853878
854- //TODO: properly handle inclusion config
855-
856- var toBeIncluded []map [string ]interface {}
857- for _ , credential := range verifiablePresentation .Credentials () {
858- toBeIncluded = append (toBeIncluded , credential .ToRawJSON ())
879+ if len (credentialsToBeIncluded ) == 0 {
880+ vcTypes := []string {}
881+ for k := range credentialsByType {
882+ vcTypes = append (vcTypes , k )
883+ }
884+ logging .Log ().Warnf ("No valid credential type was provided. Provided credential type: %v" , vcTypes )
885+ return sameDevice , ErrorNoValidCredentialTypeProvided
859886 }
860887
861- flatClaims , _ := v .credentialsConfig .GetFlatClaims (loginSession .clientId , loginSession .scope )
862- token , err := v .generateJWT (toBeIncluded , verifiablePresentation .Holder , hostname , flatClaims )
888+ token , err := v .generateJWT (credentialsToBeIncluded , verifiablePresentation .Holder , hostname , flatClaims )
863889 if err != nil {
864890 logging .Log ().Warnf ("Was not able to create a jwt for %s. Err: %v" , state , err )
865891 return sameDevice , err
0 commit comments