From 9f992f7699aed04d193acaaecde1b5170813bcdd Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Mon, 20 Nov 2023 15:49:55 +0100 Subject: [PATCH] PR feedback --- vcr/credential/resolver.go | 3 +-- vcr/pe/presentation_submission.go | 36 ++++++++++++++------------ vcr/pe/presentation_submission_test.go | 4 +-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/vcr/credential/resolver.go b/vcr/credential/resolver.go index f60ecc8fb4..eda669ff84 100644 --- a/vcr/credential/resolver.go +++ b/vcr/credential/resolver.go @@ -83,8 +83,7 @@ func PresentationSigner(presentation vc.VerifiablePresentation) (*did.DID, error verificationMethod, err := did.ParseDIDURL(proofs[0].VerificationMethod.String()) if err != nil || verificationMethod.DID.Empty() { return nil, fmt.Errorf("invalid verification method for JSON-LD presentation: %w", err) - } else { - return &verificationMethod.DID, nil } + return &verificationMethod.DID, nil } } diff --git a/vcr/pe/presentation_submission.go b/vcr/pe/presentation_submission.go index 633b652dfa..8dcef7c917 100644 --- a/vcr/pe/presentation_submission.go +++ b/vcr/pe/presentation_submission.go @@ -26,6 +26,7 @@ import ( "github.com/nuts-foundation/go-did/did" "github.com/nuts-foundation/go-did/vc" v2 "github.com/nuts-foundation/nuts-node/vcr/pe/schema/v2" + "strings" ) // ParsePresentationSubmission validates the given JSON and parses it into a PresentationSubmission. @@ -189,7 +190,7 @@ func (s PresentationSubmission) Resolve(presentations []vc.VerifiablePresentatio result := make(map[string]vc.VerifiableCredential) for _, inputDescriptor := range s.DescriptorMap { - resolvedCredential, err := resolveCredential(inputDescriptor.Id, 0, inputDescriptor, envelope) + resolvedCredential, err := resolveCredential(nil, inputDescriptor, envelope) if err != nil { return nil, fmt.Errorf("unable to resolve credential for input descriptor '%s': %w", inputDescriptor.Id, err) } @@ -198,10 +199,13 @@ func (s PresentationSubmission) Resolve(presentations []vc.VerifiablePresentatio return result, nil } -func resolveCredential(descriptorID string, level int, mapping InputDescriptorMappingObject, value interface{}) (*vc.VerifiableCredential, error) { +func resolveCredential(path []string, mapping InputDescriptorMappingObject, value interface{}) (*vc.VerifiableCredential, error) { + fullPath := append(path, mapping.Path) + fullPathString := strings.Join(fullPath, "/") + targetValueRaw, err := jsonpath.Get(mapping.Path, value) if err != nil { - return nil, fmt.Errorf("unable to get value for path %s: %w", mapping.Path, err) + return nil, fmt.Errorf("unable to get value for path %s: %w", fullPathString, err) } var decodedTargetValue interface{} @@ -211,12 +215,12 @@ func resolveCredential(descriptorID string, level int, mapping InputDescriptorMa if mapping.Format == vc.JWTCredentialProofFormat { decodedTargetValue, err = vc.ParseVerifiableCredential(targetValue) if err != nil { - return nil, fmt.Errorf("invalid JWT credential at path '%s': %w", mapping.Path, err) + return nil, fmt.Errorf("invalid JWT credential at path '%s': %w", fullPathString, err) } } else if mapping.Format == vc.JWTPresentationProofFormat { decodedTargetValue, err = vc.ParseVerifiablePresentation(targetValue) if err != nil { - return nil, fmt.Errorf("invalid JWT presentation at path '%s': %w", mapping.Path, err) + return nil, fmt.Errorf("invalid JWT presentation at path '%s': %w", fullPathString, err) } } case map[string]interface{}: @@ -225,30 +229,28 @@ func resolveCredential(descriptorID string, level int, mapping InputDescriptorMa if mapping.Format == vc.JSONLDCredentialProofFormat { decodedTargetValue, err = vc.ParseVerifiableCredential(string(targetValueAsJSON)) if err != nil { - return nil, fmt.Errorf("invalid JSON-LD credential at path '%s' (level %d): %w", mapping.Path, level, err) + return nil, fmt.Errorf("invalid JSON-LD credential at path '%s': %w", fullPathString, err) } } else if mapping.Format == vc.JSONLDPresentationProofFormat { decodedTargetValue, err = vc.ParseVerifiablePresentation(string(targetValueAsJSON)) if err != nil { - return nil, fmt.Errorf("invalid JSON-LD presentation at path '%s' (level %d): %w", mapping.Path, level, err) + return nil, fmt.Errorf("invalid JSON-LD presentation at path '%s': %w", fullPathString, err) } } } if decodedTargetValue == nil { - return nil, fmt.Errorf("value of Go type '%T' at path '%s' (level %d) can't be decoded using format '%s'", targetValueRaw, mapping.Path, level, mapping.Format) + return nil, fmt.Errorf("value of Go type '%T' at path '%s' can't be decoded using format '%s'", targetValueRaw, fullPathString, mapping.Format) } if mapping.PathNested == nil { if decodedCredential, ok := decodedTargetValue.(*vc.VerifiableCredential); ok { return decodedCredential, nil - } else { - return nil, fmt.Errorf("path '%s' (level %d) does not reference a credential", mapping.Path, level) } - } else { - // path_nested implies the credential is not found at the evaluated JSON path, but further down. - // We need to decode the value at the path (could be a credential or presentation in JWT or VP format) and evaluate the nested path. - decodedValueJSON, _ := json.Marshal(decodedTargetValue) - var decodedValueMap map[string]interface{} - _ = json.Unmarshal(decodedValueJSON, &decodedValueMap) - return resolveCredential(descriptorID, level+1, *mapping.PathNested, decodedValueMap) + return nil, fmt.Errorf("path '%s' does not reference a credential", fullPathString) } + // path_nested implies the credential is not found at the evaluated JSON path, but further down. + // We need to decode the value at the path (could be a credential or presentation in JWT or VP format) and evaluate the nested path. + decodedValueJSON, _ := json.Marshal(decodedTargetValue) + var decodedValueMap map[string]interface{} + _ = json.Unmarshal(decodedValueJSON, &decodedValueMap) + return resolveCredential(fullPath, *mapping.PathNested, decodedValueMap) } diff --git a/vcr/pe/presentation_submission_test.go b/vcr/pe/presentation_submission_test.go index 7d6ca39109..badec4dffe 100644 --- a/vcr/pe/presentation_submission_test.go +++ b/vcr/pe/presentation_submission_test.go @@ -310,7 +310,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { credentials, err := submission.Resolve([]vc.VerifiablePresentation{vp}) - require.EqualError(t, err, "unable to resolve credential for input descriptor '1': path '$.verifiableCredential' (level 0) does not reference a credential") + require.EqualError(t, err, "unable to resolve credential for input descriptor '1': path '$.verifiableCredential' does not reference a credential") assert.Nil(t, credentials) }) t.Run("invalid JSON-LD credential", func(t *testing.T) { @@ -379,7 +379,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { credentials, err := submission.Resolve([]vc.VerifiablePresentation{vp}) - assert.EqualError(t, err, "unable to resolve credential for input descriptor '1': value of Go type 'string' at path '$.verifiableCredential.expirationDate' (level 0) can't be decoded using format 'ldp_vc'") + assert.EqualError(t, err, "unable to resolve credential for input descriptor '1': value of Go type 'string' at path '$.verifiableCredential.expirationDate' can't be decoded using format 'ldp_vc'") assert.Nil(t, credentials) }) }