Skip to content

Commit

Permalink
populate SearchResult.Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Dec 15, 2023
1 parent 3729af1 commit e9269c4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
1 change: 0 additions & 1 deletion discovery/api/v1/wapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Wrapper struct {
}

func (w *Wrapper) ResolveStatusCode(err error) int {
// todo
switch {
case errors.Is(err, discovery.ErrServerModeDisabled):
return http.StatusBadRequest
Expand Down
3 changes: 2 additions & 1 deletion discovery/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
)

// Tag is value that references a point in the list.
// It is opaque for clients: they should not try to interpret it.
// The server who issued the tag can interpret it as Lamport timestamp.
type Tag string
Expand Down Expand Up @@ -97,5 +98,5 @@ type Client interface {

type SearchResult struct {
VP vc.VerifiablePresentation
Fields map[string]string
Fields map[string]interface{}
}
25 changes: 21 additions & 4 deletions discovery/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/vc"
"github.com/nuts-foundation/nuts-node/core"
"github.com/nuts-foundation/nuts-node/discovery/log"
"github.com/nuts-foundation/nuts-node/storage"
"github.com/nuts-foundation/nuts-node/vcr"
"github.com/nuts-foundation/nuts-node/vcr/credential"
Expand Down Expand Up @@ -260,7 +261,8 @@ func loadDefinitions(directory string) (map[string]ServiceDefinition, error) {
}

func (m *Module) Search(serviceID string, query map[string]string) ([]SearchResult, error) {
if _, exists := m.serverDefinitions[serviceID]; !exists {
serviceDefinition, exists := m.serverDefinitions[serviceID]
if !exists {
return nil, ErrServerModeDisabled
}
matchingVPs, err := m.store.search(serviceID, query)
Expand All @@ -269,10 +271,25 @@ func (m *Module) Search(serviceID string, query map[string]string) ([]SearchResu
}
var result []SearchResult
for _, matchingVP := range matchingVPs {
// Match credentials to Presentation Definition, to resolve map with InputDescriptorId -> CredentialValue
submissionVCs, inputDescriptorMappingObjects, err := serviceDefinition.PresentationDefinition.Match(matchingVP.VerifiableCredential)
var fields map[string]interface{}
if err != nil {
log.Logger().Infof("Search() is unable to build submission for VP '%s': %s", matchingVP.ID, err)
} else {
credentialMap := make(map[string]vc.VerifiableCredential)
for i := 0; i < len(inputDescriptorMappingObjects); i++ {
credentialMap[inputDescriptorMappingObjects[i].Id] = submissionVCs[i]
}
fields, err = serviceDefinition.PresentationDefinition.ResolveConstraintsFields(credentialMap)
if err != nil {
log.Logger().Infof("Search() is unable to resolve Input Descriptor Constraints Fields map for VP '%s': %s", matchingVP.ID, err)
}
}

result = append(result, SearchResult{
VP: matchingVP,
// TODO: TokenIntrospection is also missing map[InputDescriptorId]CredentialValue ?
//Fields: matchingVP.Fields,
VP: matchingVP,
Fields: fields,
})
}
return result, nil
Expand Down
9 changes: 6 additions & 3 deletions discovery/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package discovery

import (
"encoding/json"
"errors"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/nuts-foundation/go-did/vc"
Expand Down Expand Up @@ -297,12 +298,14 @@ func TestModule_Search(t *testing.T) {
"credentialSubject.id": aliceDID.String(),
})
assert.NoError(t, err)
assert.Equal(t, []SearchResult{
expectedJSON, _ := json.Marshal([]SearchResult{
{
VP: vpAlice,
Fields: nil,
Fields: map[string]interface{}{"issuer_field": authorityDID},
},
}, results)
})
actualJSON, _ := json.Marshal(results)
assert.JSONEq(t, string(expectedJSON), string(actualJSON))
})
t.Run("not a client for this service ID", func(t *testing.T) {
m, _ := setupModule(t, storageEngine)
Expand Down
2 changes: 2 additions & 0 deletions discovery/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var testServiceID = "usecase_v1"

func testDefinitions() map[string]ServiceDefinition {
issuerPattern := "did:example:*"
issuerFieldID := "issuer_field"
return map[string]ServiceDefinition{
testServiceID: {
ID: testServiceID,
Expand All @@ -61,6 +62,7 @@ func testDefinitions() map[string]ServiceDefinition {
Constraints: &pe.Constraints{
Fields: []pe.Field{
{
Id: &issuerFieldID,
Path: []string{"$.issuer"},
Filter: &pe.Filter{
Type: "string",
Expand Down
3 changes: 0 additions & 3 deletions docs/_static/discovery/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ components:
fields:
type: object
description: Input descriptor IDs and their mapped values that from the Verifiable Credential.
additionalProperties:
type: string
description: The value of the field that matched the query.
securitySchemes:
jwtBearerAuth:
type: http
Expand Down

0 comments on commit e9269c4

Please sign in to comment.