This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JwtVerifiableCredentialTest.java
73 lines (59 loc) · 3.16 KB
/
JwtVerifiableCredentialTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.medibloc.vc.verifiable.jwt;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.jwk.Curve;
import com.nimbusds.jose.jwk.ECKey;
import com.nimbusds.jose.jwk.gen.ECKeyGenerator;
import org.junit.Test;
import org.medibloc.vc.VerifiableCredentialException;
import org.medibloc.vc.model.Credential;
import org.medibloc.vc.model.CredentialTest;
import java.net.MalformedURLException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.text.ParseException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class JwtVerifiableCredentialTest {
@Test
public void createAndVerify() throws MalformedURLException, VerifiableCredentialException, ParseException, JOSEException, InvalidKeySpecException, NoSuchAlgorithmException {
Credential credential = CredentialTest.buildCredential();
ECKey ecJWK = new ECKeyGenerator(Curve.SECP256K1).generate();
String nonce = "this-is-random";
JwtVerifiableCredential vc = new JwtVerifiableCredential(
credential, "ES256K", credential.getIssuer().getId() + "#key1", ecJWK.toECPrivateKey(), nonce
);
assertNotNull(vc);
assertEquals(credential, vc.getCredential());
vc.verify(ecJWK.toECPublicKey(), nonce);
assertEquals(vc.getJwt(), vc.serialize());
}
@Test(expected = VerifiableCredentialException.class)
public void createWithInvalidAlgo() throws MalformedURLException, VerifiableCredentialException, ParseException, JOSEException {
Credential credential = CredentialTest.buildCredential();
ECKey ecJWK = new ECKeyGenerator(Curve.SECP256K1).generate();
new JwtVerifiableCredential(
credential, "INVALID", credential.getIssuer().getId() + "#key1", ecJWK.toECPrivateKey(), "this-is-random"
);
}
@Test(expected = VerifiableCredentialException.class)
public void signatureVerificationFailure() throws ParseException, VerifiableCredentialException, MalformedURLException, JOSEException {
Credential credential = CredentialTest.buildCredential();
ECKey ecJWK1 = new ECKeyGenerator(Curve.SECP256K1).generate();
String nonce = "this-is-random";
JwtVerifiableCredential vc = new JwtVerifiableCredential(
credential, "ES256K", credential.getIssuer().getId() + "#key1", ecJWK1.toECPrivateKey(), nonce
);
ECKey ecJWK2 = new ECKeyGenerator(Curve.SECP256K1).generate();
vc.verify(ecJWK2.toECPublicKey(), nonce);
}
@Test(expected = VerifiableCredentialException.class)
public void nonceVerificationFailure() throws ParseException, VerifiableCredentialException, MalformedURLException, JOSEException {
Credential credential = CredentialTest.buildCredential();
ECKey ecJWK = new ECKeyGenerator(Curve.SECP256K1).generate();
String nonce = "this-is-random";
JwtVerifiableCredential vc = new JwtVerifiableCredential(
credential, "ES256K", credential.getIssuer().getId() + "#key1", ecJWK.toECPrivateKey(), nonce
);
vc.verify(ecJWK.toECPublicKey(), "wrong-nonce");
}
}