@Before public void setUp() throws Exception { filter = new OpenIDAuthenticationFilter(); filter.setConsumer(new MockOpenIDConsumer(REDIRECT_URL)); SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); filter.setAuthenticationSuccessHandler(new SavedRequestAwareAuthenticationSuccessHandler()); successHandler.setDefaultTargetUrl(DEFAULT_TARGET_URL); filter.setAuthenticationManager(new AuthenticationManager() { public Authentication authenticate(Authentication a) { return a; } }); filter.afterPropertiesSet(); }
String claimedIdentity = obtainUsername(request); String returnToUrl = buildReturnToUrl(request); String realm = lookupRealm(returnToUrl); String openIdUrl = consumer.beginConsumption(request, claimedIdentity, returnToUrl, realm); Authentication authentication = this.getAuthenticationManager().authenticate( token);
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); if (consumer == null) { try { consumer = new OpenID4JavaConsumer(); } catch (ConsumerException e) { throw new IllegalArgumentException("Failed to initialize OpenID", e); } } if (returnToUrlParameters.isEmpty() && getRememberMeServices() instanceof AbstractRememberMeServices) { returnToUrlParameters = new HashSet<>(); returnToUrlParameters .add(((AbstractRememberMeServices) getRememberMeServices()) .getParameter()); } }
/** * Tests that the filter encodes any query parameters on the return_to URL. */ @Test public void encodesUrlParameters() throws Exception { // Arbitrary parameter name and value that will both need to be encoded: String paramName = "foo&bar"; String paramValue = "http://example.com/path?a=b&c=d"; MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_PATH); req.addParameter(paramName, paramValue); filter.setReturnToUrlParameters(Collections.singleton(paramName)); URI returnTo = new URI(filter.buildReturnToUrl(req)); String query = returnTo.getRawQuery(); assertThat(count(query, '=')).isEqualTo(1); assertThat(count(query, '&')).isZero(); }
@Override public void configure(H http) throws Exception { getAuthenticationFilter().setConsumer(getConsumer()); super.configure(http); }
/** * Creates a new instance */ public OpenIDLoginConfigurer() { super(new OpenIDAuthenticationFilter(), "/login/openid"); }
@Test public void requestWhenOpenIDAndRememberMeConfiguredThenRememberMePassedToIdp() throws Exception { this.spring.configLocations(this.xml("WithRememberMe")).autowire(); OpenIDAuthenticationFilter openIDFilter = getFilter(OpenIDAuthenticationFilter.class); String openIdEndpointUrl = "http://testopenid.com?openid.return_to="; Set<String> returnToUrlParameters = new HashSet<>(); returnToUrlParameters.add(AbstractRememberMeServices.DEFAULT_PARAMETER); openIDFilter.setReturnToUrlParameters(returnToUrlParameters); OpenIDConsumer consumer = mock(OpenIDConsumer.class); when(consumer.beginConsumption(any(HttpServletRequest.class), anyString(), anyString(), anyString())) .then(invocation -> openIdEndpointUrl + invocation.getArgument(2)); openIDFilter.setConsumer(consumer); String expectedReturnTo = new StringBuilder("http://localhost/login/openid").append("?") .append(AbstractRememberMeServices.DEFAULT_PARAMETER) .append("=").append("on").toString(); this.mvc.perform(get("/")) .andExpect(status().isFound()) .andExpect(redirectedUrl("http://localhost/login")); this.mvc.perform(get("/login")) .andExpect(status().isOk()) .andExpect(content().string(containsString(AbstractRememberMeServices.DEFAULT_PARAMETER))); this.mvc.perform(get("/login/openid") .param(OpenIDAuthenticationFilter.DEFAULT_CLAIMED_IDENTITY_FIELD, "http://hey.openid.com/") .param(AbstractRememberMeServices.DEFAULT_PARAMETER, "on")) .andExpect(status().isFound()) .andExpect(redirectedUrl(openIdEndpointUrl + expectedReturnTo)); }
@Test public void testFilterOperation() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.setServletPath(REQUEST_PATH); req.setRequestURI(REQUEST_PATH); req.setServerPort(8080); MockHttpServletResponse response = new MockHttpServletResponse(); req.setParameter("openid_identifier", " " + CLAIMED_IDENTITY_URL); req.setRemoteHost("www.example.com"); filter.setConsumer(new MockOpenIDConsumer() { public String beginConsumption(HttpServletRequest req, String claimedIdentity, String returnToUrl, String realm) throws OpenIDConsumerException { assertThat(claimedIdentity).isEqualTo(CLAIMED_IDENTITY_URL); assertThat(returnToUrl).isEqualTo(DEFAULT_TARGET_URL); assertThat(realm).isEqualTo("http://localhost:8080/"); return REDIRECT_URL; } }); FilterChain fc = mock(FilterChain.class); filter.doFilter(req, response, fc); assertThat(response.getRedirectedUrl()).isEqualTo(REDIRECT_URL); // Filter chain shouldn't proceed verify(fc, never()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); }
auth = (OpenIDAuthenticationToken) super.attemptAuthentication(req, rsp);
/** * {@inheritDoc} */ @Override protected String lookupRealm(String returnToUrl) { String mapping = super.lookupRealm(returnToUrl); if (mapping == null) { try { URL url = new URL(returnToUrl); int port = url.getPort(); StringBuilder realmBuffer = new StringBuilder(returnToUrl.length()) .append(url.getProtocol()) .append("://") .append(url.getHost()); if (port != -1) { realmBuffer.append(":").append(port); } realmBuffer.append("/"); mapping = realmBuffer.toString(); } catch (MalformedURLException e) { log.warn("returnToUrl was not a valid URL: [" + returnToUrl + "]", e); } } return mapping; } }
@Override public void configure(H http) throws Exception { getAuthenticationFilter().setConsumer(getConsumer()); super.configure(http); }
/** * Creates a new instance */ public OpenIDLoginConfigurer() { super(new OpenIDAuthenticationFilter(), "/login/openid"); }
Authentication oidAuth = super.attemptAuthentication(request, response);
String claimedIdentity = obtainUsername(request); String returnToUrl = buildReturnToUrl(request); String realm = lookupRealm(returnToUrl); String openIdUrl = consumer.beginConsumption(request, claimedIdentity, returnToUrl, realm); Authentication authentication = this.getAuthenticationManager().authenticate( token);
@Override public void configure(H http) throws Exception { getAuthenticationFilter().setConsumer(getConsumer()); super.configure(http); }
/** * Creates a new instance */ public OpenIDLoginConfigurer() { super(new OpenIDAuthenticationFilter(), "/login/openid"); }
Authentication oidAuth = super.attemptAuthentication(request, response);
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); if (consumer == null) { try { consumer = new OpenID4JavaConsumer(); } catch (ConsumerException e) { throw new IllegalArgumentException("Failed to initialize OpenID", e); } } if (returnToUrlParameters.isEmpty() && getRememberMeServices() instanceof AbstractRememberMeServices) { returnToUrlParameters = new HashSet<>(); returnToUrlParameters .add(((AbstractRememberMeServices) getRememberMeServices()) .getParameter()); } }
@Override public void configure(H http) throws Exception { getAuthenticationFilter().setConsumer(getConsumer()); super.configure(http); }
/** * Creates a new instance */ public OpenIDLoginConfigurer() { super(new OpenIDAuthenticationFilter(),"/login/openid"); }