Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Nov 20, 2023
1 parent 4c01792 commit 9f992f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
3 changes: 1 addition & 2 deletions vcr/credential/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
36 changes: 19 additions & 17 deletions vcr/pe/presentation_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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{}
Expand All @@ -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{}:
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions vcr/pe/presentation_submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 9f992f7

Please sign in to comment.