Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -70,6 +72,9 @@ public class JWTTokenRestAuthenticationServiceImpl implements RestAuthentication
@Autowired
ClarinTokenService clarinTokenService;

@Autowired
private ConfigurationService configurationService;

@Lazy
@Autowired
private DSpaceCsrfAuthenticationStrategy dspaceCsrfAuthenticationStrategy;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading