forked from trustbloc/adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Refactor credential manifest output descriptors
closes trustbloc#580 Signed-off-by: talwinder50 <talwinderkaur50@gmail.com>
- Loading branch information
1 parent
869218a
commit 40b5615
Showing
7 changed files
with
331 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package memcmdescriptor | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/cm" | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/presexch" | ||
) | ||
|
||
// CMAttachmentDescriptors defines the part of properties of credential manifest | ||
type CMAttachmentDescriptors struct { | ||
OutputDesc []*cm.OutputDescriptor `json:"output_descriptor,omitempty"` | ||
// TODO [#Issue-612] Support for submission requirement will put whole presentation_definition here | ||
// instead of input_descriptor | ||
InputDesc []*presexch.InputDescriptor `json:"input_descriptor,omitempty"` | ||
Options map[string]string `json:"options,omitempty"` | ||
} | ||
|
||
// Provider provide credential attachment descriptors ops. | ||
type Provider struct { | ||
cmDescriptors map[string]*CMAttachmentDescriptors | ||
} | ||
|
||
// New return new provider for credential manifest descriptor provider. | ||
func New(cmDescriptorsFile io.Reader) (*Provider, error) { | ||
p := &Provider{ | ||
cmDescriptors: map[string]*CMAttachmentDescriptors{}, | ||
} | ||
|
||
err := json.NewDecoder(cmDescriptorsFile).Decode(&p.cmDescriptors) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode credential manifest descriptors file: %w", err) | ||
} | ||
|
||
for _, descriptors := range p.cmDescriptors { | ||
err := cm.ValidateOutputDescriptors(descriptors.OutputDesc) | ||
if err != nil { | ||
return nil, fmt.Errorf("aries-framework - failed to validate output "+ | ||
"descriptors: %w", err) | ||
} | ||
|
||
if descriptors.InputDesc != nil { | ||
presDef := presexch.PresentationDefinition{ID: uuid.NewString(), InputDescriptors: descriptors.InputDesc} | ||
|
||
err := presDef.ValidateSchema() | ||
if err != nil { | ||
return nil, fmt.Errorf("aries-framework - failed to validate input "+ | ||
"descriptors: %w", err) | ||
} | ||
} | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
// FetchCMDescriptorsByScope allows to fetch the descriptor by scope | ||
func (p *Provider) FetchCMDescriptorsByScope(scope string) (*CMAttachmentDescriptors, bool) { | ||
descriptor, ok := p.cmDescriptors[scope] | ||
|
||
return descriptor, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package memcmdescriptor | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// nolint:gochecknoglobals | ||
var cmOutDescData = `{ | ||
"udc-scope": { | ||
"output_descriptor": [{ | ||
"uri": "https://trustbloc.github.io/context/vc/examples-ext-v1.jsonld" | ||
}] | ||
} | ||
}` | ||
|
||
// nolint:gochecknoglobals | ||
var cmDescData = ` | ||
{ | ||
"prc":{ | ||
"output_descriptor":[ | ||
{ | ||
"id":"udc_output", | ||
"schema":"https://www.w3.org/2018/credentials/examples/v1" | ||
} | ||
], | ||
"input_descriptor":[ | ||
{ | ||
"id":"prc_input", | ||
"schema":[ | ||
{ | ||
"uri":"https://w3id.org/citizenship#PermanentResidentCard" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
` | ||
|
||
// nolint:gochecknoglobals | ||
var invalidCMDescData = ` | ||
{ | ||
"prc":{ | ||
"output_descriptor":[ | ||
{ | ||
"id":"udc_output", | ||
"schema":"https://www.w3.org/2018/credentials/examples/v1" | ||
} | ||
], | ||
"input_descriptor":[ | ||
{ | ||
"id":"prc_input" | ||
} | ||
] | ||
} | ||
} | ||
` | ||
|
||
func TestProvider_New(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("test success", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
p, err := New(reader(t, map[string]*Provider{})) | ||
require.NoError(t, err) | ||
require.NotNil(t, p) | ||
}) | ||
|
||
t.Run("test failed to decode credential manifest descriptors file", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, err := New(bytes.NewReader([]byte("{"))) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "failed to decode credential manifest descriptors file") | ||
}) | ||
t.Run("aries-framework - failed to validate output descriptors", // nolint:paralleltest | ||
func(t *testing.T) { | ||
cmOutputdesc, err := New(bytes.NewReader([]byte(cmOutDescData))) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "aries-framework - failed to validate output descriptors: "+ | ||
"missing ID for output descriptor") | ||
require.Nil(t, cmOutputdesc) | ||
}) | ||
t.Run("aries-framework - failed to validate input descriptors", // nolint:paralleltest | ||
func(t *testing.T) { | ||
cmDesc, err := New(bytes.NewReader([]byte(invalidCMDescData))) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "aries-framework - failed to validate input descriptors") | ||
require.Nil(t, cmDesc) | ||
}) | ||
} | ||
|
||
func TestFetchCMDescriptorsByScope(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("fetch cm descriptor success", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
p, err := New(bytes.NewReader([]byte(cmDescData))) | ||
require.NoError(t, err) | ||
require.NotNil(t, p) | ||
|
||
val, found := p.FetchCMDescriptorsByScope("prc") | ||
require.True(t, found) | ||
require.NotNil(t, val) | ||
}) | ||
t.Run("fetch cm descriptor not found", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
p, err := New(reader(t, map[string]*Provider{})) | ||
require.NoError(t, err) | ||
require.NotNil(t, p) | ||
|
||
val, found := p.FetchCMDescriptorsByScope("prc") | ||
require.False(t, found) | ||
require.Nil(t, val) | ||
}) | ||
} | ||
|
||
func reader(t *testing.T, jsn interface{}) io.Reader { | ||
t.Helper() | ||
|
||
bits, err := json.Marshal(jsn) | ||
require.NoError(t, err) | ||
|
||
return bytes.NewReader(bits) | ||
} |
Oops, something went wrong.