@Test public void shouldAuditLogUnsuccessfulAuthorization() throws SSOException { AuditRecord auditRecord = mock(AuditRecord.class); HttpServletRequest request = mock(HttpServletRequest.class); SSOToken subjectToken = mock(SSOToken.class); SSOTokenID subjectTokenId = mock(SSOTokenID.class); Principal principal = mock(Principal.class); given(auditRecord.getHttpServletRequest()).willReturn(request); given(auditRecord.getAuthResult()).willReturn(AuthResult.FAILURE); given(request.getRequestURI()).willReturn("REQUEST_URI"); given(requestUtils.getTokenId(request)).willReturn("TOKEN_ID"); given(ssoTokenFactory.getTokenFromId("TOKEN_ID")).willReturn(subjectToken); given(subjectToken.getTokenID()).willReturn(subjectTokenId); given(subjectTokenId.toString()).willReturn("TOKEN_ID"); given(subjectToken.getPrincipal()).willReturn(principal); given(principal.getName()).willReturn("PRINCIPAL_NAME"); authZAuditLogger.audit(auditRecord); verify(loggerFactory).getLogger("amAuthorization.error"); ArgumentCaptor<LogRecord> logRecordCaptor = ArgumentCaptor.forClass(LogRecord.class); verify(logger).log(logRecordCaptor.capture(), eq(adminToken)); LogRecord logRecord = logRecordCaptor.getValue(); assertEquals(logRecord.getLevel(), Level.INFO); assertTrue(logRecord.getMessage().contains("Failed")); assertEquals(logRecord.getLogFor(), subjectToken); verify(logger).flush(); }
@Override public void audit(AuditRecord auditRecord) { String tokenId = requestUtils.getTokenId(auditRecord.getHttpServletRequest()); String message; Logger logger; switch (auditRecord.getAuthResult()) { case SUCCESS: { message = "Authorization Succeeded. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".access"); break; } default: { message = "Authorization Failed. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".error"); } } SSOToken adminToken = ssoTokenFactory.getAdminToken(); SSOToken subjectToken = ssoTokenFactory.getTokenFromId(tokenId); LogRecord logRecord = new LogRecord(Level.INFO, message, subjectToken); logger.log(logRecord, adminToken); logger.flush(); }
AuthZAuditLogger implements AuditLogger { @Override public void audit(AuditRecord auditRecord) { String tokenId = requestUtils.getTokenId(auditRecord.getHttpServletRequest()); String message; Logger logger; switch (auditRecord.getAuthResult()) { case SUCCESS: { message = "Authorization Succeeded. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".access"); break; } default: { message = "Authorization Failed. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".error"); } } SSOToken adminToken = ssoTokenFactory.getAdminToken(); SSOToken subjectToken = ssoTokenFactory.getTokenFromId(tokenId); LogRecord logRecord = new LogRecord(Level.INFO, message, subjectToken); logger.log(logRecord, adminToken); logger.flush(); } }
AuthZAuditLogger implements AuditLogger { @Override public void audit(AuditRecord auditRecord) { String tokenId = requestUtils.getTokenId(auditRecord.getHttpServletRequest()); String message; Logger logger; switch (auditRecord.getAuthResult()) { case SUCCESS: { message = "Authorization Succeeded. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".access"); break; } default: { message = "Authorization Failed. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".error"); } } SSOToken adminToken = ssoTokenFactory.getAdminToken(); SSOToken subjectToken = ssoTokenFactory.getTokenFromId(tokenId); LogRecord logRecord = new LogRecord(Level.INFO, message, subjectToken); logger.log(logRecord, adminToken); logger.flush(); } @Inject  AuthZAuditLogger(SSOTokenFactory ssoTokenFactory, AuthnRequestUtils requestUtils,
            LoggerFactory loggerFactory); }
AuthZAuditLogger implements AuditLogger { @Override public void audit(AuditRecord auditRecord) { String tokenId = requestUtils.getTokenId(auditRecord.getHttpServletRequest()); String message; Logger logger; switch (auditRecord.getAuthResult()) { case SUCCESS: { message = "Authorization Succeeded. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".access"); break; } default: { message = "Authorization Failed. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".error"); } } SSOToken adminToken = ssoTokenFactory.getAdminToken(); SSOToken subjectToken = ssoTokenFactory.getTokenFromId(tokenId); LogRecord logRecord = new LogRecord(Level.INFO, message, subjectToken); logger.log(logRecord, adminToken); logger.flush(); } @Inject  AuthZAuditLogger(SSOTokenFactory ssoTokenFactory, AuthnRequestUtils requestUtils,
            LoggerFactory loggerFactory); @Override void audit(AuditRecord auditRecord); }
AuthZAuditLogger implements AuditLogger { @Override public void audit(AuditRecord auditRecord) { String tokenId = requestUtils.getTokenId(auditRecord.getHttpServletRequest()); String message; Logger logger; switch (auditRecord.getAuthResult()) { case SUCCESS: { message = "Authorization Succeeded. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".access"); break; } default: { message = "Authorization Failed. " + auditRecord.getHttpServletRequest().getRequestURI(); logger = loggerFactory.getLogger(AUDIT_LOG_NAME + ".error"); } } SSOToken adminToken = ssoTokenFactory.getAdminToken(); SSOToken subjectToken = ssoTokenFactory.getTokenFromId(tokenId); LogRecord logRecord = new LogRecord(Level.INFO, message, subjectToken); logger.log(logRecord, adminToken); logger.flush(); } @Inject  AuthZAuditLogger(SSOTokenFactory ssoTokenFactory, AuthnRequestUtils requestUtils,
            LoggerFactory loggerFactory); @Override void audit(AuditRecord auditRecord);  }