Skip to content

Commit a14623b

Browse files
author
Mohamed Sameem Ahamed Azeem
committed
Merge remote-tracking branch 'remotes/origin/master' into release-0.2.x
# Conflicts: # pom.xml
2 parents 0dbda09 + 2c030e1 commit a14623b

27 files changed

+799
-413
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Dependency:
1818
<dependency>
1919
<groupId>com.trustnet</groupId>
2020
<artifactId>verifiable-credentials-java</artifactId>
21-
<version>0.2-SNAPSHOT</version>
21+
<version>0.3-SNAPSHOT</version>
2222
<scope>compile</scope>
2323
</dependency>
2424

pom.xml

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
<scm>
1616
<url>https://github.com/danubetech/verifiable-credentials-java</url>
1717
<connection>scm:git:git://github.com/danubetech/verifiable-credentials-java.git</connection>
18-
<developerConnection>scm:git:[email protected]:danubetech/verifiable-credentials-java.git</developerConnection>
19-
<tag>HEAD</tag>
20-
</scm>
18+
<developerConnection>scm:git:git@danubetech/verifiable-credentials-java.git</developerConnection>
19+
</scm>
2120

2221
<distributionManagement>
2322
<repository>
@@ -26,7 +25,7 @@
2625
<url>https://maven.pkg.github.com/danubetech/verifiable-credentials-java</url>
2726
</repository>
2827
</distributionManagement>
29-
28+
3029
<properties>
3130
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3231
<github.global.server>github</github.global.server>
@@ -103,6 +102,7 @@
103102
</build>
104103

105104
<dependencies>
105+
106106
<dependency>
107107
<groupId>org.junit.jupiter</groupId>
108108
<artifactId>junit-jupiter-engine</artifactId>
@@ -135,22 +135,8 @@
135135
<dependency>
136136
<groupId>com.nimbusds</groupId>
137137
<artifactId>nimbus-jose-jwt</artifactId>
138-
<version>8.2.1</version>
139-
<scope>compile</scope>
140-
</dependency>
141-
<dependency>
142-
<groupId>com.github.jsonld-java</groupId>
143-
<artifactId>jsonld-java</artifactId>
144-
<version>0.12.0</version>
138+
<version>8.11</version>
145139
<scope>compile</scope>
146-
<!-- <exclusions>
147-
<exclusion>
148-
<artifactId>httpcore-osgi</artifactId>
149-
</exclusion>
150-
<exclusion>
151-
<artifactId>httpclient-osgi</artifactId>
152-
</exclusion>
153-
</exclusions> -->
154140
</dependency>
155141
<!-- <dependency>
156142
<groupId>org.slf4j</groupId>

