Skip to content

Commit 3415c1d

Browse files
committed
Use overloaded methods.
Signed-off-by: Markus Sabadello <[email protected]>
1 parent 6747fdb commit 3415c1d

File tree

5 files changed

+22
-28
lines changed

5 files changed

+22
-28
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public String getCompactSerialization() {
145145
return this.compactSerialization;
146146
}
147147

148-
public String toJwtRSA(RSAKey rsaKey) throws JOSEException {
148+
public String toJwt(RSAKey rsaKey) throws JOSEException {
149149

150150
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).build();
151151
SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload());
@@ -158,7 +158,7 @@ public String toJwtRSA(RSAKey rsaKey) throws JOSEException {
158158
return compactSerialization;
159159
}
160160

161-
public String toJwtEd25519(OctetKeyPair octetKeyPair) throws JOSEException {
161+
public String toJwt(OctetKeyPair octetKeyPair) throws JOSEException {
162162

163163
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.EdDSA).build();
164164
SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload());
@@ -171,7 +171,7 @@ public String toJwtEd25519(OctetKeyPair octetKeyPair) throws JOSEException {
171171
return compactSerialization;
172172
}
173173

174-
public String toJwtES256K(ECKey ecKey) throws JOSEException {
174+
public String toJwt(ECKey ecKey) throws JOSEException {
175175

176176
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.ES256K).build();
177177
SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload());

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.danubetech.verifiablecredentials.jwt;
22

33
import java.io.IOException;
4-
import java.security.PrivateKey;
54
import java.util.Date;
65
import java.util.UUID;
76

@@ -11,6 +10,7 @@
1110
import com.nimbusds.jose.JWSHeader;
1211
import com.nimbusds.jose.JWSSigner;
1312
import com.nimbusds.jose.crypto.RSASSASigner;
13+
import com.nimbusds.jose.jwk.RSAKey;
1414
import com.nimbusds.jwt.JWTClaimsSet;
1515
import com.nimbusds.jwt.SignedJWT;
1616

@@ -77,12 +77,12 @@ public String getCompactSerialization() {
7777
return this.compactSerialization;
7878
}
7979

80-
public String toJwt(String algorithm, PrivateKey privateKey) throws JOSEException {
80+
public String toJwt(RSAKey rsaKey) throws JOSEException {
8181

82-
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.parse(algorithm)).build();
82+
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).build();
8383
SignedJWT signedJWT = new SignedJWT(jwsHeader, this.getPayload());
8484

85-
JWSSigner signer = new RSASSASigner(privateKey);
85+
JWSSigner signer = new RSASSASigner(rsaKey);
8686

8787
signedJWT.sign(signer);
8888

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.io.BufferedReader;
44
import java.io.File;
55
import java.io.FileReader;
6-
import java.security.PrivateKey;
7-
import java.security.PublicKey;
86
import java.text.ParseException;
97
import java.util.Arrays;
108
import java.util.List;
@@ -57,12 +55,11 @@ public static void main(String[] args) throws Exception {
5755
output = verifiableCredential.toJsonString();
5856
} else {
5957

60-
PrivateKey privateKey = readPrivateKey(argJwt);
61-
PublicKey publicKey = readPublicKey(argJwt);
58+
RSAKey rsaKey = readRSAKey(argJwt);
6259

6360
if (argDecode) {
6461

65-
JwtVerifiableCredential jwtVerifiableCredential = JwtVerifiableCredential.fromJwt(input, JWSAlgorithm.RS256.getName(), publicKey, false);
62+
JwtVerifiableCredential jwtVerifiableCredential = JwtVerifiableCredential.fromJwt(input, JWSAlgorithm.RS256.getName(), rsaKey.toPublicKey(), false);
6663
VerifiableCredential verifiableCredential = jwtVerifiableCredential.toVerifiableCredential();
6764

6865
output = verifiableCredential.toJsonString();
@@ -73,18 +70,18 @@ public static void main(String[] args) throws Exception {
7370

7471
if (argPresentation) {
7572

76-
jwtVerifiableCredential.toJwt(JWSAlgorithm.RS256.getName(), privateKey);
73+
jwtVerifiableCredential.toJwt(rsaKey);
7774
JwtVerifiablePresentation jwtVerifiablePresentation = JwtVerifiablePresentation.fromJwtVerifiableCredential(jwtVerifiableCredential, argAud);
7875

79-
output = jwtVerifiablePresentation.toJwt(JWSAlgorithm.RS256.getName(), privateKey);
76+
output = jwtVerifiablePresentation.toJwt(rsaKey);
8077
} else {
8178

8279
if (argNoJws) {
8380

8481
output = jwtVerifiableCredential.getPayload().toJSONObject().toJSONString();
8582
} else {
8683

87-
output = jwtVerifiableCredential.toJwt(JWSAlgorithm.RS256.getName(), privateKey);
84+
output = jwtVerifiableCredential.toJwt(rsaKey);
8885
}
8986
}
9087
}
@@ -136,24 +133,14 @@ static String argInput(List<String> argsList) {
136133
return argsList.get(argsList.size()-1);
137134
}
138135

139-
static PrivateKey readPrivateKey(String jwt) throws ParseException, JOSEException {
136+
static RSAKey readRSAKey(String jwt) throws ParseException, JOSEException {
140137

141138
JSONObject jsonObject = JSONObjectUtils.parse(new String(Base64.decodeBase64(jwt)));
142139
JSONObject rs256PrivateKeyJwk = (JSONObject) jsonObject.get("rs256PrivateKeyJwk");
143140

144141
RSAKey jwk = (RSAKey) JWK.parse(rs256PrivateKeyJwk);
145142

146-
return jwk.toPrivateKey();
147-
}
148-
149-
static PublicKey readPublicKey(String jwt) throws ParseException, JOSEException {
150-
151-
JSONObject jsonObject = JSONObjectUtils.parse(new String(Base64.decodeBase64(jwt)));
152-
JSONObject rs256PrivateKeyJwk = (JSONObject) jsonObject.get("rs256PrivateKeyJwk");
153-
154-
RSAKey jwk = (RSAKey) JWK.parse(rs256PrivateKeyJwk);
155-
156-
return jwk.toPublicKey();
143+
return jwk;
157144
}
158145

159146
static String readInput(File input) throws Exception {

src/test/java/com/danubetech/verifiablecredentials/JwtTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void testSign() throws Exception {
3030

3131
assertNotNull(jwtPayload);
3232

33-
String jwtString = jwtVerifiableCredential.toJwt(JWSAlgorithm.RS256.getName(), TestUtil.testRSAPrivateKey);
33+
String jwtString = jwtVerifiableCredential.toJwt(TestUtil.rsaKey);
3434

3535
assertNotNull(jwtString);
3636

src/test/java/com/danubetech/verifiablecredentials/TestUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
import org.apache.commons.codec.binary.Base64;
1313

14+
import com.nimbusds.jose.jwk.RSAKey;
15+
1416
class TestUtil {
1517

1618
static final RSAPrivateKey testRSAPrivateKey;
1719
static final RSAPublicKey testRSAPublicKey;
20+
static final RSAKey rsaKey;
1821

1922
static {
2023

@@ -49,6 +52,10 @@ class TestUtil {
4952

5053
throw new RuntimeException(ex.getMessage(), ex);
5154
}
55+
56+
rsaKey = new com.nimbusds.jose.jwk.RSAKey.Builder(testRSAPublicKey)
57+
.privateKey(testRSAPrivateKey)
58+
.build();
5259
}
5360

5461
static String read(InputStream inputStream) throws Exception {

0 commit comments

Comments
 (0)