Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
feat: fix format filtering in pEx V2 for JWTVCs, and add bdd tests (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
Moopli authored Sep 12, 2022
1 parent 9fc7486 commit f7a0dc3
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 49 deletions.
9 changes: 7 additions & 2 deletions pkg/doc/presexch/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions test/bdd/agent/agent_sdk_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions test/bdd/bddtests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -219,7 +219,7 @@ func features() []feature {
webkms.NewCryptoSDKSteps(),
waci.NewIssuanceDIDCommV1SDKSteps(),
waci.NewIssuanceDIDCommV2SDKSteps(),
walletjsonld.NewSDKSteps(),
vcwallet.NewSDKSteps(),
legacyconnection.NewLegacyConnectionControllerSteps(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature: Verifiable Credential flows in universal wallet SDK
And "Berkley" issues "<format>" 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 "<queryType>" query type
When "Vanna" queries "<format>" credentials issued by "Berkley" using "<queryType>" query type
Then "Alice" resolves query
And "Alice" adds "<format>" presentations proof
And "Alice" closes wallet
Expand All @@ -43,7 +43,7 @@ Feature: Verifiable Credential flows in universal wallet SDK
And "Alice" issues "<format>" 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 "<format>" credentials issued by "Alice" using "<queryType>" query type
And "Alice" resolves query
And "Alice" adds "<format>" presentations proof
And "Alice" closes wallet
Expand Down
40 changes: 39 additions & 1 deletion test/bdd/pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package walletjsonld
package vcwallet

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package walletjsonld
package vcwallet

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
72 changes: 72 additions & 0 deletions test/bdd/pkg/vcwallet/query.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions test/bdd/pkg/vcwallet/testdata/presentation_definition_v2.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package walletjsonld
package vcwallet

import (
"encoding/base64"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 `+
Expand Down
33 changes: 0 additions & 33 deletions test/bdd/pkg/walletjsonld/query.go

This file was deleted.

0 comments on commit f7a0dc3

Please sign in to comment.