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 @@ -63,7 +63,6 @@ public class WISEAuthenticationProcessingFilter extends UsernamePasswordAuthenti
public static final String TEACHER_DEFAULT_TARGET_PATH = "/teacher";
public static final String ADMIN_DEFAULT_TARGET_PATH = "/admin";
public static final String RESEARCHER_DEFAULT_TARGET_PATH = "/teacher";
public static final String LOGIN_DISABLED_MESSGE_PAGE = "/pages/maintenance.html";

private static final Log LOGGER = LogFactory.getLog(WISEAuthenticationProcessingFilter.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.wise.portal.presentation.web.filters;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.core.Authentication;
Expand All @@ -36,6 +38,7 @@
import org.wise.portal.domain.authentication.impl.StudentUserDetails;
import org.wise.portal.domain.authentication.impl.TeacherUserDetails;
import org.wise.portal.domain.portal.Portal;
import org.wise.portal.presentation.web.controllers.ControllerUtil;
import org.wise.portal.service.authentication.AuthorityNotFoundException;
import org.wise.portal.service.authentication.UserDetailsService;
import org.wise.portal.service.portal.PortalService;
Expand Down Expand Up @@ -100,7 +103,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
}
}

// if user is not admin and login is disallowed, log out user and redirect them to the "we are undergoing maintenance" page
// if user is not admin and login is disallowed, log out user and tell the sign-in page
try {
Portal portal = portalService.getById(Integer.valueOf(1));
if (!userIsAdmin && !portal.isLoginAllowed()) {
Expand All @@ -109,14 +112,15 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
new SecurityContextLogoutHandler().logout(request, response, auth);
}
SecurityContextHolder.getContext().setAuthentication(null);
String contextPath = request.getContextPath();
response.sendRedirect(
contextPath + WISEAuthenticationProcessingFilter.LOGIN_DISABLED_MESSGE_PAGE);
JSONObject responseJSON = ControllerUtil.createErrorResponse();
responseJSON.put("isLoginDisabled", true);
response.setContentType("application/json");
response.getWriter().write(responseJSON.toString());
return;
}
} catch (ObjectNotFoundException e) {
// do nothing
} catch (IOException ioe) {
} catch (IOException | JSONException e) {
// do nothing
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.wise.portal.presentation.web.filters;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

import org.easymock.EasyMock;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.util.ReflectionTestUtils;
import org.wise.portal.domain.authentication.MutableUserDetails;
import org.wise.portal.domain.authentication.impl.StudentUserDetails;
import org.wise.portal.domain.portal.Portal;
import org.wise.portal.domain.portal.impl.PortalImpl;
import org.wise.portal.service.authentication.UserDetailsService;
import org.wise.portal.service.portal.PortalService;

class WISEAuthenticationSuccessHandlerTest {

@AfterEach
void tearDown() {
SecurityContextHolder.clearContext();
}

@Test
void signingInToAClosedSite_shouldAnswerLoginDisabled() throws Exception {
Portal portal = new PortalImpl();
portal.setSettings("{isLoginAllowed:false}");
PortalService portalService = EasyMock.createMock(PortalService.class);
EasyMock.expect(portalService.getById(1)).andReturn(portal);
UserDetailsService userDetailsService = EasyMock.createMock(UserDetailsService.class);
EasyMock.replay(portalService, userDetailsService);
WISEAuthenticationSuccessHandler handler = new WISEAuthenticationSuccessHandler();
ReflectionTestUtils.setField(handler, "portalService", portalService);
ReflectionTestUtils.setField(handler, "userDetailsService", userDetailsService);
ReflectionTestUtils.setField(handler, "appProperties",
new MockEnvironment().withProperty("wise.hostname", "http://localhost"));
MutableUserDetails student = new StudentUserDetails();
student.setUsername("student");
student.setLanguage("en");
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(student, "", List.of());
SecurityContextHolder.getContext().setAuthentication(authentication);
MockHttpServletRequest request = new MockHttpServletRequest("POST",
"/api/j_acegi_security_check");
MockHttpServletResponse response = new MockHttpServletResponse();

handler.onAuthenticationSuccess(request, response, authentication);

assertNull(response.getRedirectedUrl());
JSONObject body = new JSONObject(response.getContentAsString());
assertEquals("error", body.getString("status"));
assertTrue(body.getBoolean("isLoginDisabled"));
assertNull(SecurityContextHolder.getContext().getAuthentication());
EasyMock.verify(portalService, userDetailsService);
}
}
Loading