diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java index 7fe1b1747cd8..9c55231d25d5 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java @@ -8,6 +8,7 @@ package org.dspace.app.rest.security.jwt; import java.io.IOException; +import java.net.URI; import java.sql.SQLException; import java.text.ParseException; import java.util.Iterator; @@ -33,6 +34,7 @@ import org.dspace.core.Context; import org.dspace.eperson.EPerson; import org.dspace.eperson.service.EPersonService; +import org.dspace.services.ConfigurationService; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; @@ -70,6 +72,9 @@ public class JWTTokenRestAuthenticationServiceImpl implements RestAuthentication @Autowired ClarinTokenService clarinTokenService; + @Autowired + private ConfigurationService configurationService; + @Lazy @Autowired private DSpaceCsrfAuthenticationStrategy dspaceCsrfAuthenticationStrategy; @@ -180,7 +185,8 @@ public void invalidateAuthenticationData(HttpServletRequest request, HttpServlet public void invalidateAuthenticationCookie(HttpServletRequest request, HttpServletResponse response) { // Re-send the same cookie (as addTokenToResponse()) with no value and a Max-Age of 0 seconds ResponseCookie cookie = ResponseCookie.from(AUTHORIZATION_COOKIE, "") - .maxAge(0).httpOnly(true).secure(true).sameSite("None").build(); + .maxAge(0).httpOnly(true).secure(true).sameSite("None") + .path(getAuthorizationCookiePath()).build(); // Write the cookie to the Set-Cookie header in order to send it response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString()); @@ -265,7 +271,7 @@ private void addTokenToResponse(final HttpServletRequest request, final HttpServ .httpOnly(true) .secure(true) .sameSite("None") - .path("/server/api/authn") + .path(getAuthorizationCookiePath()) .build(); // Write the cookie to the Set-Cookie header in order to send it @@ -312,6 +318,17 @@ private String getShortLivedToken(HttpServletRequest request) { return tokenValue; } + /** + * Path of the temporary authorization cookie: /api/authn under the path of dspace.server.url. + * The login callbacks and the UI use that public URL, so the browser sends the cookie back to /login + * even when the REST API runs under another path than /server (e.g. /repository/server). + * @return cookie path + */ + private String getAuthorizationCookiePath() { + String serverPath = URI.create(configurationService.getProperty("dspace.server.url")).getRawPath(); + return StringUtils.removeEnd(serverPath, "/") + "/api/authn"; + } + /** * Get the value of the (temporary) authorization cookie, if exists. * @param request current request diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java index 576864d4f137..7d8a1844b843 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java @@ -357,6 +357,56 @@ public void testStatusNotAuthenticated() throws Exception { .andExpect(jsonPath("$._embedded").doesNotExist()); } + /** + * The temporary auth cookie and its removal carry /api/authn under the path of dspace.server.url, also when + * the servlet context path differs from it (a proxy in front of the REST API). Otherwise a browser never + * sends the cookie back to /login. + */ + @Test + public void testShibAuthCookiePathFollowsServerUrl() throws Exception { + setAuthenticationMethodSequence(SHIB_ONLY); + + String serverUrl = configurationService.getProperty("dspace.server.url"); + try { + assertShibAuthCookiePath("https://dspace.example.com/server", "", "/server/api/authn"); + assertShibAuthCookiePath("https://dspace.example.com/server/", "", "/server/api/authn"); + assertShibAuthCookiePath("https://dspace.example.com/repository/server", "/server", + "/repository/server/api/authn"); + assertShibAuthCookiePath("https://dspace.example.com/caf%C3%A9/server", "/server", + "/caf%C3%A9/server/api/authn"); + } finally { + configurationService.setProperty("dspace.server.url", serverUrl); + } + } + + private void assertShibAuthCookiePath(String serverUrl, String contextPath, String cookiePath) throws Exception { + configurationService.setProperty("dspace.server.url", serverUrl); + String uiURL = configurationService.getProperty("dspace.ui.url"); + + Cookie authCookie = getClient().perform(get(contextPath + "/api/authn/shibboleth") + .contextPath(contextPath) + .header("Referer", "https://myshib.example.com") + .param("redirectUrl", uiURL) + .requestAttr("SHIB-MAIL", eperson.getEmail()) + .requestAttr("SHIB-SCOPED-AFFILIATION", "faculty;staff")) + .andExpect(status().is3xxRedirection()) + .andExpect(cookie().exists(AUTHORIZATION_COOKIE)) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, cookiePath)) + .andReturn().getResponse().getCookie(AUTHORIZATION_COOKIE); + + // /login destroys the cookie with the same path, otherwise the browser would keep the old one. + getClient().perform(post(contextPath + "/api/authn/login") + .contextPath(contextPath) + .header("Origin", uiURL) + .secure(true) + .cookie(authCookie)) + .andExpect(status().isOk()) + .andExpect(cookie().value(AUTHORIZATION_COOKIE, "")) + .andExpect(cookie().maxAge(AUTHORIZATION_COOKIE, 0)) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, cookiePath)) + .andExpect(header().exists(AUTHORIZATION_HEADER)); + } + @Test public void testStatusShibAuthenticatedWithCookie() throws Exception { //Enable Shibboleth login only @@ -387,6 +437,7 @@ public void testStatusShibAuthenticatedWithCookie() throws Exception { .andExpect(cookie().doesNotExist("DSPACE-XSRF-COOKIE")) .andExpect(header().doesNotExist("DSPACE-XSRF-TOKEN")) .andExpect(cookie().exists(AUTHORIZATION_COOKIE)) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, "/api/authn")) .andReturn().getResponse().getCookie(AUTHORIZATION_COOKIE); // Verify the temporary cookie now exists & obtain its token for use below @@ -433,6 +484,7 @@ public void testStatusShibAuthenticatedWithCookie() throws Exception { .andExpect(status().isOk()) // Verify the Auth cookie has been destroyed .andExpect(cookie().value(AUTHORIZATION_COOKIE, "")) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, "/api/authn")) // Verify Authorization header is returned .andExpect(header().exists(AUTHORIZATION_HEADER)) // Verify that the CSRF token has been changed