SAMLProcessingFilter extends AbstractAuthenticationProcessingFilter { public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { try { log.debug("Attempting SAML2 authentication using profile {}", getProfileName()); SAMLMessageContext context = contextProvider.getLocalEntity(request, response); processor.retrieveMessage(context); context.setCommunicationProfileId(getProfileName()); context.setLocalEntityEndpoint(SAMLUtil.getEndpoint(context.getLocalEntityRoleMetadata().getEndpoints(), context.getInboundSAMLBinding(), context.getInboundMessageTransport(), uriComparator)); SAMLAuthenticationToken token = new SAMLAuthenticationToken(context); return getAuthenticationManager().authenticate(token); } catch (SAMLException e) { log.debug("Incoming SAML message is invalid", e); throw new AuthenticationServiceException("Incoming SAML message is invalid", e); } catch (MetadataProviderException e) { log.debug("Error determining metadata contracts", e); throw new AuthenticationServiceException("Error determining metadata contracts", e); } catch (MessageDecodingException e) { log.debug("Error decoding incoming SAML message", e); throw new AuthenticationServiceException("Error decoding incoming SAML message", e); } catch (org.opensaml.xml.security.SecurityException e) { log.debug("Incoming SAML message is invalid", e); throw new AuthenticationServiceException("Incoming SAML message is invalid", e); } } SAMLProcessingFilter(); protected  SAMLProcessingFilter(String defaultFilterProcessesUrl); Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response); @Deprecated void setDefaultTargetUrl(String url); @Autowired void setSAMLProcessor(SAMLProcessor processor); @Autowired void setContextProvider(SAMLContextProvider contextProvider); @Autowired(required = false) void setUriComparator(URIComparator uriComparator); @Override void afterPropertiesSet(); @Override void setFilterProcessesUrl(String filterProcessesUrl); String getFilterProcessesUrl(); static final String FILTER_URL; }
@Test(expected = AuthenticationServiceException.class) public void testErrorDuringProcessing() throws Exception { SAMLTestHelper.setLocalContextParameters(request, "/saml", null); expect(processor.retrieveMessage((SAMLMessageContext) notNull())).andThrow(new SAMLException("Processing error")); replayMock(); processingFiler.attemptAuthentication(request, null); verifyMock(); }
@Test(expected = AuthenticationServiceException.class) public void testInvalidBinding() throws Exception { AuthenticationManager manager = createMock(AuthenticationManager.class); processingFiler.setAuthenticationManager(manager); SAMLTestHelper.setLocalContextParameters(request, "/saml", null); final Capture<SAMLMessageContext> context = new Capture<SAMLMessageContext>(); expect(request.getRequestURL()).andReturn(new StringBuffer("http: expect(request.getQueryString()).andReturn(null); expect(processor.retrieveMessage(capture(context))).andAnswer(new IAnswer<SAMLMessageContext>() { public SAMLMessageContext answer() throws Throwable { context.getValue().setInboundSAMLBinding(SAMLConstants.SAML2_REDIRECT_BINDING_URI); return context.getValue(); } }); replay(manager); replayMock(); processingFiler.attemptAuthentication(request, null); verifyMock(); verify(manager); }
@Test(expected = AuthenticationServiceException.class) public void testInvalidEndpoint() throws Exception { AuthenticationManager manager = createMock(AuthenticationManager.class); processingFiler.setAuthenticationManager(manager); SAMLTestHelper.setLocalContextParameters(request, "/saml", null); final Capture<SAMLMessageContext> context = new Capture<SAMLMessageContext>(); expect(request.getRequestURL()).andReturn(new StringBuffer("http: expect(request.getQueryString()).andReturn(null); expect(processor.retrieveMessage(capture(context))).andAnswer(new IAnswer<SAMLMessageContext>() { public SAMLMessageContext answer() throws Throwable { context.getValue().setInboundSAMLBinding(SAMLConstants.SAML2_REDIRECT_BINDING_URI); return context.getValue(); } }); replay(manager); replayMock(); processingFiler.attemptAuthentication(request, null); verifyMock(); verify(manager); }
@Test public void testCorrectPass() throws Exception { Authentication token = new UsernamePasswordAuthenticationToken("user", "pass"); AuthenticationManager manager = createMock(AuthenticationManager.class); processingFiler.setAuthenticationManager(manager); SAMLTestHelper.setLocalContextParameters(request, "/saml", null); final Capture<SAMLMessageContext> context = new Capture<SAMLMessageContext>(); expect(request.getRequestURL()).andReturn(new StringBuffer("http: expect(request.getQueryString()).andReturn(null); expect(processor.retrieveMessage(capture(context))).andAnswer(new IAnswer<SAMLMessageContext>() { public SAMLMessageContext answer() throws Throwable { context.getValue().setInboundSAMLBinding(org.opensaml.common.xml.SAMLConstants.SAML2_POST_BINDING_URI); return context.getValue(); } }); expect(manager.authenticate((Authentication) notNull())).andReturn(token); replay(manager); replayMock(); Authentication authentication = processingFiler.attemptAuthentication(request, null); assertEquals(token, authentication); verifyMock(); verify(manager); }