Skip to content

Commit

Permalink
Merge pull request #44 from koptan/issue-30-new-system-exception
Browse files Browse the repository at this point in the history
Feat: new Exception system
  • Loading branch information
borisrizov-zf authored Feb 20, 2024
2 parents 8edaf01 + 8066e63 commit cc049f0
Show file tree
Hide file tree
Showing 89 changed files with 1,469 additions and 1,304 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ target/
!**/src/main/**/target/
!**/src/test/**/target/
.DS_Store
.class

### STS ###
.apt_generated
Expand Down
10 changes: 5 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static VerifiableCredential createVCWithoutProof() {

```

5. To Generate VerifiableCredential with ED21559/JWS proof:
5. To Generate VerifiableCredential with ED25519/JWS proof:


```java
Expand All @@ -202,7 +202,7 @@ import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCreden
import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofGenerator;


public static VerifiableCredential createVCWithED21559Proof(
public static VerifiableCredential createVCWithED25519Proof(
VerifiableCredential credential, byte[] privateKey, Did issuer) {

// VC Builder
Expand All @@ -217,7 +217,7 @@ import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofGenerator;
.type(credential.getTypes());

// Ed25519 Proof Builder
final LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.ED21559);
final LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.ED25519);
final Ed25519Signature2020 proof =
(Ed25519Signature2020) generator.createProof(
builder.build(), URI.create(issuer + "#key-1"), privateKey);
Expand Down Expand Up @@ -370,7 +370,7 @@ import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofValidation;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020;

public static boolean verifyED21559LD(VerifiableCredential verifiableCredential) {
public static boolean verifyED25519LD(VerifiableCredential verifiableCredential) {
// DID Resolver Constracture params
DidWebParser didParser = new DidWebParser();
var httpClient = HttpClient.newHttpClient();
Expand All @@ -381,7 +381,7 @@ import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020;
new DidWebDocumentResolver(httpClient, didParser, enforceHttps));

LinkedDataProofValidation proofValidation =
LinkedDataProofValidation.newInstance(SignatureType.ED21559,didDocumentResolverRegistry);
LinkedDataProofValidation.newInstance(SignatureType.ED25519,didDocumentResolverRegistry);
return proofValidation.verify(verifiableCredential);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -27,9 +27,9 @@
import org.eclipse.tractusx.ssi.lib.crypt.IKeyGenerator;
import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey;
import org.eclipse.tractusx.ssi.lib.crypt.KeyPair;
import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559Generator;
import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519Generator;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException;
import org.eclipse.tractusx.ssi.lib.exception.key.KeyGenerationException;
import org.eclipse.tractusx.ssi.lib.model.MultibaseString;
import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory;
import org.eclipse.tractusx.ssi.lib.model.did.Did;
Expand All @@ -54,7 +54,7 @@ public static DidDocument buildDidDocument(String hostName) throws KeyGeneration

// Extracting keys
// final Ed25519KeySet keySet = new Ed25519KeySet(privateKey, publicKey);
IKeyGenerator keyGenerator = new x21559Generator();
IKeyGenerator keyGenerator = new x25519Generator();
KeyPair keyPair = keyGenerator.generateKey();
IPublicKey publicKey = keyPair.getPublicKey();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -30,8 +30,8 @@
import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey;
import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey;
import org.eclipse.tractusx.ssi.lib.crypt.jwk.JsonWebKey;
import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PrivateKey;
import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PublicKey;
import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PrivateKey;
import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PublicKey;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.model.did.Did;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
Expand All @@ -54,8 +54,8 @@ public static DidDocument buildDidDocument(String hostName) {
final Did did = DidWebFactory.fromHostname(hostName);
OctetKeyPair octetKeyPair = new OctetKeyPairGenerator(Curve.Ed25519).keyID("1").generate();

IPrivateKey privateKey = new x21559PrivateKey(octetKeyPair.getDecodedD());
IPublicKey publicKey = new x21559PublicKey(octetKeyPair.getDecodedX());
IPrivateKey privateKey = new x25519PrivateKey(octetKeyPair.getDecodedD());
IPublicKey publicKey = new x25519PublicKey(octetKeyPair.getDecodedX());

// JWK
JsonWebKey jwk = new JsonWebKey(octetKeyPair.getKeyID(), publicKey, privateKey);
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/org/eclipse/tractusx/ssi/examples/ResolveDIDDoc.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -22,14 +22,13 @@
package org.eclipse.tractusx.ssi.examples;

import java.net.http.HttpClient;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebResolver;
import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser;
import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException;
import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException;
import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException;
import org.eclipse.tractusx.ssi.lib.model.did.Did;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.did.DidMethod;

/** This is an example class to demonstrate did document resolve from given did web url */
public class ResolveDIDDoc {
Expand All @@ -38,26 +37,23 @@ public class ResolveDIDDoc {
*
* @param didUrl the did url
* @return the did document
* @throws org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException
* @throws DidDocumentResolverNotRegisteredException the did document resolver not registered
* exception
*/
public static DidDocument ResovleDocument(String didUrl)
throws DidDocumentResolverNotRegisteredException {
throws DidParseException, DidResolverException {

// DID Resolver Constracture params
DidWebParser didParser = new DidWebParser();
var httpClient = HttpClient.newHttpClient();
var enforceHttps = false;

// DID Method
DidMethod didWeb = new DidMethod("web");

// DID
Did did = DidWebFactory.fromHostname(didUrl);

var didDocumentResolverRegistry = new DidDocumentResolverRegistryImpl();
didDocumentResolverRegistry.register(
new DidWebDocumentResolver(httpClient, didParser, enforceHttps));
return didDocumentResolverRegistry.get(didWeb).resolve(did);
var didResolver = new DidWebResolver(httpClient, didParser, enforceHttps);

return didResolver.resolve(did);
}
}
30 changes: 17 additions & 13 deletions src/main/java/org/eclipse/tractusx/ssi/examples/VC.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -26,11 +26,12 @@
import java.util.List;
import java.util.Map;
import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey;
import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat;
import org.eclipse.tractusx.ssi.lib.exception.SsiException;
import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException;
import org.eclipse.tractusx.ssi.lib.exception.json.TransformJsonLdException;
import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException;
import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureGenerateFailedException;
import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedSignatureTypeException;
import org.eclipse.tractusx.ssi.lib.model.did.Did;
import org.eclipse.tractusx.ssi.lib.model.proof.ed21559.Ed25519Signature2020;
import org.eclipse.tractusx.ssi.lib.model.proof.ed25519.Ed25519Signature2020;
import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder;
Expand Down Expand Up @@ -71,7 +72,7 @@ public static VerifiableCredential createVCWithoutProof() {
}

/**
* Create verifiable credential with ED21559 proof
* Create verifiable credential with ED25519 proof
*
* @param credential the credential
* @param privateKey the private key
Expand All @@ -81,9 +82,10 @@ public static VerifiableCredential createVCWithoutProof() {
* @throws SsiException the ssi exception
* @throws InvalidePrivateKeyFormat the invalide private key format
*/
public static VerifiableCredential createVCWithED21559Proof(
public static VerifiableCredential createVCWithED25519Proof(
VerifiableCredential credential, IPrivateKey privateKey, Did issuer)
throws UnsupportedSignatureTypeException, SsiException, InvalidePrivateKeyFormat {
throws UnsupportedSignatureTypeException, InvalidPrivateKeyFormatException,
SignatureGenerateFailedException, TransformJsonLdException {

// VC Builder
final VerifiableCredentialBuilder builder =
Expand All @@ -98,7 +100,7 @@ public static VerifiableCredential createVCWithED21559Proof(

// Ed25519 Proof Builder
final LinkedDataProofGenerator generator =
LinkedDataProofGenerator.newInstance(SignatureType.ED21559);
LinkedDataProofGenerator.newInstance(SignatureType.ED25519);
final Ed25519Signature2020 proof =
(Ed25519Signature2020)
generator.createProof(builder.build(), URI.create(issuer + "#key-1"), privateKey);
Expand All @@ -122,7 +124,8 @@ public static VerifiableCredential createVCWithED21559Proof(
*/
public static VerifiableCredential createVCWithJWSProof(
VerifiableCredential credential, IPrivateKey privateKey, Did issuer)
throws UnsupportedSignatureTypeException, SsiException, InvalidePrivateKeyFormat {
throws UnsupportedSignatureTypeException, InvalidPrivateKeyFormatException,
SignatureGenerateFailedException, TransformJsonLdException {

// VC Builder
final VerifiableCredentialBuilder builder =
Expand All @@ -136,9 +139,10 @@ public static VerifiableCredential createVCWithJWSProof(
.type(credential.getTypes());

// JWS Proof Builder
final LinkedDataProofGenerator generator =
LinkedDataProofGenerator.newInstance(SignatureType.JWS);
final JWSSignature2020 proof =
LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.JWS);

JWSSignature2020 proof = null;
proof =
(JWSSignature2020)
generator.createProof(builder.build(), URI.create(issuer + "#key-1"), privateKey);

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/eclipse/tractusx/ssi/examples/Validation.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -22,8 +22,9 @@
package org.eclipse.tractusx.ssi.examples;

import com.nimbusds.jwt.SignedJWT;
import org.eclipse.tractusx.ssi.lib.exception.JwtAudienceCheckFailedException;
import org.eclipse.tractusx.ssi.lib.exception.JwtExpiredException;
import org.eclipse.tractusx.ssi.lib.exception.proof.JwtAudienceCheckException;
import org.eclipse.tractusx.ssi.lib.exception.proof.JwtExpiredException;
import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureParseException;
import org.eclipse.tractusx.ssi.lib.jwt.SignedJwtValidator;

/** This is example class to demonstrate how to validate JWT expiry date and audience */
Expand All @@ -33,11 +34,11 @@ public class Validation {
*
* @param signedJWT the signed jwt
* @param audience the audience
* @throws JwtAudienceCheckFailedException the jwt audience check failed exception
* @throws JwtAudienceCheckException the jwt audience check failed exception
* @throws JwtExpiredException the jwt expired exception
*/
public static void validateJWTDate(SignedJWT signedJWT, String audience)
throws JwtAudienceCheckFailedException, JwtExpiredException {
throws JwtAudienceCheckException, JwtExpiredException, SignatureParseException {
SignedJwtValidator jwtValidator = new SignedJwtValidator();
jwtValidator.validateDate(signedJWT);
}
Expand All @@ -47,11 +48,11 @@ public static void validateJWTDate(SignedJWT signedJWT, String audience)
*
* @param signedJWT the signed jwt
* @param audience the audience
* @throws JwtAudienceCheckFailedException the jwt audience check failed exception
* @throws JwtAudienceCheckException the jwt audience check failed exception
* @throws JwtExpiredException the jwt expired exception
*/
public static void validateJWTAudiences(SignedJWT signedJWT, String audience)
throws JwtAudienceCheckFailedException, JwtExpiredException {
throws JwtAudienceCheckException, JwtExpiredException, SignatureParseException {
SignedJwtValidator jwtValidator = new SignedJwtValidator();
jwtValidator.validateAudiences(signedJWT, audience);
}
Expand Down
Loading

0 comments on commit cc049f0

Please sign in to comment.