@Ignore("Until EasyMock 3.7 upgrade - waiting for release") @Test public void testOneExceptionAndAuthenticates() { Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); expect(mockFileProvider.authenticate(authentication)).andReturn(SUCCESSFUL_AUTHENTICATION); replay(mockFileProvider, mockSimpleProvider, mockExternalServerProvider); Authentication authenticationResult = provider.authenticate(authentication); assertSame(authenticationResult, SUCCESSFUL_AUTHENTICATION); verify(mockFileProvider, mockSimpleProvider, mockExternalServerProvider); }
@Override public Authentication authenticate(Authentication inAuthentication) throws AuthenticationException { logger.info("Authenticating user:" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); logger.info("authentication.class=" + inAuthentication.getClass().getName()); HashMap<String, Object> auditRecord = new HashMap<>(); auditRecord.put("user", inAuthentication.getName()); auditRecord.put("principal", inAuthentication.getPrincipal().toString()); auditRecord.put("auth_class", inAuthentication.getClass().getName()); if (inAuthentication instanceof UsernamePasswordAuthenticationToken) { UsernamePasswordAuthenticationToken authClass = (UsernamePasswordAuthenticationToken) inAuthentication; Object details = authClass.getDetails(); if (details instanceof WebAuthenticationDetails) { WebAuthenticationDetails webAuthentication = (WebAuthenticationDetails) details; auditRecord.put("remote_ip", webAuthentication.getRemoteAddress()); auditRecord.put("session", webAuthentication.getSessionId()); } } boolean isSuccess = false; try { Authentication authentication = inAuthentication; AuthenticationException authException = null; for (AuthMethod authMethod : AuthMethod.values()) { try { authentication = doAuth(authentication, authMethod); if (authentication != null && authentication.isAuthenticated()) { logger.info("Authenticated using method=" + authMethod.name() + ", user=" + authentication.getName()); auditRecord.put("result", "allowed"); isSuccess = true; auditRecord.put("authType", authMethod.name()); return authentication; } } catch (AuthenticationException ex) { if (authException == null) { authException = ex; } } catch (Exception e) { logger.error(e, e.getCause()); } } auditRecord.put("result", "denied"); logger.warn("Authentication failed for user=" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); if (authException != null) { auditRecord.put("reason", authException.getMessage()); throw authException; } return authentication; } finally { try { String jsonStr = JSONUtil.toJson(auditRecord); auditLogger.log(isSuccess ? Level.INFO : Level.WARN, jsonStr); } catch (Exception e) { logger.error("Unable to add audit log entry", e); } } }
LogsearchAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication inAuthentication) throws AuthenticationException { logger.info("Authenticating user:" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); logger.info("authentication.class=" + inAuthentication.getClass().getName()); HashMap<String, Object> auditRecord = new HashMap<>(); auditRecord.put("user", inAuthentication.getName()); auditRecord.put("principal", inAuthentication.getPrincipal().toString()); auditRecord.put("auth_class", inAuthentication.getClass().getName()); if (inAuthentication instanceof UsernamePasswordAuthenticationToken) { UsernamePasswordAuthenticationToken authClass = (UsernamePasswordAuthenticationToken) inAuthentication; Object details = authClass.getDetails(); if (details instanceof WebAuthenticationDetails) { WebAuthenticationDetails webAuthentication = (WebAuthenticationDetails) details; auditRecord.put("remote_ip", webAuthentication.getRemoteAddress()); auditRecord.put("session", webAuthentication.getSessionId()); } } boolean isSuccess = false; try { Authentication authentication = inAuthentication; AuthenticationException authException = null; for (AuthMethod authMethod : AuthMethod.values()) { try { authentication = doAuth(authentication, authMethod); if (authentication != null && authentication.isAuthenticated()) { logger.info("Authenticated using method=" + authMethod.name() + ", user=" + authentication.getName()); auditRecord.put("result", "allowed"); isSuccess = true; auditRecord.put("authType", authMethod.name()); return authentication; } } catch (AuthenticationException ex) { if (authException == null) { authException = ex; } } catch (Exception e) { logger.error(e, e.getCause()); } } auditRecord.put("result", "denied"); logger.warn("Authentication failed for user=" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); if (authException != null) { auditRecord.put("reason", authException.getMessage()); throw authException; } return authentication; } finally { try { String jsonStr = JSONUtil.toJson(auditRecord); auditLogger.log(isSuccess ? Level.INFO : Level.WARN, jsonStr); } catch (Exception e) { logger.error("Unable to add audit log entry", e); } } } }
LogsearchAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication inAuthentication) throws AuthenticationException { logger.info("Authenticating user:" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); logger.info("authentication.class=" + inAuthentication.getClass().getName()); HashMap<String, Object> auditRecord = new HashMap<>(); auditRecord.put("user", inAuthentication.getName()); auditRecord.put("principal", inAuthentication.getPrincipal().toString()); auditRecord.put("auth_class", inAuthentication.getClass().getName()); if (inAuthentication instanceof UsernamePasswordAuthenticationToken) { UsernamePasswordAuthenticationToken authClass = (UsernamePasswordAuthenticationToken) inAuthentication; Object details = authClass.getDetails(); if (details instanceof WebAuthenticationDetails) { WebAuthenticationDetails webAuthentication = (WebAuthenticationDetails) details; auditRecord.put("remote_ip", webAuthentication.getRemoteAddress()); auditRecord.put("session", webAuthentication.getSessionId()); } } boolean isSuccess = false; try { Authentication authentication = inAuthentication; AuthenticationException authException = null; for (AuthMethod authMethod : AuthMethod.values()) { try { authentication = doAuth(authentication, authMethod); if (authentication != null && authentication.isAuthenticated()) { logger.info("Authenticated using method=" + authMethod.name() + ", user=" + authentication.getName()); auditRecord.put("result", "allowed"); isSuccess = true; auditRecord.put("authType", authMethod.name()); return authentication; } } catch (AuthenticationException ex) { if (authException == null) { authException = ex; } } catch (Exception e) { logger.error(e, e.getCause()); } } auditRecord.put("result", "denied"); logger.warn("Authentication failed for user=" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); if (authException != null) { auditRecord.put("reason", authException.getMessage()); throw authException; } return authentication; } finally { try { String jsonStr = JSONUtil.toJson(auditRecord); auditLogger.log(isSuccess ? Level.INFO : Level.WARN, jsonStr); } catch (Exception e) { logger.error("Unable to add audit log entry", e); } } }  }
LogsearchAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication inAuthentication) throws AuthenticationException { logger.info("Authenticating user:" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); logger.info("authentication.class=" + inAuthentication.getClass().getName()); HashMap<String, Object> auditRecord = new HashMap<>(); auditRecord.put("user", inAuthentication.getName()); auditRecord.put("principal", inAuthentication.getPrincipal().toString()); auditRecord.put("auth_class", inAuthentication.getClass().getName()); if (inAuthentication instanceof UsernamePasswordAuthenticationToken) { UsernamePasswordAuthenticationToken authClass = (UsernamePasswordAuthenticationToken) inAuthentication; Object details = authClass.getDetails(); if (details instanceof WebAuthenticationDetails) { WebAuthenticationDetails webAuthentication = (WebAuthenticationDetails) details; auditRecord.put("remote_ip", webAuthentication.getRemoteAddress()); auditRecord.put("session", webAuthentication.getSessionId()); } } boolean isSuccess = false; try { Authentication authentication = inAuthentication; AuthenticationException authException = null; for (AuthMethod authMethod : AuthMethod.values()) { try { authentication = doAuth(authentication, authMethod); if (authentication != null && authentication.isAuthenticated()) { logger.info("Authenticated using method=" + authMethod.name() + ", user=" + authentication.getName()); auditRecord.put("result", "allowed"); isSuccess = true; auditRecord.put("authType", authMethod.name()); return authentication; } } catch (AuthenticationException ex) { if (authException == null) { authException = ex; } } catch (Exception e) { logger.error(e, e.getCause()); } } auditRecord.put("result", "denied"); logger.warn("Authentication failed for user=" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); if (authException != null) { auditRecord.put("reason", authException.getMessage()); throw authException; } return authentication; } finally { try { String jsonStr = JSONUtil.toJson(auditRecord); auditLogger.log(isSuccess ? Level.INFO : Level.WARN, jsonStr); } catch (Exception e) { logger.error("Unable to add audit log entry", e); } } }  @Override Authentication authenticate(Authentication inAuthentication); }
LogsearchAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication inAuthentication) throws AuthenticationException { logger.info("Authenticating user:" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); logger.info("authentication.class=" + inAuthentication.getClass().getName()); HashMap<String, Object> auditRecord = new HashMap<>(); auditRecord.put("user", inAuthentication.getName()); auditRecord.put("principal", inAuthentication.getPrincipal().toString()); auditRecord.put("auth_class", inAuthentication.getClass().getName()); if (inAuthentication instanceof UsernamePasswordAuthenticationToken) { UsernamePasswordAuthenticationToken authClass = (UsernamePasswordAuthenticationToken) inAuthentication; Object details = authClass.getDetails(); if (details instanceof WebAuthenticationDetails) { WebAuthenticationDetails webAuthentication = (WebAuthenticationDetails) details; auditRecord.put("remote_ip", webAuthentication.getRemoteAddress()); auditRecord.put("session", webAuthentication.getSessionId()); } } boolean isSuccess = false; try { Authentication authentication = inAuthentication; AuthenticationException authException = null; for (AuthMethod authMethod : AuthMethod.values()) { try { authentication = doAuth(authentication, authMethod); if (authentication != null && authentication.isAuthenticated()) { logger.info("Authenticated using method=" + authMethod.name() + ", user=" + authentication.getName()); auditRecord.put("result", "allowed"); isSuccess = true; auditRecord.put("authType", authMethod.name()); return authentication; } } catch (AuthenticationException ex) { if (authException == null) { authException = ex; } } catch (Exception e) { logger.error(e, e.getCause()); } } auditRecord.put("result", "denied"); logger.warn("Authentication failed for user=" + inAuthentication.getName() + ", userDetail=" + inAuthentication.toString()); if (authException != null) { auditRecord.put("reason", authException.getMessage()); throw authException; } return authentication; } finally { try { String jsonStr = JSONUtil.toJson(auditRecord); auditLogger.log(isSuccess ? Level.INFO : Level.WARN, jsonStr); } catch (Exception e) { logger.error("Unable to add audit log entry", e); } } }  @Override Authentication authenticate(Authentication inAuthentication);  }