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 450cbd1
Showing
7 changed files
with
297 additions
and
116 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,70 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package cmdescriptor | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/cm" | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/presexch" | ||
"io" | ||
"io/ioutil" | ||
) | ||
|
||
// CMAttachmentDescriptors defines the part of properties of credential manifest | ||
type CMAttachmentDescriptors struct { | ||
OutputDesc []*cm.OutputDescriptor `json:"output_descriptor,omitempty"` | ||
InputDesc []*presexch.InputDescriptor `json:"input_descriptor,omitempty"` | ||
Options map[string]string `json:"options,omitempty"` | ||
} | ||
|
||
type Provider struct { | ||
cmDescriptors map[string]*CMAttachmentDescriptors | ||
} | ||
|
||
// New return new provider for presentation exchange. | ||
func New(cmDescriptorsFile io.Reader) (*Provider, error) { | ||
p := &Provider{ | ||
cmDescriptors: map[string]*CMAttachmentDescriptors{}, | ||
} | ||
data, err := ioutil.ReadAll(cmDescriptorsFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read credential manifest descriptors file : %w", err) | ||
} | ||
|
||
if err := json.Unmarshal(data, &p.cmDescriptors); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal 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 | ||
} | ||
|
||
func (p *Provider) FetchCMDescriptorsByScope(scope string) (*CMAttachmentDescriptors, bool) { | ||
if _, ok := p.cmDescriptors[scope]; !ok { | ||
return nil, false | ||
} | ||
|
||
return p.cmDescriptors[scope],true | ||
} |
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,102 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package cmdescriptor | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"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 = `{ | ||
"udc-scope-1":{ | ||
"output_descriptor":[ | ||
{ | ||
"id":"udc_output", | ||
"schema":"https://www.w3.org/2018/credentials/examples/v1" | ||
} | ||
], | ||
"input_descriptor":[ | ||
{ | ||
"testing":"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 read credential manifest descriptors file", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, err := New(&mockReader{err: errors.New("test")}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "failed to read credential manifest descriptors file") | ||
}) | ||
|
||
t.Run("test failed to unmarshal 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 unmarshal 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(cmDescData))) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "aries-framework - failed to validate input descriptors") | ||
require.Nil(t, cmDesc) | ||
}) | ||
} | ||
|
||
func reader(t *testing.T, jsn interface{}) io.Reader { | ||
t.Helper() | ||
|
||
bits, err := json.Marshal(jsn) | ||
require.NoError(t, err) | ||
|
||
return bytes.NewReader(bits) | ||
} | ||
|
||
type mockReader struct { | ||
err error | ||
} | ||
|
||
func (m *mockReader) Read([]byte) (int, error) { | ||
return 0, m.err | ||
} |
Oops, something went wrong.