[CST-5669] Added redirect on ORCID login error

This commit is contained in:
Luca Giamminonni
2022-06-06 18:07:35 +02:00
parent 00031a0dd6
commit ab0fbcc9fe
2 changed files with 105 additions and 98 deletions

View File

@@ -70,6 +70,16 @@ public class OrcidLoginFilter extends StatelessLoginFilter {
redirectAfterSuccess(req, res);
}
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException, ServletException {
String baseRediredirectUrl = configurationService.getProperty("dspace.ui.url");
String redirectUrl = baseRediredirectUrl + "/error?status=401&code=orcid.generic-error";
response.sendRedirect(redirectUrl);
}
/**
* After successful login, redirect to the DSpace URL specified by this Orcid
* request (in the "redirectUrl" request parameter). If that 'redirectUrl' is

View File

@@ -10,7 +10,6 @@ package org.dspace.app.rest;
import static java.util.Arrays.asList;
import static org.dspace.app.matcher.MetadataValueMatcher.with;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
@@ -23,7 +22,6 @@ import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -133,7 +131,8 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
.param("code", CODE))
.andExpect(status().isUnauthorized());
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"));
}
@Test
@@ -176,7 +175,8 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
.param("code", CODE))
.andExpect(status().isUnauthorized());
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"));
verify(orcidClientMock).getAccessToken(CODE);
verify(orcidClientMock).getPerson(ACCESS_TOKEN, ORCID);
@@ -191,16 +191,13 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
when(orcidClientMock.getAccessToken(CODE)).thenReturn(buildOrcidTokenResponse(ORCID, ACCESS_TOKEN));
when(orcidClientMock.getPerson(ACCESS_TOKEN, ORCID)).thenReturn(buildPerson("Test", "User"));
MvcResult mvcResult = getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
.param("code", CODE))
.andExpect(status().isUnauthorized())
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"))
.andExpect(cookie().doesNotExist("Authorization-cookie"))
.andExpect(header().exists("WWW-Authenticate"))
.andReturn();
String authenticateHeader = mvcResult.getResponse().getHeader("WWW-Authenticate");
assertThat(authenticateHeader, containsString("orcid realm=\"DSpace REST API\""));
verify(orcidClientMock).getAccessToken(CODE);
verify(orcidClientMock).getPerson(ACCESS_TOKEN, ORCID);
verifyNoMoreInteractions(orcidClientMock);
@@ -211,9 +208,9 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
public void testWithoutAuthorizationCode() throws Exception {
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid"))
.andExpect(status().isUnauthorized())
.andExpect(cookie().doesNotExist("Authorization-cookie"))
.andExpect(header().exists("WWW-Authenticate"));
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"))
.andExpect(cookie().doesNotExist("Authorization-cookie"));
verifyNoInteractions(orcidClientMock);
@@ -269,9 +266,9 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
.param("code", CODE))
.andExpect(status().isUnauthorized())
.andExpect(cookie().doesNotExist("Authorization-cookie"))
.andExpect(header().exists("WWW-Authenticate"));
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"))
.andExpect(cookie().doesNotExist("Authorization-cookie"));
verify(orcidClientMock).getAccessToken(CODE);
verifyNoMoreInteractions(orcidClientMock);
@@ -329,9 +326,9 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
.param("code", CODE))
.andExpect(status().isUnauthorized())
.andExpect(cookie().doesNotExist("Authorization-cookie"))
.andExpect(header().exists("WWW-Authenticate"));
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"))
.andExpect(cookie().doesNotExist("Authorization-cookie"));
verify(orcidClientMock).getAccessToken(CODE);
verify(orcidClientMock).getPerson(ACCESS_TOKEN, ORCID);
@@ -356,9 +353,9 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
.param("code", CODE))
.andExpect(status().isUnauthorized())
.andExpect(cookie().doesNotExist("Authorization-cookie"))
.andExpect(header().exists("WWW-Authenticate"));
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"))
.andExpect(cookie().doesNotExist("Authorization-cookie"));
verify(orcidClientMock).getAccessToken(CODE);
verifyNoMoreInteractions(orcidClientMock);
@@ -383,9 +380,9 @@ public class OrcidLoginFilterIT extends AbstractControllerIntegrationTest {
getClient().perform(get("/api/" + AuthnRest.CATEGORY + "/orcid")
.param("code", CODE))
.andExpect(status().isUnauthorized())
.andExpect(cookie().doesNotExist("Authorization-cookie"))
.andExpect(header().exists("WWW-Authenticate"));
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("http://localhost:4000/error?status=401&code=orcid.generic-error"))
.andExpect(cookie().doesNotExist("Authorization-cookie"));
verify(orcidClientMock).getAccessToken(CODE);
verify(orcidClientMock).getPerson(ACCESS_TOKEN, ORCID);