|
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.security.GeneralSecurityException; |
5 | | -import java.security.PublicKey; |
6 | | -import java.security.interfaces.RSAPublicKey; |
7 | 5 | import java.text.ParseException; |
8 | 6 | import java.util.Date; |
9 | 7 |
|
10 | 8 | import com.danubetech.verifiablecredentials.VerifiableCredential; |
11 | 9 | import com.fasterxml.jackson.core.JsonParseException; |
12 | 10 | import com.nimbusds.jose.JOSEException; |
13 | | -import com.nimbusds.jose.JWSAlgorithm; |
14 | | -import com.nimbusds.jose.JWSHeader; |
15 | | -import com.nimbusds.jose.JWSSigner; |
16 | | -import com.nimbusds.jose.JWSVerifier; |
17 | | -import com.nimbusds.jose.crypto.ECDSASigner; |
18 | | -import com.nimbusds.jose.crypto.Ed25519Signer; |
19 | | -import com.nimbusds.jose.crypto.RSASSASigner; |
20 | | -import com.nimbusds.jose.crypto.RSASSAVerifier; |
21 | | -import com.nimbusds.jose.jwk.ECKey; |
22 | | -import com.nimbusds.jose.jwk.OctetKeyPair; |
23 | | -import com.nimbusds.jose.jwk.RSAKey; |
| 11 | +import com.nimbusds.jose.JWSObject; |
24 | 12 | import com.nimbusds.jwt.JWTClaimsSet; |
25 | 13 | import com.nimbusds.jwt.SignedJWT; |
26 | 14 |
|
27 | 15 | import net.minidev.json.JSONObject; |
28 | 16 |
|
29 | | -public class JwtVerifiableCredential { |
| 17 | +public class JwtVerifiableCredential extends JwtObject<VerifiableCredential> { |
30 | 18 |
|
31 | 19 | public static final String JWT_CLAIM_VC = "vc"; |
32 | 20 |
|
33 | | - private final JWTClaimsSet payload; |
34 | | - private final VerifiableCredential payloadVerifiableCredential; |
| 21 | + private JwtVerifiableCredential(JWTClaimsSet payload, VerifiableCredential payloadObject, JWSObject jwsObject, String compactSerialization) { |
35 | 22 |
|
36 | | - private String compactSerialization; |
37 | | - |
38 | | - private JwtVerifiableCredential(JWTClaimsSet payload, VerifiableCredential payloadVerifiableCredential, String compactSerialization) { |
39 | | - |
40 | | - if (payload == null) throw new NullPointerException(); |
41 | | - if (payloadVerifiableCredential == null) throw new NullPointerException(); |
42 | | - |
43 | | - this.payload = payload; |
44 | | - this.payloadVerifiableCredential = payloadVerifiableCredential; |
45 | | - this.compactSerialization = compactSerialization; |
| 23 | + super(payload, payloadObject, jwsObject, compactSerialization); |
46 | 24 | } |
47 | 25 |
|
48 | | - public static JwtVerifiableCredential fromJwt(String jwt, String algorithm, PublicKey publicKey, boolean doValidate) throws GeneralSecurityException, ParseException, JOSEException, JsonParseException, IOException { |
49 | | - |
50 | | - boolean validate; |
51 | | - |
52 | | - SignedJWT signedJWT = SignedJWT.parse(jwt); |
| 26 | + public static JwtVerifiableCredential fromCompactSerialization(String compactSerialization) throws GeneralSecurityException, ParseException, JOSEException, JsonParseException, IOException { |
53 | 27 |
|
54 | | - if (doValidate) { |
| 28 | + SignedJWT signedJWT = SignedJWT.parse(compactSerialization); |
55 | 29 |
|
56 | | - JWSVerifier verifier = new RSASSAVerifier((RSAPublicKey) publicKey); |
57 | | - validate = signedJWT.verify(verifier); |
58 | | - |
59 | | - if (! validate) throw new GeneralSecurityException("Invalid signature: " + jwt); |
60 | | - } |
61 | | - |
62 | | - JWTClaimsSet jwtPayload = signedJWT.getJWTClaimsSet(); |
63 | | - JSONObject jsonLdObject = (JSONObject) jwtPayload.getClaims().get(JWT_CLAIM_VC); |
| 30 | + JWTClaimsSet payload = signedJWT.getJWTClaimsSet(); |
| 31 | + JSONObject jsonLdObject = (JSONObject) payload.getClaims().get(JWT_CLAIM_VC); |
64 | 32 | VerifiableCredential payloadVerifiableCredential = VerifiableCredential.fromJsonString(jsonLdObject.toJSONString(), false); |
65 | 33 |
|
66 | | - return new JwtVerifiableCredential(jwtPayload, payloadVerifiableCredential, jwt); |
67 | | - } |
68 | | - |
69 | | - public static JwtVerifiableCredential fromJwt(String jwt, String algorithm, PublicKey publicKey) throws GeneralSecurityException, ParseException, JOSEException, JsonParseException, IOException { |
70 | | - |
71 | | - return fromJwt(jwt, algorithm, publicKey, true); |
| 34 | + return new JwtVerifiableCredential(payload, payloadVerifiableCredential, signedJWT, compactSerialization); |
72 | 35 | } |
73 | 36 |
|
74 | 37 | public static JwtVerifiableCredential fromVerifiableCredential(VerifiableCredential verifiableCredential, String aud) { |
@@ -122,75 +85,23 @@ public static JwtVerifiableCredential fromVerifiableCredential(VerifiableCredent |
122 | 85 |
|
123 | 86 | payloadBuilder.claim(JWT_CLAIM_VC, payloadVerifiableCredential.getJsonLdObject()); |
124 | 87 |
|
125 | | - return new JwtVerifiableCredential(payloadBuilder.build(), payloadVerifiableCredential, null); |
| 88 | + JWTClaimsSet payload = payloadBuilder.build(); |
| 89 | + |
| 90 | + return new JwtVerifiableCredential(payload, payloadVerifiableCredential, null, null); |
126 | 91 | } |
127 | 92 |
|
128 | 93 | public static JwtVerifiableCredential fromVerifiableCredential(VerifiableCredential verifiableCredential) { |
129 | 94 |
|
130 | 95 | return fromVerifiableCredential(verifiableCredential, null); |
131 | 96 | } |
132 | 97 |
|
133 | | - public JWTClaimsSet getPayload() { |
134 | | - |
135 | | - return this.payload; |
136 | | - } |
137 | | - |
138 | | - public VerifiableCredential getPayloadVerifiableCredential() { |
139 | | - |
140 | | - return this.payloadVerifiableCredential; |
141 | | - } |
142 | | - |
143 | | - public String getCompactSerialization() { |
144 | | - |
145 | | - return this.compactSerialization; |
146 | | - } |
147 | | - |
148 | | - public String toJwt(RSAKey rsaKey) throws JOSEException { |
149 | | - |
150 | | - JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).build(); |
151 | | - SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload()); |
152 | | - |
153 | | - JWSSigner signer = new RSASSASigner(rsaKey); |
154 | | - |
155 | | - signedJWT.sign(signer); |
156 | | - |
157 | | - this.compactSerialization = signedJWT.serialize(); |
158 | | - return compactSerialization; |
159 | | - } |
160 | | - |
161 | | - public String toJwt(OctetKeyPair octetKeyPair) throws JOSEException { |
162 | | - |
163 | | - JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.EdDSA).build(); |
164 | | - SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload()); |
165 | | - |
166 | | - JWSSigner signer = new Ed25519Signer(octetKeyPair); |
167 | | - |
168 | | - signedJWT.sign(signer); |
169 | | - |
170 | | - this.compactSerialization = signedJWT.serialize(); |
171 | | - return compactSerialization; |
172 | | - } |
173 | | - |
174 | | - public String toJwt(ECKey ecKey) throws JOSEException { |
175 | | - |
176 | | - JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.ES256K).build(); |
177 | | - SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload()); |
178 | | - |
179 | | - JWSSigner signer = new ECDSASigner(ecKey); |
180 | | - |
181 | | - signedJWT.sign(signer); |
182 | | - |
183 | | - this.compactSerialization = signedJWT.serialize(); |
184 | | - return compactSerialization; |
185 | | - } |
186 | | - |
187 | 98 | public VerifiableCredential toVerifiableCredential() { |
188 | 99 |
|
189 | 100 | VerifiableCredential verifiableCredential; |
190 | 101 |
|
191 | 102 | try { |
192 | 103 |
|
193 | | - verifiableCredential = VerifiableCredential.fromJsonString(this.getPayloadVerifiableCredential().toJsonString(), false); |
| 104 | + verifiableCredential = VerifiableCredential.fromJsonString(this.getPayloadObject().toJsonString(), false); |
194 | 105 | } catch (IOException ex) { |
195 | 106 |
|
196 | 107 | throw new RuntimeException(ex.getMessage(), ex); |
|
0 commit comments