KeycloakSigningKeyResolver extends SigningKeyResolverAdapter { @Override public Key resolveSigningKey(JwsHeader header, String plaintext) { if (MACHINE_TOKEN_KIND.equals(header.get("kind"))) { throw new MachineTokenJwtException(); } return getJwtPublicKey(header); } @Inject  KeycloakSigningKeyResolver(JwkProvider jwkProvider); @Override Key resolveSigningKey(JwsHeader header, String plaintext); @Override Key resolveSigningKey(JwsHeader header, Claims claims);  }
@Test(expectedExceptions = MachineTokenJwtException.class) public void shouldThrowMachineTokenExceptionOnMachineTokensWithPlainText() { final Map<String, Object> param = new HashMap<>(); param.put("kind", MACHINE_TOKEN_KIND); DefaultJwsHeader header = new DefaultJwsHeader(param); signingKeyResolver.resolveSigningKey(header, "plaintext"); verifyNoMoreInteractions(jwkProvider); }
@Test(expectedExceptions = MachineTokenJwtException.class) public void shouldThrowMachineTokenExceptionOnMachineTokensWithClaims() { final Map<String, Object> param = new HashMap<>(); param.put("kind", MACHINE_TOKEN_KIND); DefaultJwsHeader header = new DefaultJwsHeader(param); signingKeyResolver.resolveSigningKey(header, new DefaultClaims()); verifyNoMoreInteractions(jwkProvider); }
@Test(expectedExceptions = JwtException.class) public void shouldThrowJwtExceptionifNoKeyIdHeader() { signingKeyResolver.resolveSigningKey(new DefaultJwsHeader(), "plaintext"); verifyNoMoreInteractions(jwkProvider); }
@Test public void shouldReturnPublicKey() throws Exception { final String kid = "123"; final Jwk jwk = mock(Jwk.class); final Map<String, Object> param = new HashMap<>(); param.put("kid", kid); final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); final KeyPair keyPair = kpg.generateKeyPair(); when(jwk.getPublicKey()).thenReturn(keyPair.getPublic()); when(jwkProvider.get(eq(kid))).thenReturn(jwk); Key actual = signingKeyResolver.resolveSigningKey(new DefaultJwsHeader(param), "plaintext"); assertEquals(actual, keyPair.getPublic()); }