VistaAccessVerifyAuthenticationFilter extends AbstractVistaAuthenticationProcessingFilter { public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { if (postOnly && !request.getMethod().equals("POST")) { throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); } String vistaId = obtainVistaId(request); String division = obtainDivision(request); String accessCode = obtainAccessCode(request); String verifyCode = obtainVerifyCode(request); String newVerifyCode = obtainNewVerifyCode(request); String confirmVerifyCode = obtainConfirmVerifyCode(request); String remoteAddress = obtainRemoteAddress(request); String remoteHostName = obtainRemoteHostName(request); if (accessCode == null) { accessCode = ""; } if (verifyCode == null) { verifyCode = ""; } VistaAuthenticationToken authRequest = createToken(vistaId, division, accessCode, verifyCode, newVerifyCode, confirmVerifyCode, remoteAddress, remoteHostName); setDetails(request, authRequest); logger.debug("Attempting authentication with token: " + authRequest); return this.getAuthenticationManager().authenticate(authRequest); } VistaAccessVerifyAuthenticationFilter(); Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response); void setAccessCodeParameter(String accessCodeParameter); void setVerifyCodeParameter(String verifyCodeParameter); final String getAccessCodeParameter(); final String getVerifyCodeParameter(); static final String ACCESS_CODE_KEY; static final String VERIFY_CODE_KEY; static final String NEW_VERIFY_CODE_KEY; static final String CONFIRM_VERIFY_CODE_KEY; }
@Test public void testAttemptAuthentication() throws Exception { VistaAccessVerifyAuthenticationFilter f = new VistaAccessVerifyAuthenticationFilter(); f.setFilterProcessesUrl("/welcome.jsp"); f.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler("/authenticationFailed.jsp")); f.setAuthenticationManager(mockAuthenticationManager); f.afterPropertiesSet(); request.addParameter(AbstractVistaAuthenticationProcessingFilter.VISTA_ID_KEY, "9F2A"); request.addParameter(AbstractVistaAuthenticationProcessingFilter.DIVISION_KEY, "500"); request.addParameter(VistaAccessVerifyAuthenticationFilter.ACCESS_CODE_KEY, "10VEHU"); request.addParameter(VistaAccessVerifyAuthenticationFilter.VERIFY_CODE_KEY, "VEHU10"); request.setRemoteAddr("10.0.1.34"); request.setRequestURI(f.getFilterProcessesUrl()); request.setMethod("POST"); VistaAuthenticationToken authRequest = new VistaAuthenticationToken("9F2A", "500", "10VEHU", "VEHU10", "10.0.1.34", "www.example.org"); when(mockAuthenticationManager.authenticate(AuthenticationTokenMatchers.eq(authRequest))).thenReturn(new VistaAuthenticationToken(new VistaUser(new RpcHost("localhost"), "9F2A", "500", "500", "12345", "500:10VEHU;VEHU10", "Vehu,Ten", true, true, true, true, new ArrayList<GrantedAuthority>()), "500:10VEHU;VEHU10", "10.0.1.34", null, new ArrayList<GrantedAuthority>())); f.doFilter(request, response, filterChain); assertNull(filterChain.getRequest()); assertNull(filterChain.getResponse()); ArgumentCaptor<VistaAuthenticationToken> arg = ArgumentCaptor.forClass(VistaAuthenticationToken.class); verify(mockAuthenticationManager).authenticate(arg.capture()); assertThat(arg.getValue().getVistaId(), equalTo("9F2A")); assertThat(arg.getValue().getDivision(), equalTo("500")); assertThat(arg.getValue().getAccessCode(), equalTo("10VEHU")); assertThat(arg.getValue().getVerifyCode(), equalTo("VEHU10")); assertThat(arg.getValue().getNewVerifyCode(), nullValue()); assertThat(arg.getValue().getConfirmVerifyCode(), nullValue()); assertThat(arg.getValue().getRemoteAddress(), equalTo("10.0.1.34")); }