VistaAppHandleAuthenticationFilter extends AbstractVistaAuthenticationProcessingFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { if (postOnly && !request.getMethod().equals("POST")) { throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); } String vistaId = obtainVistaId(request); String division = obtainDivision(request); String appHandle = obtainAppHandle(request); String remoteAddress = obtainRemoteAddress(request); String remoteHostName = obtainRemoteHostName(request); if (division == null) { division = ""; } if (appHandle == null) { appHandle = ""; } VistaAuthenticationToken authRequest = createToken(vistaId, division, appHandle, remoteAddress, remoteHostName); setDetails(request, authRequest); logger.debug("Attempting authentication with token: " + authRequest); return this.getAuthenticationManager().authenticate(authRequest); } VistaAppHandleAuthenticationFilter(); @Override Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response); void setAppHandleParameter(String appHandleParameter); final String getAppHandleParameter(); static final String APP_HANDLE_KEY; }
@Test public void testAttemptAuthentication() throws Exception { VistaAppHandleAuthenticationFilter f = new VistaAppHandleAuthenticationFilter(); f.setFilterProcessesUrl("/welcome.jsp"); f.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler("/authenticationFailed.jsp")); f.setAuthenticationManager(mockAuthenticationManager); f.afterPropertiesSet(); request.addParameter(VistaAppHandleAuthenticationFilter.VISTA_ID_KEY, "9F2A"); request.addParameter(VistaAppHandleAuthenticationFilter.DIVISION_KEY, "500"); request.addParameter(VistaAppHandleAuthenticationFilter.APP_HANDLE_KEY, "1AB2C3D4E5F6"); request.setRemoteAddr("10.0.1.34"); request.setRemoteHost("www.example.org"); request.setRequestURI(f.getFilterProcessesUrl()); request.setMethod("POST"); VistaAuthenticationToken authRequest = new VistaAuthenticationToken("9F2A", "500", "1AB2C3D4E5F6", "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", "www.example.org", 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().getAppHandle(), equalTo("1AB2C3D4E5F6")); assertThat(arg.getValue().getRemoteAddress(), equalTo("10.0.1.34")); assertThat(arg.getValue().getRemoteHostName(), equalTo("www.example.org")); assertThat(arg.getValue().getAccessCode(), nullValue()); assertThat(arg.getValue().getVerifyCode(), nullValue()); assertThat(arg.getValue().getNewVerifyCode(), nullValue()); assertThat(arg.getValue().getConfirmVerifyCode(), nullValue()); }