From f7a0dc3a00bd4c8139e8c89e25d027e9093928a9 Mon Sep 17 00:00:00 2001 From: Filip Burlacu Date: Mon, 12 Sep 2022 14:41:16 -0400 Subject: [PATCH] feat: fix format filtering in pEx V2 for JWTVCs, and add bdd tests (#3368) - also fix an issue with initializing sdk agents, when old clients still exist under the same name - also rename wallet-jsonld bdd tests since they aren't jsonld-only Signed-off-by: Filip Burlacu --- pkg/doc/presexch/definition.go | 9 ++- test/bdd/agent/agent_sdk_steps.go | 3 + test/bdd/bddtests_test.go | 4 +- ...allet_jsonld.feature => vc_wallet.feature} | 4 +- test/bdd/pkg/context/context.go | 40 ++++++++++- .../pkg/{walletjsonld => vcwallet}/agent.go | 2 +- .../{walletjsonld => vcwallet}/credential.go | 2 +- .../bdd/pkg/{walletjsonld => vcwallet}/did.go | 2 +- .../presentation.go | 13 +++- test/bdd/pkg/vcwallet/query.go | 72 +++++++++++++++++++ .../pkg/{walletjsonld => vcwallet}/signer.go | 2 +- .../testdata/presentation_definition_fmt.json | 0 .../testdata/presentation_definition_v2.json | 22 ++++++ .../testdata/query_by_example_fmt.json | 0 .../pkg/{walletjsonld => vcwallet}/wallet.go | 2 +- .../wallet_steps.go | 4 +- test/bdd/pkg/walletjsonld/query.go | 33 --------- 17 files changed, 165 insertions(+), 49 deletions(-) rename test/bdd/features/{wallet_jsonld.feature => vc_wallet.feature} (95%) rename test/bdd/pkg/{walletjsonld => vcwallet}/agent.go (95%) rename test/bdd/pkg/{walletjsonld => vcwallet}/credential.go (99%) rename test/bdd/pkg/{walletjsonld => vcwallet}/did.go (99%) rename test/bdd/pkg/{walletjsonld => vcwallet}/presentation.go (89%) create mode 100644 test/bdd/pkg/vcwallet/query.go rename test/bdd/pkg/{walletjsonld => vcwallet}/signer.go (96%) rename test/bdd/pkg/{walletjsonld => vcwallet}/testdata/presentation_definition_fmt.json (100%) create mode 100644 test/bdd/pkg/vcwallet/testdata/presentation_definition_v2.json rename test/bdd/pkg/{walletjsonld => vcwallet}/testdata/query_by_example_fmt.json (100%) rename test/bdd/pkg/{walletjsonld => vcwallet}/wallet.go (99%) rename test/bdd/pkg/{walletjsonld => vcwallet}/wallet_steps.go (97%) delete mode 100644 test/bdd/pkg/walletjsonld/query.go diff --git a/pkg/doc/presexch/definition.go b/pkg/doc/presexch/definition.go index 485459d9d..41a171575 100644 --- a/pkg/doc/presexch/definition.go +++ b/pkg/doc/presexch/definition.go @@ -89,6 +89,11 @@ type Format struct { LdpVP *LdpType `json:"ldp_vp,omitempty"` } +func (f *Format) notNil() bool { + return f != nil && + (f.Jwt != nil || f.JwtVC != nil || f.JwtVP != nil || f.Ldp != nil || f.LdpVC != nil || f.LdpVP != nil) +} + // JwtType contains alg. type JwtType struct { Alg []string `json:"alg,omitempty"` @@ -383,7 +388,7 @@ func (pd *PresentationDefinition) applyRequirement(req *requirement, creds []*ve for _, descriptor := range req.InputDescriptors { format := pd.Format - if descriptor.Format != nil { + if descriptor.Format.notNil() { format = descriptor.Format } @@ -394,7 +399,7 @@ func (pd *PresentationDefinition) applyRequirement(req *requirement, creds []*ve return "", nil, err } - if format != nil { + if format.notNil() { vpFormat, filtered = filterFormat(format, filtered) } diff --git a/test/bdd/agent/agent_sdk_steps.go b/test/bdd/agent/agent_sdk_steps.go index 40f631eb0..ae148c4fd 100644 --- a/test/bdd/agent/agent_sdk_steps.go +++ b/test/bdd/agent/agent_sdk_steps.go @@ -616,6 +616,9 @@ func (a *SDKSteps) createFramework(agentID string, autoTrigger bool, opts ...ari return fmt.Errorf("failed to create context: %w", err) } + // TODO: clear bddcontext agent data for given agent name before initializing new one + a.bddContext.DeleteSDKAgent(agentID) + a.bddContext.Agents[agentID] = agent a.bddContext.AgentCtx[agentID] = ctx a.bddContext.Messengers[agentID] = agent.Messenger() diff --git a/test/bdd/bddtests_test.go b/test/bdd/bddtests_test.go index 404c0c34b..646609ad5 100644 --- a/test/bdd/bddtests_test.go +++ b/test/bdd/bddtests_test.go @@ -34,10 +34,10 @@ import ( "github.com/hyperledger/aries-framework-go/test/bdd/pkg/outofband" "github.com/hyperledger/aries-framework-go/test/bdd/pkg/presentproof" "github.com/hyperledger/aries-framework-go/test/bdd/pkg/rfc0593" + "github.com/hyperledger/aries-framework-go/test/bdd/pkg/vcwallet" "github.com/hyperledger/aries-framework-go/test/bdd/pkg/vdr" "github.com/hyperledger/aries-framework-go/test/bdd/pkg/verifiable" "github.com/hyperledger/aries-framework-go/test/bdd/pkg/waci" - "github.com/hyperledger/aries-framework-go/test/bdd/pkg/walletjsonld" "github.com/hyperledger/aries-framework-go/test/bdd/pkg/webkms" ) @@ -219,7 +219,7 @@ func features() []feature { webkms.NewCryptoSDKSteps(), waci.NewIssuanceDIDCommV1SDKSteps(), waci.NewIssuanceDIDCommV2SDKSteps(), - walletjsonld.NewSDKSteps(), + vcwallet.NewSDKSteps(), legacyconnection.NewLegacyConnectionControllerSteps(), } } diff --git a/test/bdd/features/wallet_jsonld.feature b/test/bdd/features/vc_wallet.feature similarity index 95% rename from test/bdd/features/wallet_jsonld.feature rename to test/bdd/features/vc_wallet.feature index a418c7bee..e3bdf2162 100644 --- a/test/bdd/features/wallet_jsonld.feature +++ b/test/bdd/features/vc_wallet.feature @@ -17,7 +17,7 @@ Feature: Verifiable Credential flows in universal wallet SDK And "Berkley" issues "" credentials at "2022-04-12" regarding "Master Degree" to "Alice" Then "Alice" adds credentials to the wallet issued by "Berkley" And "Alice" verifies credential issued by "Berkley" - When "Vanna" queries credentials issued by "Berkley" using "" query type + When "Vanna" queries "" credentials issued by "Berkley" using "" query type Then "Alice" resolves query And "Alice" adds "" presentations proof And "Alice" closes wallet @@ -43,7 +43,7 @@ Feature: Verifiable Credential flows in universal wallet SDK And "Alice" issues "" credentials using the wallet Then "Alice" adds credentials to the wallet issued by "Alice" And "Alice" verifies credential issued by "Alice" - When "Vanna" queries credentials issued by "Alice" using "PresentationExchange" query type + When "Vanna" queries "" credentials issued by "Alice" using "" query type And "Alice" resolves query And "Alice" adds "" presentations proof And "Alice" closes wallet diff --git a/test/bdd/pkg/context/context.go b/test/bdd/pkg/context/context.go index 75918dbd8..19f584718 100644 --- a/test/bdd/pkg/context/context.go +++ b/test/bdd/pkg/context/context.go @@ -38,7 +38,7 @@ type BDDContext struct { RouteCallbacks map[string]chan interface{} PublicDIDDocs map[string]*did.Doc PublicKeys map[string]*jwk.JWK - PublicEncKeys map[string][]byte + PublicEncKeys map[string][]byte // TODO: PublicEndKeys values are never set. KeyHandles map[string]interface{} PublicDIDs map[string]string PeerDIDs map[string]string @@ -97,6 +97,44 @@ func (b *BDDContext) Destroy() { } } +// DeleteSDKAgent deletes an SDK agent, clearing all of its BDDContext data. +func (b *BDDContext) DeleteSDKAgent(agentName string) { + b.lock.Lock() + defer b.lock.Unlock() + + if conn, ok := b.webSocketConns[agentName]; ok { + err := conn.Close(websocket.StatusNormalClosure, "bddtests destroy context") + if err != nil { + logger.Warnf("failed to close websocket connection for [%s] : %v", agentName, err) + } + + delete(b.webSocketConns, agentName) + } + + if agent, ok := b.Agents[agentName]; ok { + if err := agent.Close(); err != nil { + logger.Warnf("failed to teardown aries framework : %s", err.Error()) + } + + delete(b.Agents, agentName) + } + + delete(b.OutOfBandClients, agentName) + delete(b.OutOfBandV2Clients, agentName) + delete(b.DIDExchangeClients, agentName) + delete(b.RouteClients, agentName) + delete(b.RouteCallbacks, agentName) + delete(b.PublicDIDDocs, agentName) + delete(b.PublicKeys, agentName) + delete(b.PublicEncKeys, agentName) + delete(b.KeyHandles, agentName) + delete(b.PublicDIDs, agentName) + delete(b.PeerDIDs, agentName) + delete(b.Messengers, agentName) + delete(b.ConnectionIDs, agentName) + delete(b.AgentCtx, agentName) +} + // RegisterWebhookURL registers given url to agent id for webhook. func (b *BDDContext) RegisterWebhookURL(agentID, url string) { b.lock.Lock() diff --git a/test/bdd/pkg/walletjsonld/agent.go b/test/bdd/pkg/vcwallet/agent.go similarity index 95% rename from test/bdd/pkg/walletjsonld/agent.go rename to test/bdd/pkg/vcwallet/agent.go index 72896fbca..e16e7b286 100644 --- a/test/bdd/pkg/walletjsonld/agent.go +++ b/test/bdd/pkg/vcwallet/agent.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package walletjsonld +package vcwallet import ( bddagent "github.com/hyperledger/aries-framework-go/test/bdd/agent" diff --git a/test/bdd/pkg/walletjsonld/credential.go b/test/bdd/pkg/vcwallet/credential.go similarity index 99% rename from test/bdd/pkg/walletjsonld/credential.go rename to test/bdd/pkg/vcwallet/credential.go index 35e85d94a..dfd353160 100644 --- a/test/bdd/pkg/walletjsonld/credential.go +++ b/test/bdd/pkg/vcwallet/credential.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package walletjsonld +package vcwallet import ( "encoding/json" diff --git a/test/bdd/pkg/walletjsonld/did.go b/test/bdd/pkg/vcwallet/did.go similarity index 99% rename from test/bdd/pkg/walletjsonld/did.go rename to test/bdd/pkg/vcwallet/did.go index a7535ffb3..545447058 100644 --- a/test/bdd/pkg/walletjsonld/did.go +++ b/test/bdd/pkg/vcwallet/did.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package walletjsonld +package vcwallet import ( "fmt" diff --git a/test/bdd/pkg/walletjsonld/presentation.go b/test/bdd/pkg/vcwallet/presentation.go similarity index 89% rename from test/bdd/pkg/walletjsonld/presentation.go rename to test/bdd/pkg/vcwallet/presentation.go index 84c575fe9..3fc65f9fe 100644 --- a/test/bdd/pkg/walletjsonld/presentation.go +++ b/test/bdd/pkg/vcwallet/presentation.go @@ -4,23 +4,30 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package walletjsonld +package vcwallet import ( "errors" "fmt" + "strings" "github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" "github.com/hyperledger/aries-framework-go/pkg/wallet" ) func (s *SDKSteps) queryPresentations(_, issuer, rawQueryType string) error { - queryType, err := wallet.GetQueryType(rawQueryType) + return s.queryPresentationWithFormat("", "", issuer, rawQueryType) +} + +func (s *SDKSteps) queryPresentationWithFormat(_, format, issuer, rawQueryType string) error { + queryTypeString := strings.Split(rawQueryType, "-")[0] + + queryType, err := wallet.GetQueryType(queryTypeString) if err != nil { return err } - query, err := s.getQuery(queryType, s.getPublicDID(issuer).ID) + query, err := s.getQuery(rawQueryType, s.getPublicDID(issuer).ID, format) if err != nil { return err } diff --git a/test/bdd/pkg/vcwallet/query.go b/test/bdd/pkg/vcwallet/query.go new file mode 100644 index 000000000..9446cc6d0 --- /dev/null +++ b/test/bdd/pkg/vcwallet/query.go @@ -0,0 +1,72 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package vcwallet + +import ( + _ "embed" //nolint:gci // required for go:embed + "encoding/json" + "fmt" + "strings" +) + +//go:embed testdata/query_by_example_fmt.json +var sampleQueryByExFmt string //nolint:gochecknoglobals + +//go:embed testdata/presentation_definition_fmt.json +var presentationDefinitionFmt string //nolint:gochecknoglobals + +//go:embed testdata/presentation_definition_v2.json +var presentationDefinitionFmtV2 string //nolint:gochecknoglobals + +func (s *SDKSteps) getQuery(queryType, didID, format string) (json.RawMessage, error) { + switch queryType { + case "QueryByExample": + return []byte(fmt.Sprintf(sampleQueryByExFmt, didID)), nil + case "PresentationExchange": + return []byte(fmt.Sprintf(presentationDefinitionFmt, didID)), nil + case "PresentationExchange-v2": + return []byte(pExV2Format(didID, s.crypto, format)), nil + } + + // QueryByFrame is not covered in tests, since BBS isn't usable yet for JWT credentials + return nil, fmt.Errorf("invalid queryType %v", queryType) +} + +const ( + ecdsa = "ECDSA" +) + +func pExV2Format(didID, crypto, format string) string { + formatString := "" + signingAlg := strings.Split(crypto, " ")[0] + + if format == "JWT" { + alg := "" + + switch signingAlg { + case "Ed25519": + alg = "EdDSA" + case ecdsa: + alg = ecdsa + } + + formatString = fmt.Sprintf(`{"jwt": {"alg":["%s"]}}`, alg) + } else if format == "JSON-LD" { + proofType := "" + + switch signingAlg { + case "Ed25519": + proofType = "Ed25519Signature2018" + case ecdsa: + proofType = "JSONWebSignature2020" + } + + formatString = fmt.Sprintf(`{"ldp": {"proof_type":["%s"]}}`, proofType) + } + + return fmt.Sprintf(presentationDefinitionFmtV2, didID, formatString) +} diff --git a/test/bdd/pkg/walletjsonld/signer.go b/test/bdd/pkg/vcwallet/signer.go similarity index 96% rename from test/bdd/pkg/walletjsonld/signer.go rename to test/bdd/pkg/vcwallet/signer.go index bd2a33adc..387a667fd 100644 --- a/test/bdd/pkg/walletjsonld/signer.go +++ b/test/bdd/pkg/vcwallet/signer.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package walletjsonld +package vcwallet import ( ariescrypto "github.com/hyperledger/aries-framework-go/pkg/crypto" diff --git a/test/bdd/pkg/walletjsonld/testdata/presentation_definition_fmt.json b/test/bdd/pkg/vcwallet/testdata/presentation_definition_fmt.json similarity index 100% rename from test/bdd/pkg/walletjsonld/testdata/presentation_definition_fmt.json rename to test/bdd/pkg/vcwallet/testdata/presentation_definition_fmt.json diff --git a/test/bdd/pkg/vcwallet/testdata/presentation_definition_v2.json b/test/bdd/pkg/vcwallet/testdata/presentation_definition_v2.json new file mode 100644 index 000000000..171c5e59f --- /dev/null +++ b/test/bdd/pkg/vcwallet/testdata/presentation_definition_v2.json @@ -0,0 +1,22 @@ +{ + "id": "ec2f83c5-eac4-4d04-b41e-6636d6670a2e", + "input_descriptors": [ + { + "id": "105b1d58-71f8-4d1e-be71-36c9c6f600c9", + "constraints": { + "fields": [ + { + "path": [ + "$.issuer.id" + ], + "filter": { + "type": "string", + "const": "%s" + } + } + ] + }, + "format":%s + } + ] +} diff --git a/test/bdd/pkg/walletjsonld/testdata/query_by_example_fmt.json b/test/bdd/pkg/vcwallet/testdata/query_by_example_fmt.json similarity index 100% rename from test/bdd/pkg/walletjsonld/testdata/query_by_example_fmt.json rename to test/bdd/pkg/vcwallet/testdata/query_by_example_fmt.json diff --git a/test/bdd/pkg/walletjsonld/wallet.go b/test/bdd/pkg/vcwallet/wallet.go similarity index 99% rename from test/bdd/pkg/walletjsonld/wallet.go rename to test/bdd/pkg/vcwallet/wallet.go index 0d0f9c470..830fbc657 100644 --- a/test/bdd/pkg/walletjsonld/wallet.go +++ b/test/bdd/pkg/vcwallet/wallet.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package walletjsonld +package vcwallet import ( "encoding/base64" diff --git a/test/bdd/pkg/walletjsonld/wallet_steps.go b/test/bdd/pkg/vcwallet/wallet_steps.go similarity index 97% rename from test/bdd/pkg/walletjsonld/wallet_steps.go rename to test/bdd/pkg/vcwallet/wallet_steps.go index 4ed90cf48..977f35ddc 100644 --- a/test/bdd/pkg/walletjsonld/wallet_steps.go +++ b/test/bdd/pkg/vcwallet/wallet_steps.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package walletjsonld +package vcwallet import ( _ "embed" //nolint:gci // required for go:embed @@ -73,6 +73,8 @@ func (s *SDKSteps) RegisterSteps(gs *godog.Suite) { gs.Step(`^"([^"]*)" adds credentials to the wallet issued by "([^"]*)"$`, s.addCredentialsToWallet) gs.Step(`^"([^"]*)" verifies credential issued by "([^"]*)"$`, s.holderVerifiesCredentialsFromIssuer) gs.Step(`^"([^"]*)" queries credentials issued by "([^"]*)" using "([^"]*)" query type$`, s.queryPresentations) + gs.Step(`^"([^"]*)" queries "([^"]*)" credentials issued by "([^"]*)" using "([^"]*)" query type$`, + s.queryPresentationWithFormat) gs.Step(`^"([^"]*)" resolves query$`, s.resolveCredentialsQuery) gs.Step(`^"([^"]*)" adds "([^"]*)" presentations proof$`, s.addResolvedPresentationProof) gs.Step(`^"([^"]*)" receives presentations `+ diff --git a/test/bdd/pkg/walletjsonld/query.go b/test/bdd/pkg/walletjsonld/query.go deleted file mode 100644 index 8c1b6a7b8..000000000 --- a/test/bdd/pkg/walletjsonld/query.go +++ /dev/null @@ -1,33 +0,0 @@ -/* -Copyright SecureKey Technologies Inc. All Rights Reserved. - -SPDX-License-Identifier: Apache-2.0 -*/ - -package walletjsonld - -import ( - _ "embed" //nolint:gci // required for go:embed - "encoding/json" - "fmt" - - "github.com/hyperledger/aries-framework-go/pkg/wallet" -) - -//go:embed testdata/query_by_example_fmt.json -var sampleQueryByExFmt string //nolint:gochecknoglobals - -//go:embed testdata/presentation_definition_fmt.json -var presentationDefinitionFmt string //nolint:gochecknoglobals - -func (s *SDKSteps) getQuery(queryType wallet.QueryType, didID string) (json.RawMessage, error) { - switch queryType { - case wallet.QueryByExample: - return []byte(fmt.Sprintf(sampleQueryByExFmt, didID)), nil - case wallet.PresentationExchange: - return []byte(fmt.Sprintf(presentationDefinitionFmt, didID)), nil - } - - // QueryByFrame is not covered in tests, since BBS isn't usable yet for JWT credentials - return nil, fmt.Errorf("invalid queryType %v", queryType) -}