src/main/java/com/danubetech/verifiablecredentials/VerifiableCredential.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ public class VerifiableCredential {
3434
public static final String JSONLD_TERM_CREDENTIAL_SUBJECT = "credentialSubject";
3535

3636
public static final SimpleDateFormat DATE_FORMAT;
37+
public static final SimpleDateFormat DATE_FORMAT_MILLIS;
3738

3839
private final LinkedHashMap<String, Object> jsonLdObject;
3940

4041
static {
4142

4243
DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
4344
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
45+
46+
DATE_FORMAT_MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
47+
DATE_FORMAT_MILLIS.setTimeZone(TimeZone.getTimeZone("UTC"));
4448
}
4549

4650
private VerifiableCredential(LinkedHashMap<String, Object> jsonLdObject, boolean validate) {
@@ -76,6 +80,7 @@ public static VerifiableCredential fromJsonLdObject(LinkedHashMap<String, Object
7680
return fromJsonLdObject(jsonLdObject, true);
7781
}
7882

83+
@SuppressWarnings("unchecked")
7984
public static VerifiableCredential fromJsonString(String jsonString, boolean validate) throws JsonParseException, IOException {
8085

8186
LinkedHashMap<String, Object> jsonLdObject = (LinkedHashMap<String, Object>) JsonUtils.fromString(jsonString);
@@ -147,6 +152,7 @@ public void setCredentialSubject(String subject) {
147152
this.getJsonLdCredentialSubject().put(JSONLD_TERM_ID, subject);
148153
}
149154

155+
@SuppressWarnings("unchecked")
150156
public List<String> getContext() {
151157

152158
return (List<String>) this.jsonLdObject.get(JsonLdConsts.CONTEXT);
@@ -160,6 +166,7 @@ public void setContext(List<String> context) {
160166
this.jsonLdObject.put(JsonLdConsts.CONTEXT, context);
161167
}
162168

169+
@SuppressWarnings("unchecked")
163170
public List<String> getType() {
164171

165172
Object object = this.jsonLdObject.get(JSONLD_TERM_TYPE);
@@ -197,12 +204,16 @@ public void setIssuer(String issuer) {
197204

198205
public Date getIssuanceDate() {
199206

207+
String issuanceDateString = (String) this.jsonLdObject.get(JSONLD_TERM_ISSUANCE_DATE);
208+
if (issuanceDateString == null) return null;
200209
try {
201-
String issuanceDateString = (String) this.jsonLdObject.get(JSONLD_TERM_ISSUANCE_DATE);
202-
if (issuanceDateString == null) return null;
203210
return DATE_FORMAT.parse(issuanceDateString);
204211
} catch (ParseException ex) {
205-
throw new RuntimeException(ex.getMessage(), ex);
212+
try {
213+
return DATE_FORMAT_MILLIS.parse(issuanceDateString);
214+
} catch (ParseException ex2) {
215+
throw new RuntimeException(ex.getMessage(), ex);
216+
}
206217
}
207218
}
208219

@@ -216,12 +227,16 @@ public void setIssuanceDate(Date issuanceDate) {
216227

217228
public Date getExpirationDate() {
218229

230+
String expirationDateString = (String) this.jsonLdObject.get(JSONLD_TERM_EXPIRATION_DATE);
231+
if (expirationDateString == null) return null;
219232
try {
220-
String expirationDateString = (String) this.jsonLdObject.get(JSONLD_TERM_EXPIRATION_DATE);
221-
if (expirationDateString == null) return null;
222233
return DATE_FORMAT.parse(expirationDateString);
223234
} catch (ParseException ex) {
224-
throw new RuntimeException(ex.getMessage(), ex);
235+
try {
236+
return DATE_FORMAT_MILLIS.parse(expirationDateString);
237+
} catch (ParseException ex2) {
238+
throw new RuntimeException(ex.getMessage(), ex);
239+
}
225240
}
226241
}
227242

@@ -286,7 +301,7 @@ public void validate() throws IllegalStateException {
286301
validateRun(() -> { if (this.getId() != null) validateUrl(this.getId()); }, "'@id' must be a valid URI.");
287302
validateRun(() -> { validateUrl(this.getIssuer()); }, "'issuer' must be a valid URI.");
288303
validateRun(() -> { validateTrue(JSONLD_CONTEXT_CREDENTIALS.equals(this.getContext().get(0)) || JSONLD_CONTEXT_CREDENTIALS_NO_WWW.equals(this.getContext().get(0))); }, "First value of @context must be https://www.w3.org/2018/credentials/v1: " + this.getContext().get(0));
289-
validateRun(() -> { for (String context : this.getContext()) validateUrl(context); }, "@context must be a valid URI: " + this.getContext());
304+
validateRun(() -> { validateUrl(this.getContext().get(0)); }, "@context must be a valid URI: " + this.getContext().get(0));
290305
validateRun(() -> { validateTrue(this.getType().contains(JSONLD_TYPE_VERIFIABLE_CREDENTIAL)); }, "@type must contain VerifiableCredential: " + this.getType());
291306
}
292307

src/main/java/com/danubetech/verifiablecredentials/VerifiablePresentation.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.ArrayList;
55
import java.util.LinkedHashMap;
6+
import java.util.List;
67

78
import com.danubetech.verifiablecredentials.jwt.JwtVerifiableCredential;
89
import com.fasterxml.jackson.core.JsonGenerationException;
@@ -51,6 +52,12 @@ public static VerifiablePresentation fromJsonLdObject(LinkedHashMap<String, Obje
5152
return new VerifiablePresentation(jsonLdObject, validate);
5253
}
5354

55+
public static VerifiablePresentation fromJsonLdObject(LinkedHashMap<String, Object> jsonLdObject) {
56+
57+
return fromJsonLdObject(jsonLdObject, true);
58+
}
59+
60+
@SuppressWarnings("unchecked")
5461
public static VerifiablePresentation fromJsonString(String jsonString, boolean validate) throws JsonParseException, IOException {
5562

5663
LinkedHashMap<String, Object> jsonLdObject = (LinkedHashMap<String, Object>) JsonUtils.fromString(jsonString);
@@ -73,8 +80,8 @@ public static VerifiablePresentation fromVerifiableCredential(VerifiableCredenti
7380
ArrayList<String> typeList = new ArrayList<String> ();
7481
typeList.add(JSONLD_TYPE_VERIFIABLE_PRESENTATION);
7582

76-
ArrayList<String> verifiableCredentialList = new ArrayList<String> ();
77-
verifiableCredentialList.add(verifiableCredential.toJsonString());
83+
ArrayList<Object> verifiableCredentialList = new ArrayList<Object> ();
84+
verifiableCredentialList.add(verifiableCredential.getJsonLdObject());
7885

7986
jsonLdObject = new LinkedHashMap<String, Object> ();
8087
jsonLdObject.put(JsonLdConsts.CONTEXT, contextList);
@@ -115,6 +122,29 @@ public static VerifiablePresentation fromJwtVerifiableCredential(JwtVerifiableCr
115122
return fromJwtVerifiableCredential(jwtVerifiableCredential, true);
116123
}
117124

125+
@SuppressWarnings("unchecked")
126+
public VerifiableCredential getVerifiableCredential() {
127+
128+
Object verifiableCredentialObject = this.getJsonLdObject().get(JSONLD_TERM_VERIFIABLE_CREDENTIAL);
129+
130+
LinkedHashMap<String, Object> jsonLdObject = null;
131+
132+
if (verifiableCredentialObject instanceof LinkedHashMap) {
133+
134+
jsonLdObject = (LinkedHashMap<String, Object>) verifiableCredentialObject;
135+
} else if (verifiableCredentialObject instanceof List) {
136+
137+
List<?> verifiableCredentialList = (List<?>) verifiableCredentialObject;
138+
139+
if (verifiableCredentialList.size() == 1) {
140+
141+
jsonLdObject = (LinkedHashMap<String, Object>) verifiableCredentialList.get(0);
142+
}
143+
}
144+
145+
return jsonLdObject == null ? null : VerifiableCredential.fromJsonLdObject(jsonLdObject);
146+
}
147+
118148
public LinkedHashMap<String, Object> getJsonLdObject() {
119149

120150
return this.jsonLdObject;

src/main/java/com/danubetech/verifiablecredentials/jwt/JwtObject.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,22 @@
2020
import info.weboftrust.ldsignatures.crypto.adapter.JWSVerifierAdapter;
2121
import info.weboftrust.ldsignatures.crypto.impl.Ed25519_EdDSA_PrivateKeySigner;
2222
import info.weboftrust.ldsignatures.crypto.impl.Ed25519_EdDSA_PublicKeyVerifier;
23-
import info.weboftrust.ldsignatures.crypto.impl.P256K_ES256K_PrivateKeySigner;
24-
import info.weboftrust.ldsignatures.crypto.impl.P256K_ES256K_PublicKeyVerifier;
2523
import info.weboftrust.ldsignatures.crypto.impl.RSA_RS256_PrivateKeySigner;
2624
import info.weboftrust.ldsignatures.crypto.impl.RSA_RS256_PublicKeyVerifier;
25+
import info.weboftrust.ldsignatures.crypto.impl.secp256k1_ES256K_PrivateKeySigner;
26+
import info.weboftrust.ldsignatures.crypto.impl.secp256k1_ES256K_PublicKeyVerifier;
2727

28-
abstract class JwtObject <T> {
28+
public class JwtObject {
2929

3030
private final JWTClaimsSet payload;
31-
private final T payloadObject;
32-
3331
private JWSObject jwsObject;
3432
private String compactSerialization;
3533

36-
protected JwtObject(JWTClaimsSet payload, T payloadObject, JWSObject jwsObject, String compactSerialization) {
34+
public JwtObject(JWTClaimsSet payload, JWSObject jwsObject, String compactSerialization) {
3735

3836
if (payload == null) throw new NullPointerException();
39-
if (payloadObject == null) throw new NullPointerException();
4037

4138
this.payload = payload;
42-
this.payloadObject = payloadObject;
4339
this.jwsObject = jwsObject;
4440
this.compactSerialization = compactSerialization;
4541
}
@@ -91,17 +87,17 @@ public String sign_Ed25519_EdDSA(com.nimbusds.jose.jwk.OctetKeyPair privateKey)
9187
return this.sign(new com.nimbusds.jose.crypto.Ed25519Signer(privateKey), JWSAlgorithm.EdDSA);
9288
}
9389

94-
public String sign_P256K_ES256K(ByteSigner signer) throws JOSEException {
90+
public String sign_secp256k1_ES256K(ByteSigner signer) throws JOSEException {
9591

9692
return this.sign(new JWSSignerAdapter(signer, JWSAlgorithm.ES256K), JWSAlgorithm.ES256K);
9793
}
9894

99-
public String sign_P256K_ES256K(ECKey privateKey) throws JOSEException {
95+
public String sign_secp256k1_ES256K(ECKey privateKey) throws JOSEException {
10096

101-
return this.sign_P256K_ES256K(new P256K_ES256K_PrivateKeySigner(privateKey));
97+
return this.sign_secp256k1_ES256K(new secp256k1_ES256K_PrivateKeySigner(privateKey));
10298
}
10399

104-
public String sign_P256K_ES256K(com.nimbusds.jose.jwk.ECKey privateKey) throws JOSEException {
100+
public String sign_secp256k1_ES256K(com.nimbusds.jose.jwk.ECKey privateKey) throws JOSEException {
105101

106102
return this.sign(new com.nimbusds.jose.crypto.ECDSASigner(privateKey), JWSAlgorithm.ES256K);
107103
}
@@ -145,33 +141,35 @@ public boolean verify_Ed25519_EdDSA(com.nimbusds.jose.jwk.OctetKeyPair publicKey
145141
return this.verify(new com.nimbusds.jose.crypto.Ed25519Verifier(publicKey));
146142
}
147143

148-
public boolean verify_P256K_ES256K(ByteVerifier verifier) throws JOSEException {
144+
public boolean verify_secp256k1_ES256K(ByteVerifier verifier) throws JOSEException {
149145

150146
return this.verify(new JWSVerifierAdapter(verifier, JWSAlgorithm.ES256K));
151147
}
152148

153-
public boolean verify_P256K_ES256K(ECKey publicKey) throws JOSEException {
149+
public boolean verify_secp256k1_ES256K(ECKey publicKey) throws JOSEException {
154150

155-
return this.verify_P256K_ES256K(new P256K_ES256K_PublicKeyVerifier(publicKey));
151+
return this.verify_secp256k1_ES256K(new secp256k1_ES256K_PublicKeyVerifier(publicKey));
156152
}
157153

158-
public boolean verify_P256K_ES256K(com.nimbusds.jose.jwk.ECKey publicKey) throws JOSEException {
154+
public boolean verify_secp256k1_ES256K(com.nimbusds.jose.jwk.ECKey publicKey) throws JOSEException {
159155

160156
return this.verify(new com.nimbusds.jose.crypto.ECDSAVerifier(publicKey));
161157
}
162158

163159
/*
164160
* Helper class
165161
*/
166-
162+
167163
private static class EscapedSlashWorkaroundJWSObject extends JWSObject {
168164

165+
private static final long serialVersionUID = -587898962717783109L;
166+
169167
public EscapedSlashWorkaroundJWSObject(final JWSHeader header, final JWTClaimsSet claimsSet) {
170168

171169
super(header, new Payload(claimsSet.toJSONObject().toJSONString().replace("\\/", "/")));
172170
}
173171
}
174-
172+
175173
/*
176174
* Getters
177175
*/
@@ -181,11 +179,6 @@ public JWTClaimsSet getPayload() {
181179
return this.payload;
182180
}
183181

184-
public T getPayloadObject() {
185-
186-
return this.payloadObject;
187-
}
188-
189182
public JWSObject getJwsObject() {
190183

191184
return this.jwsObject;

src/main/java/com/danubetech/verifiablecredentials/jwt/JwtVerifiableCredential.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import net.minidev.json.JSONObject;
1616

17-
public class JwtVerifiableCredential extends JwtObject<VerifiableCredential> {
17+
public class JwtVerifiableCredential extends JwtWrappingObject<VerifiableCredential> {
1818

1919
public static final String JWT_CLAIM_VC = "vc";
2020

src/main/java/com/danubetech/verifiablecredentials/jwt/JwtVerifiablePresentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.nimbusds.jose.JWSObject;
99
import com.nimbusds.jwt.JWTClaimsSet;
1010

11-
public class JwtVerifiablePresentation extends JwtObject<JwtVerifiableCredential> {
11+
public class JwtVerifiablePresentation extends JwtWrappingObject<JwtVerifiableCredential> {
1212

1313
public static final String JWT_CLAIM_VP = "vp";
1414

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.danubetech.verifiablecredentials.jwt;
2+
3+
import com.nimbusds.jose.JWSObject;
4+
import com.nimbusds.jwt.JWTClaimsSet;
5+
6+
public abstract class JwtWrappingObject <T> extends JwtObject {
7+
8+
private final T payloadObject;
9+
10+
protected JwtWrappingObject(JWTClaimsSet payload, T payloadObject, JWSObject jwsObject, String compactSerialization) {
11+
12+
super(payload, jwsObject, compactSerialization);
13+
14+
if (payloadObject == null) throw new NullPointerException();
15+
16+
this.payloadObject = payloadObject;
17+
}
18+
19+
/*
20+
* Getters
21+
*/
22+
23+
public T getPayloadObject() {
24+
25+
return this.payloadObject;
26+
}
27+
}

src/main/java/com/danubetech/verifiablecredentials/w3ctestsuite/Generator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ static String readInput(File input) throws Exception {
153153
String line;
154154
while ((line = reader.readLine()) != null) buffer.append(line);
155155

156+
reader.close();
157+
156158
return buffer.toString();
157159
}
158160
}

0 commit comments

Comments
 (0)