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

feat: convert jsonpath to RFC 9535 #94

Merged
merged 6 commits into from
Jan 27, 2025
Merged
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
60 changes: 54 additions & 6 deletions presexch/definition.go
Original file line number Diff line number Diff line change
@@ -1487,7 +1487,29 @@ func filterField(f *Field, credential map[string]interface{}, isJWTCredential bo
return lastErr
}

//nolint:gocyclo
// deprecated: convertToRFC9535Format converts path to RFC 9535 format.
func convertToRFC9535Format(path string) string {
var builder strings.Builder

segments := strings.Split(path, ".")

for i, segment := range segments {
if strings.Contains(segment, "-") && !strings.Contains(segment, "['") {
builder.WriteString(fmt.Sprintf("['%s']", segment))
continue
}

if i != 0 {
builder.WriteString(".")
}

builder.WriteString(segment)
}

return builder.String()
}

//nolint:gocyclo,funlen
func checkPathValue(
isJWTCredential bool,
path string,
@@ -1496,8 +1518,14 @@ func checkPathValue(
schema gojsonschema.JSONLoader,
) error {
if isJWTCredential { // compatibility
if strings.Contains(path, "$.vc.") {
path = strings.Replace(path, "$.vc.", "$.", 1)
replacements := map[string]string{
"$.vc.": "$.",
"$.vc[": "$[",
"$['vc']": "$",
}

for old, newVal := range replacements {
path = strings.Replace(path, old, newVal, 1)
}

if strings.EqualFold(path, "$.issuer") {
@@ -1514,9 +1542,29 @@ func checkPathValue(
return errors.New("expected $ or @ at start of path")
}

pathParsed, err := pathv2.Parse(path)
if err != nil {
return err
var pathParsed *pathv2.Path

var pathErr error

for _, p := range []string{
path,
// nolint:lll
convertToRFC9535Format(path), // Remove in 6months and update profiles presentations to use RFC 9535 format
} {
parsed, parseErr := pathv2.Parse(p)

pathParsed = parsed

if parseErr == nil {
pathErr = nil
break
}

pathErr = errors.Join(pathErr, fmt.Errorf("path %s: %w", p, parseErr))
}

if pathErr != nil {
return pathErr
}

selected := pathParsed.Select(credential)
65 changes: 65 additions & 0 deletions presexch/definition_test.go
Original file line number Diff line number Diff line change
@@ -2668,6 +2668,71 @@ func TestPresentationDefinition_CreateVP_V1Credential(t *testing.T) {
require.ErrorContains(t, err, "credentials have different base contexts")
})

t.Run("Match credential in jwt_vc format with escaping", func(t *testing.T) {
cases := []string{
"$['vc']['some_object']['hash-algo']",
"$.vc['some_object']['hash-algo']",
"$.vc.some_object['hash-algo']",
"$.vc.some_object.hash-algo",

"$['vc']['some_object']['hash-key']['hash-value']",
"$.vc['some_object']['hash-key']['hash-value']",
"$.vc.some_object['hash-key']['hash-value']",
"$.vc.some_object.hash-key.hash-value",
}

for _, c := range cases {
t.Run(c, func(t *testing.T) {
pd := &PresentationDefinition{
ID: uuid.New().String(),
InputDescriptors: []*InputDescriptor{{
ID: uuid.New().String(),
Constraints: &Constraints{
Fields: []*Field{
{
Path: []string{"$.vc.last_name"},
},
{
Path: []string{c},
},
},
},
}},
}

issuerDID := "did:example:76e12ec712ebc6f1c221ebfeb1f"

vc := createTestCredential(t, credentialProto{
Issued: utiltime.NewTime(time.Now()),
Context: []string{verifiable.V1ContextURI},
Types: []string{verifiable.VCType},
ID: uuid.New().String(),
Subject: []verifiable.Subject{{ID: issuerDID}},
Issuer: &verifiable.Issuer{ID: issuerDID},
CustomFields: map[string]interface{}{
"first_name": "Jesse",
"last_name": "Travis",
"some_object": map[string]interface{}{
"hash-algo": "xx",
"hash-key": map[string]interface{}{
"hash-value": "yy",
},
},
"hash-algo": "xx",
"age": 17,
},
})

jwtVC, err := vc.CreateUnsecuredJWTVC(false)
require.NoError(t, err)

vp, err := pd.CreateVP([]*verifiable.Credential{jwtVC}, lddl)
require.NoError(t, err)
require.Len(t, vp.Credentials(), 1)
})
}
})

t.Run("Match credential in jwt_vc format with filter containing $.vc path", func(t *testing.T) {
pd := &PresentationDefinition{
ID: uuid.New().String(),