|
| 1 | +package com.auth0.jwt; |
| 2 | + |
| 3 | +import com.auth0.jwt.algorithms.Algorithm; |
| 4 | +import net.jodah.concurrentunit.Waiter; |
| 5 | +import org.junit.AfterClass; |
| 6 | +import org.junit.BeforeClass; |
| 7 | +import org.junit.Rule; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.rules.ExpectedException; |
| 10 | + |
| 11 | +import java.security.interfaces.ECKey; |
| 12 | +import java.security.interfaces.RSAKey; |
| 13 | +import java.util.concurrent.ExecutorService; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.TimeoutException; |
| 16 | + |
| 17 | +import static com.auth0.jwt.PemUtils.readPublicKeyFromFile; |
| 18 | + |
| 19 | +public class ConcurrentVerifyTest { |
| 20 | + |
| 21 | + private static final long TIMEOUT = 60 * 1000 * 1000; //1 min |
| 22 | + private static final int THREAD_COUNT = 1500; |
| 23 | + private static final int REPEAT_COUNT = 10000; |
| 24 | + private static final String PUBLIC_KEY_FILE = "src/test/resources/rsa-public.pem"; |
| 25 | + private static final String PUBLIC_KEY_FILE_256 = "src/test/resources/ec256-key-public.pem"; |
| 26 | + private static final String PUBLIC_KEY_FILE_384 = "src/test/resources/ec384-key-public.pem"; |
| 27 | + private static final String PUBLIC_KEY_FILE_512 = "src/test/resources/ec512-key-public.pem"; |
| 28 | + |
| 29 | + @Rule |
| 30 | + public ExpectedException exception = ExpectedException.none(); |
| 31 | + private static ExecutorService executor; |
| 32 | + |
| 33 | + @BeforeClass |
| 34 | + public static void beforeAll() throws Exception { |
| 35 | + executor = Executors.newFixedThreadPool(THREAD_COUNT); |
| 36 | + } |
| 37 | + |
| 38 | + @AfterClass |
| 39 | + public static void afterAll() throws Exception { |
| 40 | + executor.shutdown(); |
| 41 | + } |
| 42 | + |
| 43 | + @SuppressWarnings("Convert2Lambda") |
| 44 | + private void concurrentVerify(final JWTVerifier verifier, final String token) throws TimeoutException { |
| 45 | + final Waiter waiter = new Waiter(); |
| 46 | + for (int i = 0; i < REPEAT_COUNT; i++) { |
| 47 | + executor.execute(new Runnable() { |
| 48 | + @Override |
| 49 | + public void run() { |
| 50 | + JWT result = null; |
| 51 | + try { |
| 52 | + result = verifier.verify(token); |
| 53 | + } catch (Exception e) { |
| 54 | + waiter.fail(e); |
| 55 | + } |
| 56 | + waiter.assertNotNull(result); |
| 57 | + waiter.resume(); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + waiter.await(TIMEOUT, REPEAT_COUNT); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void shouldPassHMAC256Verification() throws Exception { |
| 67 | + Algorithm algorithm = Algorithm.HMAC256("secret"); |
| 68 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 69 | + String token = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M"; |
| 70 | + |
| 71 | + concurrentVerify(verifier, token); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void shouldPassHMAC384Verification() throws Exception { |
| 76 | + String token = "eyJhbGciOiJIUzM4NCIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.uztpK_wUMYJhrRv8SV-1LU4aPnwl-EM1q-wJnqgyb5DHoDteP6lN_gE1xnZJH5vw"; |
| 77 | + Algorithm algorithm = Algorithm.HMAC384("secret"); |
| 78 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 79 | + |
| 80 | + concurrentVerify(verifier, token); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void shouldPassHMAC512Verification() throws Exception { |
| 85 | + String token = "eyJhbGciOiJIUzUxMiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.VUo2Z9SWDV-XcOc_Hr6Lff3vl7L9e5Vb8ThXpmGDFjHxe3Dr1ZBmUChYF-xVA7cAdX1P_D4ZCUcsv3IefpVaJw"; |
| 86 | + Algorithm algorithm = Algorithm.HMAC512("secret"); |
| 87 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 88 | + |
| 89 | + concurrentVerify(verifier, token); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void shouldPassRSA256Verification() throws Exception { |
| 94 | + String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.dxXF3MdsyW-AuvwJpaQtrZ33fAde9xWxpLIg9cO2tMLH2GSRNuLAe61KsJusZhqZB9Iy7DvflcmRz-9OZndm6cj_ThGeJH2LLc90K83UEvvRPo8l85RrQb8PcanxCgIs2RcZOLygERizB3pr5icGkzR7R2y6zgNCjKJ5_NJ6EiZsGN6_nc2PRK_DbyY-Wn0QDxIxKoA5YgQJ9qafe7IN980pXvQv2Z62c3XR8dYuaXBqhthBj-AbaFHEpZapN-V-TmuLNzR2MCB6Xr7BYMuCaqWf_XU8og4XNe8f_8w9Wv5vvgqMM1KhqVpG5VdMJv4o_L4NoCROHhtUQSLRh2M9cA"; |
| 95 | + Algorithm algorithm = Algorithm.RSA256((RSAKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA")); |
| 96 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 97 | + |
| 98 | + concurrentVerify(verifier, token); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void shouldPassRSA384Verification() throws Exception { |
| 103 | + String token = "eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.TZlWjXObwGSQOiu2oMq8kiKz0_BR7bbBddNL6G8eZ_GoR82BXOZDqNrQr7lb_M-78XGBguWLWNIdYhzgxOUL9EoCJlrqVm9s9vo6G8T1sj1op-4TbjXZ61TwIvrJee9BvPLdKUJ9_fp1Js5kl6yXkst40Th8Auc5as4n49MLkipjpEhKDKaENKHpSubs1ripSz8SCQZSofeTM_EWVwSw7cpiM8Fy8jOPvWG8Xz4-e3ODFowvHVsDcONX_4FTMNbeRqDuHq2ZhCJnEfzcSJdrve_5VD5fM1LperBVslTrOxIgClOJ3RmM7-WnaizJrWP3D6Z9OLxPxLhM6-jx6tcxEw"; |
| 104 | + Algorithm algorithm = Algorithm.RSA384((RSAKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA")); |
| 105 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 106 | + |
| 107 | + concurrentVerify(verifier, token); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void shouldPassRSA512Verification() throws Exception { |
| 112 | + String token = "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mvL5LoMyIrWYjk5umEXZTmbyIrkbbcVPUkvdGZbu0qFBxGOf0nXP5PZBvPcOu084lvpwVox5n3VaD4iqzW-PsJyvKFgi5TnwmsbKchAp7JexQEsQOnTSGcfRqeUUiBZqRQdYsho71oAB3T4FnalDdFEpM-fztcZY9XqKyayqZLreTeBjqJm4jfOWH7KfGBHgZExQhe96NLq1UA9eUyQwdOA1Z0SgXe4Ja5PxZ6Fm37KnVDtDlNnY4JAAGFo6y74aGNnp_BKgpaVJCGFu1f1S5xCQ1HSvs8ZSdVWs5NgawW3wRd0kRt_GJ_Y3mIwiF4qUyHWGtsSHu_qjVdCTtbFyow"; |
| 113 | + Algorithm algorithm = Algorithm.RSA512((RSAKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA")); |
| 114 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 115 | + |
| 116 | + concurrentVerify(verifier, token); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void shouldPassECDSA256VerificationWithJOSESignature() throws Exception { |
| 121 | + String token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g"; |
| 122 | + ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"); |
| 123 | + Algorithm algorithm = Algorithm.ECDSA256(key); |
| 124 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 125 | + |
| 126 | + concurrentVerify(verifier, token); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void shouldPassECDSA256VerificationWithDERSignature() throws Exception { |
| 131 | + String token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.MEYCIQDiJWTf5jS/hFPj/0hpCWn7x1n/h+xPMjKWCs9MMusS9AIhAMcFPJVLe2A9uvb8hl8sRO2IpGoKDRpDmyH14ixNPAHW"; |
| 132 | + ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"); |
| 133 | + Algorithm algorithm = Algorithm.ECDSA256(key); |
| 134 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 135 | + |
| 136 | + concurrentVerify(verifier, token); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void shouldPassECDSA384VerificationWithJOSESignature() throws Exception { |
| 141 | + String token = "eyJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJhdXRoMCJ9.50UU5VKNdF1wfykY8jQBKpvuHZoe6IZBJm5NvoB8bR-hnRg6ti-CHbmvoRtlLfnHfwITa_8cJMy6TenMC2g63GQHytc8rYoXqbwtS4R0Ko_AXbLFUmfxnGnMC6v4MS_z"; |
| 142 | + ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_384, "EC"); |
| 143 | + Algorithm algorithm = Algorithm.ECDSA384(key); |
| 144 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 145 | + |
| 146 | + concurrentVerify(verifier, token); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void shouldPassECDSA384VerificationWithDERSignature() throws Exception { |
| 151 | + String token = "eyJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJhdXRoMCJ9.MGUCMQDnRRTlUo10XXB/KRjyNAEqm+4dmh7ohkEmbk2+gHxtH6GdGDq2L4Idua+hG2Ut+ccCMH8CE2v/HCTMuk3pzAtoOtxkB8rXPK2KF6m8LUuEdCqPwF2yxVJn8ZxpzAur+DEv8w=="; |
| 152 | + ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_384, "EC"); |
| 153 | + Algorithm algorithm = Algorithm.ECDSA384(key); |
| 154 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 155 | + |
| 156 | + concurrentVerify(verifier, token); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void shouldPassECDSA512VerificationWithJOSESignature() throws Exception { |
| 161 | + String token = "eyJhbGciOiJFUzUxMiJ9.eyJpc3MiOiJhdXRoMCJ9.AeCJPDIsSHhwRSGZCY6rspi8zekOw0K9qYMNridP1Fu9uhrA1QrG-EUxXlE06yvmh2R7Rz0aE7kxBwrnq8L8aOBCAYAsqhzPeUvyp8fXjjgs0Eto5I0mndE2QHlgcMSFASyjHbU8wD2Rq7ZNzGQ5b2MZfpv030WGUajT-aZYWFUJHVg2"; |
| 162 | + ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"); |
| 163 | + Algorithm algorithm = Algorithm.ECDSA512(key); |
| 164 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 165 | + |
| 166 | + concurrentVerify(verifier, token); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void shouldPassECDSA512VerificationWithDERSignature() throws Exception { |
| 171 | + String token = "eyJhbGciOiJFUzUxMiJ9.eyJpc3MiOiJhdXRoMCJ9.MIGIAkIB4Ik8MixIeHBFIZkJjquymLzN6Q7DQr2pgw2uJ0/UW726GsDVCsb4RTFeUTTrK+aHZHtHPRoTuTEHCuerwvxo4EICQgGALKocz3lL8qfH1444LNBLaOSNJp3RNkB5YHDEhQEsox21PMA9kau2TcxkOW9jGX6b9N9FhlGo0/mmWFhVCR1YNg=="; |
| 172 | + ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"); |
| 173 | + Algorithm algorithm = Algorithm.ECDSA512(key); |
| 174 | + JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build(); |
| 175 | + |
| 176 | + concurrentVerify(verifier, token); |
| 177 | + } |
| 178 | +} |
0 commit comments