Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix verification bug with JWT credentials #257

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion credential/exchange/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exchange

import (
"fmt"
credutil "github.com/TBD54566975/ssi-sdk/credential/util"
"strings"

"github.com/TBD54566975/ssi-sdk/crypto"
Expand Down Expand Up @@ -115,13 +116,19 @@ func VerifyPresentationSubmissionVP(def PresentationDefinition, vp credential.Ve
}

// resolve the claim from the JSON path expression in the submission descriptor
submittedClaim, err := jsonpath.JsonPathLookup(vpJSON, submissionDescriptor.Path)
claim, err := jsonpath.JsonPathLookup(vpJSON, submissionDescriptor.Path)
if err != nil {
err := errors.Wrapf(err, "could not resolve claim from submission descriptor<%s> with path: %s",
submissionDescriptor.ID, submissionDescriptor.Path)
logrus.WithError(err).Error()
return err
}
submittedClaim, err := credutil.ClaimAsJSON(claim)
if err != nil {
err := errors.Wrapf(err, "getting claim as go-json: <%s>", claim)
logrus.WithError(err).Error()
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

util.NewErrorf()...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping it here for consistency, and preventing scope creep. This should be addressed in #258

}

// verify the submitted claim complies with the input descriptor

Expand Down
44 changes: 44 additions & 0 deletions credential/exchange/verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,48 @@ func TestVerifyPresentationSubmissionVP(t *testing.T) {
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "matching path for claim could not be found")
})

t.Run("Verification with JWT credential", func(t *testing.T) {
def := PresentationDefinition{
ID: "test-id",
InputDescriptors: []InputDescriptor{
{
ID: "id-1",
Constraints: &Constraints{
Fields: []Field{
{
Path: []string{"$.vc.credentialSubject.company", "$.credentialSubject.company"},
ID: "works-for-block",
Purpose: "need to check the company of the subject",
},
},
},
},
},
}
signer, _ := getJWKSignerVerifier(t)
testVC := getTestVerifiableCredential()
vcData, err := signing.SignVerifiableCredentialJWT(*signer, testVC)
assert.NoError(t, err)
b := NewPresentationSubmissionBuilder(def.ID)
assert.NoError(t, b.SetDescriptorMap([]SubmissionDescriptor{
{
ID: "id-1",
Format: string(JWTVPTarget),
Path: "$.verifiableCredential[0]",
PathNested: nil,
},
}))
ps, err := b.Build()
assert.NoError(t, err)

vpBuilder := credential.NewVerifiablePresentationBuilder()
assert.NoError(t, vpBuilder.SetPresentationSubmission(ps))
assert.NoError(t, vpBuilder.AddVerifiableCredentials([]interface{}{string(vcData)}...))
vp2, err := vpBuilder.Build()
assert.NoError(t, err)
vp := *vp2

assert.NoError(t, VerifyPresentationSubmissionVP(def, vp))
})
}
24 changes: 24 additions & 0 deletions credential/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,27 @@ func CredentialsFromInterface(genericCred interface{}) (*credential.VerifiableCr
return nil, fmt.Errorf("invalid credential type: %s", reflect.TypeOf(genericCred).Kind().String())
}
}

// ClaimAsJSON converts a claim with an unknown interface{} into the go-json representation of that credential.
// claim can only be of type {string, map[string]interface, VerifiableCredential}.
func ClaimAsJSON(claim interface{}) (map[string]interface{}, error) {
switch c := claim.(type) {
case map[string]interface{}:
return c, nil
default:
}

vc, err := CredentialsFromInterface(claim)
if err != nil {
return nil, errors.Wrap(err, "credential from interface")
}
vcData, err := json.Marshal(vc)
if err != nil {
return nil, errors.Wrap(err, "marshalling credential")
}
var submittedClaim map[string]interface{}
if err := json.Unmarshal(vcData, &submittedClaim); err != nil {
return nil, errors.Wrap(err, "unmarshalling credential")
}
return submittedClaim, nil
}