Skip to content

Commit

Permalink
[#35] Separate subject validation from JSON-LD schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tdiesler committed Oct 4, 2023
1 parent 57585c6 commit 40b824e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,32 @@ private static void validateRun(Runnable runnable, String message) throws Illega
}
}

@Deprecated
public static void validate(VerifiableCredential verifiableCredential) throws IllegalStateException {
validateJsonLd(verifiableCredential);
}

@Deprecated
public static void validate(VerifiablePresentation verifiablePresentation) throws IllegalStateException {
validateJsonLd(verifiablePresentation);
}

public static void validateJsonLd(VerifiableCredential verifiableCredential) throws IllegalStateException {

foundation.identity.jsonld.validation.Validation.validate(verifiableCredential);

validateJson(verifiableCredential);
}

public static void validateJsonLd(VerifiablePresentation verifiablePresentation) throws IllegalStateException {

foundation.identity.jsonld.validation.Validation.validate(verifiablePresentation);

validateJson(verifiablePresentation);
}

public static void validateJson(VerifiableCredential verifiableCredential) throws IllegalStateException {

validateRun(() -> { validateTrue(verifiableCredential.getJsonObject() != null); }, "Bad or missing JSON object.");
validateRun(() -> { validateTrue(verifiableCredential.getContexts().size() > 0); }, "Bad or missing '@context'.");
validateRun(() -> { validateUrl(verifiableCredential.getContexts().get(0)); }, "@context must be a valid URI: " + verifiableCredential.getContexts().get(0));
Expand All @@ -56,9 +78,7 @@ public static void validate(VerifiableCredential verifiableCredential) throws Il
validateRun(() -> { validateTrue(verifiableCredential.getCredentialSubject() != null); }, "Bad or missing 'credentialSubject'.");
}

public static void validate(VerifiablePresentation verifiablePresentation) throws IllegalStateException {

foundation.identity.jsonld.validation.Validation.validate(verifiablePresentation);
public static void validateJson(VerifiablePresentation verifiablePresentation) throws IllegalStateException {

validateRun(() -> { validateTrue(verifiablePresentation.getJsonObject() != null); }, "Bad or missing JSON object.");
validateRun(() -> { validateTrue(verifiablePresentation.getContexts().size() > 0); }, "Bad or missing '@context'.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
public class SignCredentialTest {

@Test
void testSign() throws Throwable {
void testSignJsonLd() throws Throwable {

VerifiableCredential verifiableCredential = VerifiableCredential.fromJson(new InputStreamReader(VerifyCredentialTest.class.getResourceAsStream("input.vc.jsonld")));
VerifiableCredential verifiableCredential = VerifiableCredential.fromJson(
new InputStreamReader(getClass().getResourceAsStream("input.vc.jsonld")));

URI verificationMethod = URI.create("did:sov:1yvXbmgPoUm4dl66D7KhyD#keys-1");
Date created = JsonLDUtils.DATE_FORMAT.parse("2018-01-01T21:19:10Z");
Expand All @@ -41,7 +42,39 @@ void testSign() throws Throwable {
assertEquals(nonce, ldProof.getNonce());
assertEquals("eyJiNjQiOmZhbHNlLCJjcml0IjpbImI2NCJdLCJhbGciOiJSUzI1NiJ9..Sn-LB5t_x-kh9mUDq1DaS1GScj3PY_2fMnNnhq09x-ZBf6_EzYfvgFOpEvdLUwxkJiEt7B2x-LGg7bp-o7UEGCbIxWdGUTG7BGAsKsU18hUwOHrVNZ6VHovbxeFgK0iNMn0MObDiGdQbYMG8C71m3AvquUP00-2UiDcqNxmGAYg5tHv7SHXLEgvaz7SnIkBklj1yj_TMXreSGa_okbXFYxh7SkMfFcxHbBFShr0Fzd8DTn8tr_WvPHR7Tx3bkJHmqFx9Wo-0e7FkLeICsgmBKa5Hzz-y_1yEQPsDaZRRsbXfBD4krL7WTplJtAwnQ5Sy-L9cwZzNhCQC6KsggGjTgQ", ldProof.getJws());

Validation.validate(verifiableCredential);
Validation.validateJsonLd(verifiableCredential);

RsaSignature2018LdVerifier verifier = new RsaSignature2018LdVerifier(TestUtil.testRSAPublicKey);
boolean verify = verifier.verify(verifiableCredential);
assertTrue(verify);
}

@Test
void testSignJson() throws Throwable {

VerifiableCredential verifiableCredential = VerifiableCredential.fromJson(
new InputStreamReader(getClass().getResourceAsStream("input.vc.json")));

URI verificationMethod = URI.create("did:sov:1yvXbmgPoUm4dl66D7KhyD#keys-1");
Date created = JsonLDUtils.DATE_FORMAT.parse("2018-01-01T21:19:10Z");
String domain = null;
String nonce = "c0ae1c8e-c7e7-469f-b252-86e6a0e7387e";

RsaSignature2018LdSigner signer = new RsaSignature2018LdSigner(TestUtil.testRSAPrivateKey);
signer.setVerificationMethod(verificationMethod);
signer.setCreated(created);
signer.setDomain(domain);
signer.setNonce(nonce);
LdProof ldProof = signer.sign(verifiableCredential, true, false);

assertEquals(SignatureSuites.SIGNATURE_SUITE_RSASIGNATURE2018.getTerm(), ldProof.getType());
assertEquals(verificationMethod, ldProof.getVerificationMethod());
assertEquals(created, ldProof.getCreated());
assertEquals(domain, ldProof.getDomain());
assertEquals(nonce, ldProof.getNonce());
assertEquals("eyJiNjQiOmZhbHNlLCJjcml0IjpbImI2NCJdLCJhbGciOiJSUzI1NiJ9..GZYi1V8tbMLl5rLIZarlj-aX3KDTTqFJFtQr-2cV910J6embA7_fQPalX3pZLzld9mQ0SdJq2hlxWpMzujXKOElfWidtxJzOyp93ZsSbrtfj7fGSV_CYOSfQ7A8n3SR4O3pp6ja4vmDmBhP95oJXh_BVTbtqvU7e-_GngC2B9uoBr4JJd2mxsOu2_97u_-scPWv9xUIm5rFTGfLz5sUGbMihY96fywSATn9mD5aLDql2thHnrkfYHgsxAqQDV-gcvlZHw5-TtxN-NnG3DD5K_mugmlV3x10ZGLC5QCw0q83LGVi7NmBMShALOFtcO5CourGDSmc1jL9qA95GXMH_dA", ldProof.getJws());

Validation.validateJson(verifiableCredential);

RsaSignature2018LdVerifier verifier = new RsaSignature2018LdVerifier(TestUtil.testRSAPublicKey);
boolean verify = verifier.verify(verifiableCredential);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"@context": [
"https://www.w3.org/2018/credentials/v1"
],
"id": "http://example.edu/credentials/1872",
"type": ["VerifiableCredential", "UniversityDegreeCredential"],
"issuer": "https://example.edu/issuers/565049",
"issuanceDate": "2010-01-01T19:73:24Z",
"credentialSubject": {
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
"givenName": "Manu",
"familyName": "Sporny",
"college": {
"id": "did:example:c276e12ec21ebfeb1f712ebc6f1"
}
}
}

0 comments on commit 40b824e

Please sign in to comment.