@Override
  public UserIdentity login(String username, Object credentials, ServletRequest request) {
    if (!(credentials instanceof SignedJWT)) {
      return null;
    }
    if (!(request instanceof HttpServletRequest)) {
      return null;
    }

    SignedJWT jwtToken = (SignedJWT) credentials;
    JWTClaimsSet claimsSet;
    boolean valid;
    try {
      claimsSet = jwtToken.getJWTClaimsSet();
      valid = validateToken(jwtToken, claimsSet, username);
    } catch (ParseException e) {
      JWT_LOGGER.warn(String.format("%s: Couldn't parse a JWT token", username), e);
      return null;
    }
    if (valid) {
      String serializedToken = (String) request.getAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE);
      UserIdentity rolesDelegate = _authorizationService.getUserIdentity((HttpServletRequest) request, username);
      if (rolesDelegate == null) {
        return null;
      } else {
        return getUserIdentity(jwtToken, claimsSet, serializedToken, username, rolesDelegate);
      }
    } else {
      return null;
    }
  }
@Test
  public void testFailAudienceValidation() throws Exception {
    UserStore testUserStore = new UserStore();
    testUserStore.addUser(TEST_USER, SecurityUtils.NO_CREDENTIAL, new String[] {"USER"});
    TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER, Arrays.asList("A", "B"));
    JwtLoginService loginService = new JwtLoginService(
        new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), Arrays.asList("C", "D"));

    SignedJWT jwtToken = SignedJWT.parse(tokenAndKeys.token());
    HttpServletRequest request = mock(HttpServletRequest.class);

    UserIdentity identity = loginService.login(TEST_USER, jwtToken, request);
    assertNull(identity);
  }