@SuppressWarnings("unchecked") @Test public void shouldFailToAuthenticateUsingJWKIfKeyIdCannotBeObtained() throws Exception { JwkProvider jwkProvider = mock(JwkProvider.class); KeyPair keyPair = RSAKeyPair(); when(jwkProvider.get(eq("key-id"))).thenThrow(JwkException.class); JwtAuthenticationProvider provider = new JwtAuthenticationProvider(jwkProvider, "test-issuer", "test-audience"); Map<String, Object> keyIdHeader = Collections.singletonMap("kid", (Object) "key-id"); String token = JWT.create() .withAudience("test-audience") .withIssuer("test-issuer") .withHeader(keyIdHeader) .sign(Algorithm.RSA256(null, (RSAPrivateKey) keyPair.getPrivate())); Authentication authentication = PreAuthenticatedAuthenticationJsonWebToken.usingToken(token); exception.expect(AuthenticationServiceException.class); exception.expectMessage("Cannot authenticate with jwt"); exception.expectCause(Matchers.<Throwable>instanceOf(JwkException.class)); provider.authenticate(authentication); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } JwtAuthentication jwt = (JwtAuthentication) authentication; try { final Authentication jwtAuth = jwt.verify(jwtVerifier(jwt)); logger.info("Authenticated with jwt with scopes {}", jwtAuth.getAuthorities()); return jwtAuth; } catch (JWTVerificationException e) { throw new BadCredentialsException("Not a valid token", e); } }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } JwtAuthentication jwt = (JwtAuthentication) authentication; try { final Authentication jwtAuth = jwt.verify(jwtVerifier(jwt)); logger.info("Authenticated with jwt with scopes {}", jwtAuth.getAuthorities()); return jwtAuth; } catch (JWTVerificationException e) { throw new BadCredentialsException("Not a valid token", e); } } }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } JwtAuthentication jwt = (JwtAuthentication) authentication; try { final Authentication jwtAuth = jwt.verify(jwtVerifier(jwt)); logger.info("Authenticated with jwt with scopes {}", jwtAuth.getAuthorities()); return jwtAuth; } catch (JWTVerificationException e) { throw new BadCredentialsException("Not a valid token", e); } } JwtAuthenticationProvider(byte[] secret, String issuer, String audience);  JwtAuthenticationProvider(JwkProvider jwkProvider, String issuer, String audience);  JwtAuthenticationProvider(byte[] secret, String[] issuers, String audience);  JwtAuthenticationProvider(JwkProvider jwkProvider,  String[] issuers, String audience); }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } JwtAuthentication jwt = (JwtAuthentication) authentication; try { final Authentication jwtAuth = jwt.verify(jwtVerifier(jwt)); logger.info("Authenticated with jwt with scopes {}", jwtAuth.getAuthorities()); return jwtAuth; } catch (JWTVerificationException e) { throw new BadCredentialsException("Not a valid token", e); } } JwtAuthenticationProvider(byte[] secret, String issuer, String audience);  JwtAuthenticationProvider(JwkProvider jwkProvider, String issuer, String audience);  JwtAuthenticationProvider(byte[] secret, String[] issuers, String audience);  JwtAuthenticationProvider(JwkProvider jwkProvider,  String[] issuers, String audience); @Override boolean supports(Class<?> authentication); @Override Authentication authenticate(Authentication authentication); @SuppressWarnings("unused") JwtAuthenticationProvider withJwtVerifierLeeway(long leeway); }
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } JwtAuthentication jwt = (JwtAuthentication) authentication; try { final Authentication jwtAuth = jwt.verify(jwtVerifier(jwt)); logger.info("Authenticated with jwt with scopes {}", jwtAuth.getAuthorities()); return jwtAuth; } catch (JWTVerificationException e) { throw new BadCredentialsException("Not a valid token", e); } } JwtAuthenticationProvider(byte[] secret, String issuer, String audience);  JwtAuthenticationProvider(JwkProvider jwkProvider, String issuer, String audience);  JwtAuthenticationProvider(byte[] secret, String[] issuers, String audience);  JwtAuthenticationProvider(JwkProvider jwkProvider,  String[] issuers, String audience); @Override boolean supports(Class<?> authentication); @Override Authentication authenticate(Authentication authentication); @SuppressWarnings("unused") JwtAuthenticationProvider withJwtVerifierLeeway(long leeway);  }