This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
test/support ability in AFGO to create and parse vc and vp in JWT format #3334
Merged
fqutishat
merged 1 commit into
hyperledger-archives:main
from
mishasizov-SK:test/create-parse-vp-vc-jwt
Aug 15, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,20 @@ | ||
# | ||
# Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
@verifiable_jwt | ||
Feature: Issue and verify Verifiable Credential and Verifiable Presentation in JWS format | ||
|
||
Scenario Outline: Issue VC - verify VC - create VP - verify VP | ||
Given crypto algorithm "<crypto>" | ||
And "Berkley" issues VC at "2022-04-12" regarding "Master Degree" to "Alice" | ||
Then "Alice" receives the VC and verifies it | ||
Then "Alice" embeds the VC into VP | ||
Then "Alice" verifies VP | ||
Examples: | ||
| crypto | | ||
| "Ed25519" | | ||
| "ECDSA Secp256r1" | | ||
| "ECDSA Secp384r1" | |
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,120 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package jwt | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/hyperledger/aries-framework-go/pkg/doc/util" | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" | ||
bddverifiable "github.com/hyperledger/aries-framework-go/test/bdd/pkg/verifiable" | ||
) | ||
|
||
func (s *SDKSteps) issueCredential(issuer, issuedAt, subject, holder string) error { | ||
err := s.createDID(issuer, holder) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vcToIssue, err := s.createVC(issuedAt, subject, issuer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vcBytes, err := s.addVCProof(vcToIssue, issuer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.issuedVCBytes = vcBytes | ||
|
||
return nil | ||
} | ||
|
||
func (s *SDKSteps) verifyCredential(holder string) error { | ||
vdr := s.bddContext.AgentCtx[holder].VDRegistry() | ||
pKeyFetcher := verifiable.NewVDRKeyResolver(vdr).PublicKeyFetcher() | ||
|
||
loader, err := bddverifiable.CreateDocumentLoader() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.issuedVC, err = verifiable.ParseCredential(s.issuedVCBytes, | ||
verifiable.WithPublicKeyFetcher(pKeyFetcher), | ||
verifiable.WithJSONLDDocumentLoader(loader)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if s.issuedVC == nil { | ||
return errors.New("received nil credential") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *SDKSteps) createVC(issuedAt, subject, issuer string) (*verifiable.Credential, error) { | ||
const dateLayout = "2006-01-02" | ||
|
||
issued, err := time.Parse(dateLayout, issuedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vcToIssue := &verifiable.Credential{ | ||
Context: []string{ | ||
"https://www.w3.org/2018/credentials/v1", | ||
"https://www.w3.org/2018/credentials/examples/v1", | ||
}, | ||
ID: "http://example.edu/credentials/1872", | ||
Types: []string{ | ||
"VerifiableCredential", | ||
"UniversityDegreeCredential", | ||
}, | ||
Subject: subject, | ||
Issuer: verifiable.Issuer{ | ||
ID: s.getPublicDID(issuer).ID, | ||
CustomFields: verifiable.CustomFields{"name": issuer}, | ||
}, | ||
Issued: util.NewTime(issued), | ||
} | ||
|
||
return vcToIssue, nil | ||
} | ||
|
||
func (s *SDKSteps) addVCProof(vc *verifiable.Credential, issuer string) ([]byte, error) { | ||
doc := s.getPublicDID(issuer) | ||
pubKeyID := doc.VerificationMethod[0].ID | ||
|
||
jwtClaims, err := vc.JWTClaims(false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
publicKeyJWK := s.bddContext.PublicKeys[issuer] | ||
|
||
keyType, err := publicKeyJWK.KeyType() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jwsAlgo, err := verifiable.KeyTypeToJWSAlgo(keyType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signer := s.getSigner(issuer) | ||
|
||
jws, err := jwtClaims.MarshalJWS(jwsAlgo, signer, pubKeyID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []byte(jws), nil | ||
} |
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,49 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package jwt | ||
|
||
import ( | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/did" | ||
bddagent "github.com/hyperledger/aries-framework-go/test/bdd/agent" | ||
bddDIDExchange "github.com/hyperledger/aries-framework-go/test/bdd/pkg/didexchange" | ||
"github.com/hyperledger/aries-framework-go/test/bdd/pkg/didresolver" | ||
) | ||
|
||
func (s *SDKSteps) createDID(issuer, holder string) error { | ||
const ( | ||
inboundHost = "localhost" | ||
inboundPort = "random" | ||
endpointURL = "${SIDETREE_URL}" | ||
acceptDidMethod = "sidetree" | ||
) | ||
|
||
participants := issuer + "," + holder | ||
agentSDK := bddagent.NewSDKSteps() | ||
agentSDK.SetContext(s.bddContext) | ||
|
||
err := agentSDK.CreateAgentWithHTTPDIDResolver(participants, inboundHost, inboundPort, endpointURL, acceptDidMethod) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := s.createKeys(participants); err != nil { | ||
return err | ||
} | ||
|
||
if err := didresolver.CreateDIDDocument(s.bddContext, participants, "JsonWebKey2020"); err != nil { | ||
return err | ||
} | ||
|
||
didExchangeSDK := bddDIDExchange.NewDIDExchangeSDKSteps() | ||
didExchangeSDK.SetContext(s.bddContext) | ||
|
||
return didExchangeSDK.WaitForPublicDID(participants, 10) | ||
} | ||
|
||
func (s *SDKSteps) getPublicDID(agentName string) *did.Doc { | ||
return s.bddContext.PublicDIDDocs[agentName] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extended the list of default verifiers.