class X509AuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) {
        if (authentication instanceof X509AuthenticationToken) {
            if (!isClientCertEnabled) {
                return null;
            }
            String username = getUserid(authentication);
            if (username == null) {
                return null;
            }
            String jwtToken = tokenCreationService.createJwtTokenWithoutCredentials(username);
            Authentication tokenAuthentication = new TokenAuthentication(username, jwtToken);
            tokenAuthentication.setAuthenticated(true);
            return tokenAuthentication;
        } else {
            // This should never happen because of supports method which should fail.
            throw new AuthenticationTokenException("Wrong authentication token. " + authentication.getClass());
        }
    }

}