-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
334 additions
and
172 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,140 @@ | ||
package testsuite | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/json" | ||
"encoding/pem" | ||
"github.com/nuts-foundation/nuts-node/crypto" | ||
"github.com/nuts-foundation/nuts-node/jsonld" | ||
"github.com/nuts-foundation/nuts-node/vcr/signature" | ||
"github.com/nuts-foundation/nuts-node/vcr/signature/proof" | ||
"github.com/piprate/json-gold/ld" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type testCase struct { | ||
name string | ||
file string | ||
} | ||
|
||
var testCases = []testCase{ | ||
// Note: there is no test for an NutsAuthorizationCredential with localParameters, | ||
// because localParameters in v1.1 aren't compatible with v1.0 since its type changed from @graph to @json. | ||
// This is not a problem, because nobody actually used it in v1.0. | ||
{ | ||
name: "NutsAuthorizationCredential", | ||
file: "authcred_001.ldjson", | ||
}, | ||
{ | ||
name: "NutsOrganizationCredential", | ||
file: "orgcred_001.ldjson", | ||
}, | ||
} | ||
|
||
// TestCompatibility tests backwards compatibility of the Nuts JSON-LD context. | ||
// It uses the test cases found in ./fixtures and checks the signature against every Nuts JSON-LD context version. | ||
func TestCompatibility(t *testing.T) { | ||
key := readSigningKey(t) | ||
type context struct { | ||
version string | ||
loader ld.DocumentLoader | ||
} | ||
contexts := []context{ | ||
{ | ||
version: "1.0", | ||
loader: jsonld.NewMappedDocumentLoader(map[string]string{ | ||
"https://nuts.nl/credentials/v1": "../../vcr/assets/assets/contexts/nuts.ldjson", | ||
jsonld.W3cVcContext: "../../vcr/assets/assets/contexts/w3c-credentials-v1.ldjson", | ||
jsonld.Jws2020Context: "../../vcr/assets/assets/contexts/lds-jws2020-v1.ldjson", | ||
}, ld.NewDefaultDocumentLoader(nil)), | ||
}, | ||
} | ||
|
||
for _, ctx := range contexts { | ||
t.Run(ctx.version, func(t *testing.T) { | ||
for _, tc := range testCases { | ||
t.Run(tc.file, func(t *testing.T) { | ||
data, err := os.ReadFile("./fixtures/" + tc.file) | ||
require.NoError(t, err) | ||
var document proof.SignedDocument | ||
err = json.Unmarshal(data, &document) | ||
require.NoError(t, err) | ||
|
||
ldProof := proof.LDProof{} | ||
err = document.UnmarshalProofValue(&ldProof) | ||
require.NoError(t, err) | ||
err = ldProof.Verify(document.DocumentWithoutProof(), signature.JSONWebSignature2020{ContextLoader: ctx.loader}, key.Public()) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestGenerateSignedFixtures is used to generate signed test fixtures of the unsigned test cases. | ||
// It's only there as runnable unit test to assert it keeps working. | ||
func TestGenerateSignedFixtures(t *testing.T) { | ||
const saveSigned = false | ||
|
||
loader := jsonld.NewMappedDocumentLoader(map[string]string{ | ||
"https://nuts.nl/credentials/v1": "../../vcr/assets/assets/contexts/nuts.ldjson", | ||
jsonld.W3cVcContext: "../../vcr/assets/assets/contexts/w3c-credentials-v1.ldjson", | ||
jsonld.Jws2020Context: "../../vcr/assets/assets/contexts/lds-jws2020-v1.ldjson", | ||
}, ld.NewDefaultDocumentLoader(nil)) | ||
|
||
privateKey := readSigningKey(t) | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.file, func(t *testing.T) { | ||
unsignedFile := "./fixtures/" + strings.ReplaceAll(testCase.file, ".ldjson", "_unsigned.ldjson") | ||
data, err := os.ReadFile(unsignedFile) | ||
require.NoError(t, err) | ||
|
||
var tbs proof.Document | ||
err = json.Unmarshal(data, &tbs) | ||
require.NoError(t, err) | ||
|
||
signed, err := proof.NewLDProof(proof.ProofOptions{ | ||
Created: time.Now(), | ||
}).Sign(tbs, signature.JSONWebSignature2020{ContextLoader: loader}, privateKey) | ||
require.NoError(t, err) | ||
|
||
var targetFile = "./fixtures/" + testCase.file | ||
// If not saving, still save it (although to temp dir) to it keeps working | ||
if !saveSigned { | ||
tempFile, err := os.CreateTemp("", testCase.file) | ||
defer func() { | ||
_ = os.Remove(tempFile.Name()) | ||
}() | ||
require.NoError(t, err) | ||
_ = tempFile.Close() | ||
targetFile = tempFile.Name() | ||
} | ||
signedBytes, err := json.MarshalIndent(signed, "", " ") | ||
require.NoError(t, err) | ||
// Copy file mode from unsigned file | ||
fileInfo, err := os.Stat(unsignedFile) | ||
require.NoError(t, err) | ||
err = os.WriteFile(targetFile, signedBytes, fileInfo.Mode()) | ||
require.NoError(t, err) | ||
println("Written to", targetFile) | ||
}) | ||
} | ||
} | ||
|
||
func readSigningKey(t *testing.T) crypto.Key { | ||
pkPEMBytes, err := os.ReadFile("private_key.pem") | ||
require.NoError(t, err) | ||
pkDerBytes, _ := pem.Decode(pkPEMBytes) | ||
privateKey, err := x509.ParseECPrivateKey(pkDerBytes.Bytes) | ||
require.NoError(t, err) | ||
return crypto.TestKey{ | ||
PrivateKey: privateKey, | ||
Kid: "key-id", | ||
} | ||
} |
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,34 @@ | ||
{ | ||
"@context": [ | ||
"https://www.w3.org/2018/credentials/v1", | ||
"https://nuts.nl/credentials/v1", | ||
"https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json" | ||
], | ||
"credentialSubject": { | ||
"id": "did:nuts:B8PUHs2AUHbFF1xLLK4eZjgErEcMXHxs68FteY7NDtCY", | ||
"purposeOfUse": "eTransfer", | ||
"resources": [ | ||
{ | ||
"operations": [ | ||
"read" | ||
], | ||
"path": "/composition/1", | ||
"userContext": true | ||
} | ||
] | ||
}, | ||
"id": "did:nuts:GvkzxsezHvEc8nGhgz6Xo3jbqkHwswLmWw3CYtCm7hAW#1", | ||
"issuanceDate": "2022-11-25T09:44:16.972576+01:00", | ||
"issuer": "did:nuts:GvkzxsezHvEc8nGhgz6Xo3jbqkHwswLmWw3CYtCm7hAW", | ||
"proof": { | ||
"created": "2022-12-05T13:47:24.714488+01:00", | ||
"jws": "eyJhbGciOiJFUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..8fD_JGQiYLvryd3EPUR7ft41piyqm0rs8_3jYVLZjgld4q9YIjPus-nAE1f4473oo4xh9PW_khbRJaiU-twBMw", | ||
"proofPurpose": "assertionMethod", | ||
"type": "JsonWebSignature2020", | ||
"verificationMethod": "key-id" | ||
}, | ||
"type": [ | ||
"NutsAuthorizationCredential", | ||
"VerifiableCredential" | ||
] | ||
} |
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,27 @@ | ||
{ | ||
"@context": [ | ||
"https://www.w3.org/2018/credentials/v1", | ||
"https://nuts.nl/credentials/v1", | ||
"https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json" | ||
], | ||
"credentialSubject": { | ||
"id": "did:nuts:B8PUHs2AUHbFF1xLLK4eZjgErEcMXHxs68FteY7NDtCY", | ||
"purposeOfUse": "eTransfer", | ||
"resources": [ | ||
{ | ||
"operations": [ | ||
"read" | ||
], | ||
"path": "/composition/1", | ||
"userContext": true | ||
} | ||
] | ||
}, | ||
"id": "did:nuts:GvkzxsezHvEc8nGhgz6Xo3jbqkHwswLmWw3CYtCm7hAW#1", | ||
"issuanceDate": "2022-11-25T09:44:16.972576+01:00", | ||
"issuer": "did:nuts:GvkzxsezHvEc8nGhgz6Xo3jbqkHwswLmWw3CYtCm7hAW", | ||
"type": [ | ||
"NutsAuthorizationCredential", | ||
"VerifiableCredential" | ||
] | ||
} |
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,28 @@ | ||
{ | ||
"@context": [ | ||
"https://nuts.nl/credentials/v1", | ||
"https://www.w3.org/2018/credentials/v1", | ||
"https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json" | ||
], | ||
"credentialSubject": { | ||
"id": "did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey", | ||
"organization": { | ||
"city": "IJbergen", | ||
"name": "Because we care B.V." | ||
} | ||
}, | ||
"id": "did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey#ec8af8cf-67d4-4b54-9bd6-8a861e729e11", | ||
"issuanceDate": "2022-06-01T15:34:40.65319+02:00", | ||
"issuer": "did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey", | ||
"proof": { | ||
"created": "2022-12-05T13:47:24.717922+01:00", | ||
"jws": "eyJhbGciOiJFUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..WyQ-9rd323hXQLgceVAA4eRf1PHwK58dMu_Ugsl63i4WPCj6gNsZQ173qneetZYzNl5IMZWgtuZMGkB5tg-lag", | ||
"proofPurpose": "assertionMethod", | ||
"type": "JsonWebSignature2020", | ||
"verificationMethod": "key-id" | ||
}, | ||
"type": [ | ||
"NutsOrganizationCredential", | ||
"VerifiableCredential" | ||
] | ||
} |
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,21 @@ | ||
{ | ||
"@context": [ | ||
"https://nuts.nl/credentials/v1", | ||
"https://www.w3.org/2018/credentials/v1", | ||
"https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json" | ||
], | ||
"credentialSubject": { | ||
"id": "did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey", | ||
"organization": { | ||
"city": "IJbergen", | ||
"name": "Because we care B.V." | ||
} | ||
}, | ||
"id": "did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey#ec8af8cf-67d4-4b54-9bd6-8a861e729e11", | ||
"issuanceDate": "2022-06-01T15:34:40.65319+02:00", | ||
"issuer": "did:nuts:CuE3qeFGGLhEAS3gKzhMCeqd1dGa9at5JCbmCfyMU2Ey", | ||
"type": [ | ||
"NutsOrganizationCredential", | ||
"VerifiableCredential" | ||
] | ||
} |
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,5 @@ | ||
-----BEGIN EC PRIVATE KEY----- | ||
MHcCAQEEICWw5A5U8aPEiu/QJyIMP4mUqFwx62CZgjr1xdxgcoZYoAoGCCqGSM49 | ||
AwEHoUQDQgAEZoS/Grh0lkKnW3ZO/NOEzq3kfIP6TYzsq3ldvJPEoK3mipaGUiYd | ||
tSsNzlnEd4g8ecj06XlVpRGSZXOz6fLFpQ== | ||
-----END EC PRIVATE KEY----- |
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
Oops, something went wrong.