Skip to content

Commit

Permalink
feat: [WACI-Issuance] Support to pass credential output descriptors
Browse files Browse the repository at this point in the history
closes trustbloc#561

Signed-off-by: talwinder50 <[email protected]>
  • Loading branch information
talwinder50 committed Jan 19, 2022
1 parent 5d5deae commit 07943e7
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 628 deletions.
2 changes: 1 addition & 1 deletion cmd/adapter-rest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/hyperledger/aries-framework-go v0.1.8-0.20220106195936-a9d6794663ed
github.com/hyperledger/aries-framework-go v0.1.8-0.20220119075823-4b3b1a0e69eb
github.com/hyperledger/aries-framework-go-ext/component/storage/mongodb v0.0.0-20211117223600-626fe1bae44d
github.com/hyperledger/aries-framework-go-ext/component/storage/mysql v0.0.0-20210909220549-ce3a2ee13e22
github.com/hyperledger/aries-framework-go/component/storageutil v0.0.0-20220106195936-a9d6794663ed
Expand Down
4 changes: 2 additions & 2 deletions cmd/adapter-rest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,8 @@ github.com/hyperledger/aries-framework-go v0.1.7-0.20210816113201-26c0665ef2b9/g
github.com/hyperledger/aries-framework-go v0.1.7/go.mod h1:uve8/q23AUnq4EM0vBkEr1lKZRC67q5RcaHXh0ZOt0Y=
github.com/hyperledger/aries-framework-go v0.1.8-0.20211201185059-733a3370f501/go.mod h1:uve8/q23AUnq4EM0vBkEr1lKZRC67q5RcaHXh0ZOt0Y=
github.com/hyperledger/aries-framework-go v0.1.8-0.20211217135421-f68d5698237a/go.mod h1:rBMOJVwyHyYbOqbb3IB/ExBkHyvFLht/W81s24GmjcE=
github.com/hyperledger/aries-framework-go v0.1.8-0.20220106195936-a9d6794663ed h1:1n1vlQHLG9yBI40w9sY6hBzqSkYthARexSEU0rHfZS8=
github.com/hyperledger/aries-framework-go v0.1.8-0.20220106195936-a9d6794663ed/go.mod h1:rBMOJVwyHyYbOqbb3IB/ExBkHyvFLht/W81s24GmjcE=
github.com/hyperledger/aries-framework-go v0.1.8-0.20220119075823-4b3b1a0e69eb h1:SC5gKf0+VSHRoyg72k+syZJpz592vEsID6xX8kfTH+M=
github.com/hyperledger/aries-framework-go v0.1.8-0.20220119075823-4b3b1a0e69eb/go.mod h1:rBMOJVwyHyYbOqbb3IB/ExBkHyvFLht/W81s24GmjcE=
github.com/hyperledger/aries-framework-go-ext/component/storage/couchdb v0.0.0-20210909220549-ce3a2ee13e22/go.mod h1:FtlFhPHMyLORgrPpvWSmEQSNhLiwAQ4V6rqNPfuDj0o=
github.com/hyperledger/aries-framework-go-ext/component/storage/mongodb v0.0.0-20210909220549-ce3a2ee13e22/go.mod h1:aiO9mXZBykIEwmgp9sSdpMuTw0P7b+ZFUltcIB9ZccY=
github.com/hyperledger/aries-framework-go-ext/component/storage/mongodb v0.0.0-20211117223600-626fe1bae44d h1:h91rxhZkAjxcIwY08RxUTE+B8WxfiWbRHNl5X98+ziA=
Expand Down
72 changes: 58 additions & 14 deletions cmd/adapter-rest/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package startcmd
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -33,6 +34,7 @@ import (
ldrest "github.com/hyperledger/aries-framework-go/pkg/controller/rest/ld"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/messaging/msghandler"
arieshttp "github.com/hyperledger/aries-framework-go/pkg/didcomm/transport/http"
"github.com/hyperledger/aries-framework-go/pkg/doc/cm"
ariesld "github.com/hyperledger/aries-framework-go/pkg/doc/ld"
"github.com/hyperledger/aries-framework-go/pkg/doc/ldcontext/remote"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries"
Expand Down Expand Up @@ -98,6 +100,12 @@ const (
"Alternatively, this can be set with the following environment variable: " + staticFilesPathEnvKey
staticFilesPathEnvKey = "ADAPTER_REST_STATIC_FILES"

cmOutputDescriptorsFilePathFlagName = "output-descriptors-path"
cmOutputDescriptorsFilePathFlagUsage = "Path to the output descriptors file of credential manifests " +
"supported by the adapter'" + "Alternatively, this can be set with the following " +
"environment variable: " + cmOutputDescriptorsFilePathEnvKey
cmOutputDescriptorsFilePathEnvKey = "ADAPTER_REST_OUTPUT_DESCRIPTORS_FILE"

tlsSystemCertPoolFlagName = "tls-systemcertpool"
tlsSystemCertPoolFlagUsage = "Use system certificate pool." +
" Possible values [true] [false]. Defaults to false if not set." +
Expand Down Expand Up @@ -265,18 +273,19 @@ type adapterRestParameters struct {
staticFiles string
presentationDefinitionsFile string
// TODO assuming same base path for all hydra endpoints for now
hydraURL string
mode string
didCommParameters *didCommParameters // didcomm
trustblocDomain string
universalResolverURL string
governanceVCSURL string
requestTokens map[string]string
walletAppURL string
oidcClientDBKeyPath string
externalURL string
didAnchorOrigin string
contextProviderURLs []string
hydraURL string
mode string
didCommParameters *didCommParameters // didcomm
trustblocDomain string
universalResolverURL string
governanceVCSURL string
requestTokens map[string]string
walletAppURL string
oidcClientDBKeyPath string
externalURL string
didAnchorOrigin string
contextProviderURLs []string
cmOutputDescriptorsFilePath string
}

// governanceProvider governance provider.
Expand Down Expand Up @@ -358,6 +367,12 @@ func getAdapterRestParameters(cmd *cobra.Command) (*adapterRestParameters, error
return nil, fmt.Errorf(confErrMsg, err)
}

outputDescriptorsFilePath, err := cmdutils.GetUserSetVarFromString(cmd, cmOutputDescriptorsFilePathFlagName,
cmOutputDescriptorsFilePathEnvKey, true)
if err != nil {
return nil, fmt.Errorf(confErrMsg, err)
}

mode, err := cmdutils.GetUserSetVarFromString(cmd, modeFlagName, modeEnvKey, true)
if err != nil {
return nil, fmt.Errorf(confErrMsg, err)
Expand Down Expand Up @@ -452,6 +467,7 @@ func getAdapterRestParameters(cmd *cobra.Command) (*adapterRestParameters, error
externalURL: externalURL,
didAnchorOrigin: didAnchorOrigin,
contextProviderURLs: contextProviderURLs,
cmOutputDescriptorsFilePath: outputDescriptorsFilePath,
}, nil
}

Expand Down Expand Up @@ -609,6 +625,7 @@ func createFlags(startCmd *cobra.Command) {
startCmd.Flags().StringP(datasourceNameFlagName, "", "", datasourceNameFlagUsage)
startCmd.Flags().StringP(datasourceTimeoutFlagName, "", "", datasourceTimeoutFlagUsage)
startCmd.Flags().StringP(staticFilesPathFlagName, "", "", staticFilesPathFlagUsage)
startCmd.Flags().StringP(cmOutputDescriptorsFilePathFlagName, "", "", cmOutputDescriptorsFilePathFlagUsage)
startCmd.Flags().StringP(presentationDefinitionsFlagName, "", "", presentationDefinitionsFlagUsage)
startCmd.Flags().StringP(hydraURLFlagName, "", "", hydraURLFlagUsage)
startCmd.Flags().StringP(modeFlagName, "", "", modeFlagUsage)
Expand Down Expand Up @@ -794,7 +811,7 @@ func addRPHandlers(parameters *adapterRestParameters, framework *aries.Aries, ro
return nil
}

// nolint:funlen
// nolint:funlen,gocyclo,cyclop
func addIssuerHandlers(parameters *adapterRestParameters, framework *aries.Aries, router *mux.Router,
rootCAs *x509.CertPool, msgRegistrar *msghandler.Registrar) error {
store, err := initStore(parameters.dsnParams.dsn, parameters.dsnParams.timeout, issuerAdapterStorePrefix)
Expand All @@ -818,6 +835,11 @@ func addIssuerHandlers(parameters *adapterRestParameters, framework *aries.Aries
if err != nil {
return fmt.Errorf("aries-framework - failed to get aries context : %w", err)
}
// TODO #572 Pass the output descriptors to issuer
_, err = readCMOutputDescriptorFile(parameters.cmOutputDescriptorsFilePath)
if err != nil {
return fmt.Errorf("failed to read and validate manifest output descriptors : %w", err)
}

clientStoreKey, err := getIssuerOIDCClientStoreKey(parameters.oidcClientDBKeyPath)
if err != nil {
Expand All @@ -835,7 +857,7 @@ func addIssuerHandlers(parameters *adapterRestParameters, framework *aries.Aries
if err != nil {
return fmt.Errorf("failed to init trustbloc did creator: %w", err)
}

// TODO #572 Pass the output descriptors to issuer
// add issuer endpoints
issuerService, err := issuer.New(&issuerops.Config{
AriesCtx: ariesCtx,
Expand Down Expand Up @@ -1116,3 +1138,25 @@ func getPresentationExchangeProvider(configFile string) (*presentationex.Provide

return p, nil
}

func readCMOutputDescriptorFile(outputDescriptorsFile string) (cmOutputDescriptors map[string][]*cm.OutputDescriptor,
err error) {
credentialManifestBytes, err := ioutil.ReadFile(filepath.Clean(outputDescriptorsFile))
if err != nil {
return nil, fmt.Errorf("read output descriptors file : %w", err)
}

err = json.Unmarshal(credentialManifestBytes, &cmOutputDescriptors)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal output descriptor file: %w", err)
}

for _, outputDescriptorsValues := range cmOutputDescriptors {
err = cm.Validate(outputDescriptorsValues)
if err != nil {
return nil, fmt.Errorf("aries-framework - failed to validate output descriptors: %w", err)
}
}

return cmOutputDescriptors, nil
}
57 changes: 54 additions & 3 deletions cmd/adapter-rest/startcmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ func TestStartCmdDIDComm(t *testing.T) { // nolint:paralleltest // shared enviro
"--" + datasourceNameFlagName, "mem://test",
"--" + datasourceTimeoutFlagName, "30",
"--" + issuerOIDCClientStoreKeyFlagName, file.Name(),
"--" + cmOutputDescriptorsFilePathFlagName, "./testdata/outputdescriptors.json",
}
startCmd.SetArgs(args)

Expand Down Expand Up @@ -419,6 +420,7 @@ func TestAdapterModes(t *testing.T) { // nolint:paralleltest // shared environme
"--" + datasourceTimeoutFlagName, "30",
"--" + governanceVCSURLFlagName, "http://example.vcs.com",
"--" + issuerOIDCClientStoreKeyFlagName, file.Name(),
"--" + cmOutputDescriptorsFilePathFlagName, "./testdata/outputdescriptors.json",
}
startCmd.SetArgs(args)

Expand Down Expand Up @@ -506,6 +508,7 @@ func TestAdapterModes(t *testing.T) { // nolint:paralleltest // shared environme
"--" + datasourceTimeoutFlagName, "30",
"--" + governanceVCSURLFlagName, "http://example.vcs.com",
"--" + issuerOIDCClientStoreKeyFlagName, file.Name() + "-nonexistent",
"--" + cmOutputDescriptorsFilePathFlagName, "./testdata/outputdescriptors.json",
}
startCmd.SetArgs(args)

Expand All @@ -514,6 +517,33 @@ func TestAdapterModes(t *testing.T) { // nolint:paralleltest // shared environme
require.Contains(t, err.Error(), "failed to read key")
})

t.Run("test adapter mode - failed to read the cm output descriptor file", func(t *testing.T) { // nolint:paralleltest,lll // shared environment variables
startCmd := GetStartCmd(&mockServer{})

testInboundHostURL := randomURL()

file, err := ioutil.TempFile("", "*.key")
require.NoError(t, err)

defer func() { require.NoError(t, os.Remove(file.Name())) }()

args := []string{
"--" + modeFlagName, issuerMode,
"--" + hostURLFlagName, "localhost:8080",
"--" + didCommInboundHostFlagName, testInboundHostURL,
"--" + datasourceNameFlagName, "mem://test",
"--" + datasourceTimeoutFlagName, "30",
"--" + governanceVCSURLFlagName, "http://example.vcs.com",
"--" + issuerOIDCClientStoreKeyFlagName, file.Name() + "-nonexistent",
"--" + cmOutputDescriptorsFilePathFlagName, "invalidFile",
}
startCmd.SetArgs(args)

err = startCmd.Execute()
require.Error(t, err)
require.Contains(t, err.Error(), "failed to read and validate manifest output descriptors")
})

t.Run("test adapter mode - wallet handler errors", func(t *testing.T) { // nolint:paralleltest,lll // shared environment variables
file, err := ioutil.TempFile("", "*.key")
require.NoError(t, err)
Expand All @@ -531,8 +561,9 @@ func TestAdapterModes(t *testing.T) { // nolint:paralleltest // shared environme
dsnParams: &dsnParams{
dsn: "mem://test",
},
didCommParameters: &didCommParameters{},
oidcClientDBKeyPath: file.Name(),
didCommParameters: &didCommParameters{},
oidcClientDBKeyPath: file.Name(),
cmOutputDescriptorsFilePath: "./testdata/outputdescriptors.json",
}

issuerAries, err := aries.New(aries.WithStoreProvider(&storage.MockStoreProvider{
Expand Down Expand Up @@ -567,7 +598,27 @@ func TestAdapterModes(t *testing.T) { // nolint:paralleltest // shared environme
require.Contains(t, err.Error(), "failed to initialize wallet bridge")
})
}

func TestReadOutputDescriptorFile(t *testing.T) { // nolint:paralleltest
t.Run("read output descriptor file success", // nolint:paralleltest
func(t *testing.T) {
cmOutputdesc, err := readCMOutputDescriptorFile("./testdata/outputdescriptors.json")
require.NoError(t, err)
require.NotNil(t, cmOutputdesc)
for k, v := range cmOutputdesc {
require.Equal(t, "udc-scope-1", k)
require.Equal(t, 1, len(cmOutputdesc))
require.NotNil(t, v)
}
})
t.Run("no such output descriptor file", // nolint:paralleltest
func(t *testing.T) {
cmOutputdesc, err := readCMOutputDescriptorFile("./testingWrongFile")
require.Error(t, err)
require.Equal(t, "read output descriptors file : open testingWrongFile: no such file or directory",
err.Error())
require.Nil(t, cmOutputdesc)
})
}
func TestTLSSystemCertPoolInvalidArgsEnvVar(t *testing.T) { // nolint:paralleltest // shared environment variables
startCmd := GetStartCmd(&mockServer{})

Expand Down
72 changes: 72 additions & 0 deletions cmd/adapter-rest/startcmd/testdata/outputdescriptors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"udc-scope-1": [
{
"id": "udc_output",
"schema": "https://www.w3.org/2018/credentials/examples/v1",
"display": {
"title": {
"path": [
"$.title",
"$.vc.title"
],
"schema": {
"type": "string"
},
"fallback": "Bachelor's Degree"
},
"subtitle": {
"path": [
"$.minor",
"$.vc.minor"
],
"schema": {
"type": "string"
},
"fallback": ""
},
"description": {
"text": "Awarded for completing a four year program at Example University."
},
"properties": [
{
"path": [
"$.name",
"$.credentialSubject.name"
],
"schema": {
"type": "string"
},
"fallback": "Not Applicable",
"label": "Degree Holder's name"
},
{
"path": [
"$.credentialSubject.degree.type"
],
"schema": {
"type": "string"
},
"fallback": "Unknown",
"label": "Degree"
}
]
},
"styles": {
"thumbnail": {
"uri": "http://example-university.org/logo.png",
"alt": "Example University logo"
},
"hero": {
"uri": "http://example-university.org/hero.png",
"alt": "Example University students in graduation ceremony"
},
"background": {
"color": "#ff0000"
},
"text": {
"color": "#d4d400"
}
}
}
]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/hyperledger/aries-framework-go v0.1.8-0.20220106195936-a9d6794663ed
github.com/hyperledger/aries-framework-go v0.1.8-0.20220119075823-4b3b1a0e69eb
github.com/hyperledger/aries-framework-go-ext/component/vdr/orb v0.1.4-0.20211219215001-23cd75276fdc
github.com/hyperledger/aries-framework-go/component/storageutil v0.0.0-20220106195936-a9d6794663ed
github.com/hyperledger/aries-framework-go/spi v0.0.0-20220106195936-a9d6794663ed
Expand Down
Loading

0 comments on commit 07943e7

Please sign in to comment.