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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support ability in AFGO to create and parse vc and vp in JWT format
Signed-off-by: Mykhailo Sizov <mykhailo.sizov@securekey.com>
- Loading branch information
1 parent
e05b76b
commit 046adf0
Showing
12 changed files
with
478 additions
and
60 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
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.