JwtMacWrapper implements PrimitiveWrapper<JwtMac, JwtMac> { @Override public JwtMac wrap(final PrimitiveSet<JwtMac> primitives) throws GeneralSecurityException { validate(primitives); return new WrappedJwtMac(primitives); } JwtMacWrapper(); @Override JwtMac wrap(final PrimitiveSet<JwtMac> primitives); @Override Class<JwtMac> getPrimitiveClass(); @Override Class<JwtMac> getInputPrimitiveClass(); static void register();  }
@Test public void test_wrapEmpty_throws() throws Exception { PrimitiveSet<JwtMac> primitiveSet = PrimitiveSet.newPrimitiveSet(JwtMac.class); try { wrapper.wrap(primitiveSet); fail(); } catch (GeneralSecurityException e) { } }
@Test public void test_wrapNoPrimary_throws() throws Exception { PrimitiveSet<JwtMac> primitiveSet = PrimitiveSet.newPrimitiveSet(JwtMac.class); primitiveSet.addPrimitive( new JwtHmac("HS256", new SecretKeySpec(Random.randBytes(32), "HMAC")), Keyset.Key.newBuilder() .setKeyId(202020) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.RAW) .build()); try { wrapper.wrap(primitiveSet); fail(); } catch (GeneralSecurityException e) { } }
@Test public void test_wrapNoRaw_throws() throws Exception { PrimitiveSet<JwtMac> primitiveSet = PrimitiveSet.newPrimitiveSet(JwtMac.class); primitiveSet.addPrimitive( new JwtHmac("HS256", new SecretKeySpec(Random.randBytes(32), "HMAC")), Keyset.Key.newBuilder() .setKeyId(202020) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.TINK) .build()); PrimitiveSet.Entry<JwtMac> entry = primitiveSet.addPrimitive( new JwtHmac("HS256", new SecretKeySpec(Random.randBytes(32), "HMAC")), Keyset.Key.newBuilder() .setKeyId(202021) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.RAW) .build()); primitiveSet.setPrimary(entry); try { wrapper.wrap(primitiveSet); fail(); } catch (GeneralSecurityException e) { } }
@Test public void test_wrapSingle_works() throws Exception { PrimitiveSet<JwtMac> primitiveSet = PrimitiveSet.newPrimitiveSet(JwtMac.class); PrimitiveSet.Entry<JwtMac> entry = primitiveSet.addPrimitive( new JwtHmac("HS256", new SecretKeySpec(Random.randBytes(32), "HMAC")), Keyset.Key.newBuilder() .setKeyId(202020) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.RAW) .build()); primitiveSet.setPrimary(entry); JwtMac wrapped = wrapper.wrap(primitiveSet); ToBeSignedJwt tbs = new ToBeSignedJwt.Builder().setJwtId("blah").build(); String compact = wrapped.createCompact(tbs); JwtValidator validator = new JwtValidator.Builder().build(); Jwt token = wrapped.verifyCompact(compact, validator); assertThat(token.getJwtId()).isEqualTo("blah"); }
@Test public void test_wrapMultiple_works() throws Exception { PrimitiveSet<JwtMac> primitiveSet = PrimitiveSet.newPrimitiveSet(JwtMac.class); JwtMac mac1 = new JwtHmac("HS256", new SecretKeySpec(Random.randBytes(32), "HMAC")); primitiveSet.addPrimitive( mac1, Keyset.Key.newBuilder() .setKeyId(202020) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.RAW) .build()); JwtMac mac2 = new JwtHmac("HS256", new SecretKeySpec(Random.randBytes(32), "HMAC")); PrimitiveSet.Entry<JwtMac> entry = primitiveSet.addPrimitive( mac2, Keyset.Key.newBuilder() .setKeyId(202021) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.RAW) .build()); primitiveSet.setPrimary(entry); JwtMac wrapped = wrapper.wrap(primitiveSet); ToBeSignedJwt tbs = new ToBeSignedJwt.Builder().setJwtId("blah").build(); String compact = wrapped.createCompact(tbs); JwtValidator validator = new JwtValidator.Builder().build(); Jwt token = wrapped.verifyCompact(compact, validator); Jwt token2 = mac2.verifyCompact(compact, validator); assertThrows(GeneralSecurityException.class, () -> mac1.verifyCompact(compact, validator)); assertThat(token.getJwtId()).isEqualTo("blah"); assertThat(token.getAlgorithm()).isEqualTo("HS256"); assertThat(token2.getJwtId()).isEqualTo("blah"); assertThat(token2.getAlgorithm()).isEqualTo("HS256"); }