mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-10 11:33:11 +00:00
Fix shibboleth redirect to work with all allowed origins
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
package org.dspace.app.rest;
|
package org.dspace.app.rest;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@@ -62,8 +63,14 @@ public class ShibbolethRestController implements InitializingBean {
|
|||||||
// Validate that the redirectURL matches either the server or UI hostname. It *cannot* be an arbitrary URL.
|
// Validate that the redirectURL matches either the server or UI hostname. It *cannot* be an arbitrary URL.
|
||||||
String redirectHostName = Utils.getHostName(redirectUrl);
|
String redirectHostName = Utils.getHostName(redirectUrl);
|
||||||
String serverHostName = Utils.getHostName(configurationService.getProperty("dspace.server.url"));
|
String serverHostName = Utils.getHostName(configurationService.getProperty("dspace.server.url"));
|
||||||
String clientHostName = Utils.getHostName(configurationService.getProperty("dspace.ui.url"));
|
ArrayList<String> allowedHostNames = new ArrayList<String>();
|
||||||
if (StringUtils.equalsAnyIgnoreCase(redirectHostName, serverHostName, clientHostName)) {
|
allowedHostNames.add(serverHostName);
|
||||||
|
String[] allowedUrls = configurationService.getArrayProperty("rest.cors.allowed-origins");
|
||||||
|
for (String url : allowedUrls) {
|
||||||
|
allowedHostNames.add(Utils.getHostName(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.equalsAnyIgnoreCase(redirectHostName, allowedHostNames.toArray(new String[0]))) {
|
||||||
log.debug("Shibboleth redirecting to " + redirectUrl);
|
log.debug("Shibboleth redirecting to " + redirectUrl);
|
||||||
response.sendRedirect(redirectUrl);
|
response.sendRedirect(redirectUrl);
|
||||||
} else {
|
} else {
|
||||||
|
@@ -12,7 +12,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration test that cover ShibbolethRestController
|
* Integration test that cover ShibbolethRestController
|
||||||
@@ -21,6 +24,17 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class ShibbolethRestControllerIT extends AbstractControllerIntegrationTest {
|
public class ShibbolethRestControllerIT extends AbstractControllerIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ConfigurationService configurationService;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
configurationService.setProperty("rest.cors.allowed-origins",
|
||||||
|
"${dspace.ui.url}, http://anotherdspacehost:4000");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRedirectToDefaultDspaceUrl() throws Exception {
|
public void testRedirectToDefaultDspaceUrl() throws Exception {
|
||||||
String token = getAuthToken(eperson.getEmail(), password);
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
@@ -32,6 +46,7 @@ public class ShibbolethRestControllerIT extends AbstractControllerIntegrationTes
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRedirectToGivenTrustedUrl() throws Exception {
|
public void testRedirectToGivenTrustedUrl() throws Exception {
|
||||||
|
|
||||||
String token = getAuthToken(eperson.getEmail(), password);
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
|
||||||
getClient(token).perform(get("/api/authn/shibboleth")
|
getClient(token).perform(get("/api/authn/shibboleth")
|
||||||
@@ -40,6 +55,16 @@ public class ShibbolethRestControllerIT extends AbstractControllerIntegrationTes
|
|||||||
.andExpect(redirectedUrl("http://localhost:8080/server/api/authn/status"));
|
.andExpect(redirectedUrl("http://localhost:8080/server/api/authn/status"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedirectToAnotherGivenTrustedUrl() throws Exception {
|
||||||
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
|
||||||
|
getClient(token).perform(get("/api/authn/shibboleth")
|
||||||
|
.param("redirectUrl", "http://anotherdspacehost:4000/home"))
|
||||||
|
.andExpect(status().is3xxRedirection())
|
||||||
|
.andExpect(redirectedUrl("http://anotherdspacehost:4000/home"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRedirectToGivenUntrustedUrl() throws Exception {
|
public void testRedirectToGivenUntrustedUrl() throws Exception {
|
||||||
String token = getAuthToken(eperson.getEmail(), password);
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
Reference in New Issue
Block a user