Search in sources :

Example 91 with IPerson

use of org.apereo.portal.security.IPerson in project uPortal by Jasig.

the class PersonService method getPerson.

/**
 * Obtain the fully-constructed {@link IPerson} associated witth the specified username.
 */
public IPerson getPerson(String username) {
    final IPerson rslt = new PersonImpl();
    rslt.setAttribute(IPerson.USERNAME, username);
    rslt.setID(userIdentityStore.getPortalUserId(username));
    rslt.setAttributes(personAttributeDao.getPerson(username).getAttributes());
    return rslt;
}
Also used : IPerson(org.apereo.portal.security.IPerson) PersonImpl(org.apereo.portal.security.provider.PersonImpl)

Example 92 with IPerson

use of org.apereo.portal.security.IPerson in project uPortal by Jasig.

the class LogoutController method doLogout.

/**
 * Process the incoming request and response.
 *
 * @param request HttpServletRequest object
 * @param response HttpServletResponse object
 */
@RequestMapping
public void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String redirect = this.selectRedirectionUrl(request);
    final HttpSession session = request.getSession(false);
    if (session != null) {
        // Record that an authenticated user is requesting to log out
        try {
            final IPerson person = personManager.getPerson(request);
            if (person != null && person.getSecurityContext().isAuthenticated()) {
                this.portalEventFactory.publishLogoutEvent(request, this, person);
            }
        } catch (final Exception e) {
            logger.error("Exception recording logout " + "associated with request " + request, e);
        }
        final String originalUid = this.identitySwapperManager.getOriginalUsername(session);
        // Logging out from a swapped user, just redirect to the Login servlet
        if (originalUid != null) {
            redirect = request.getContextPath() + "/Login";
        } else {
            // Clear out the existing session for the user
            try {
                session.invalidate();
            } catch (final IllegalStateException ise) {
                // it need not insist that it be the one to perform the invalidating.
                if (logger.isTraceEnabled()) {
                    logger.trace("LogoutController encountered IllegalStateException invalidating a presumably already-invalidated session.", ise);
                }
            }
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Redirecting to " + redirect + " to send the user back to the guest page.");
    }
    final String encodedRedirectURL = response.encodeRedirectURL(redirect);
    response.sendRedirect(encodedRedirectURL);
}
Also used : IPerson(org.apereo.portal.security.IPerson) HttpSession(javax.servlet.http.HttpSession) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 93 with IPerson

use of org.apereo.portal.security.IPerson in project uPortal by Jasig.

the class LoginController method service.

/**
 * Process the incoming HttpServletRequest. Note that this processing occurs after
 * PortalPreAuthenticatedProcessingFilter has run and performed pre-processing.
 *
 * @param request
 * @param response
 * @exception ServletException
 * @exception IOException
 */
@RequestMapping
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // create the redirect URL, adding fname and args parameters if necessary
    String redirectTarget = null;
    // check for custom redirect strategies
    if (loginRedirect != null) {
        redirectTarget = loginRedirect.redirectTarget(request);
    }
    if (redirectTarget == null) {
        final String refUrl = request.getParameter(REFERER_URL_PARAM);
        final URL redirectLocation = parseLocalRefUrl(request, refUrl);
        if (redirectLocation != null) {
            redirectTarget = redirectLocation.toString();
        }
        if (redirectTarget == null) {
            /* Grab the target functional name, if any, off the login request.
                 * Also any arguments for the target. We will pass them  along after authentication.
                 */
            String targetFname = request.getParameter("uP_fname");
            if (targetFname == null) {
                final IPortalUrlBuilder defaultUrl = this.portalUrlProvider.getDefaultUrl(request);
                redirectTarget = defaultUrl.getUrlString();
            } else {
                try {
                    final IPortalUrlBuilder urlBuilder = this.portalUrlProvider.getPortalUrlBuilderByPortletFName(request, targetFname, UrlType.RENDER);
                    Enumeration<String> e = request.getParameterNames();
                    while (e.hasMoreElements()) {
                        String paramName = e.nextElement();
                        if (!paramName.equals("uP_fname")) {
                            urlBuilder.addParameter(paramName, request.getParameterValues(paramName));
                        }
                    }
                    redirectTarget = urlBuilder.getUrlString();
                } catch (IllegalArgumentException e) {
                    final IPortalUrlBuilder defaultUrl = this.portalUrlProvider.getDefaultUrl(request);
                    redirectTarget = defaultUrl.getUrlString();
                }
            }
        }
        IPerson person = null;
        final Object authError = request.getSession(false).getAttribute(LoginController.AUTH_ERROR_KEY);
        if (authError == null || !((Boolean) authError)) {
            person = this.personManager.getPerson(request);
        }
        if (person == null || !person.getSecurityContext().isAuthenticated()) {
            if (request.getMethod().equals("POST"))
                request.getSession(false).setAttribute(AUTH_ATTEMPTED_KEY, "true");
            // Preserve the attempted username so it can be redisplayed to the user
            String attemptedUserName = request.getParameter("userName");
            if (attemptedUserName != null)
                request.getSession(false).setAttribute(ATTEMPTED_USERNAME_KEY, request.getParameter("userName"));
        }
    }
    final String encodedRedirectURL = response.encodeRedirectURL(redirectTarget);
    if (log.isDebugEnabled()) {
        log.debug("Redirecting to " + redirectTarget);
    }
    response.sendRedirect(encodedRedirectURL);
}
Also used : IPerson(org.apereo.portal.security.IPerson) IPortalUrlBuilder(org.apereo.portal.url.IPortalUrlBuilder) URL(java.net.URL) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 94 with IPerson

use of org.apereo.portal.security.IPerson in project uPortal by Jasig.

the class MaxInactiveFilterTest method noTimeSetWorkflow.

@Test
public void noTimeSetWorkflow() throws IOException, ServletException {
    final HttpSession session = mock(HttpSession.class);
    final HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getSession(false)).thenReturn(session);
    // no calls, used in doFilter()
    final ServletResponse resp = mock(ServletResponse.class);
    final FilterChain chain = mock(FilterChain.class);
    final ISecurityContext securityContext = mock(ISecurityContext.class);
    when(securityContext.isAuthenticated()).thenReturn(true);
    final IPerson person = mock(IPerson.class);
    when(person.getSecurityContext()).thenReturn(securityContext);
    when(person.getAttribute(SESSION_MAX_INACTIVE_SET_ATTR)).thenReturn(null);
    when(person.getAttribute(IPerson.USERNAME)).thenReturn("jsmith");
    final IPersonManager personManager = mock(IPersonManager.class);
    when(personManager.getPerson(req)).thenReturn(person);
    final IMaxInactiveStrategy maxInactiveStrategy = mock(IMaxInactiveStrategy.class);
    final Integer interval = 5;
    when(maxInactiveStrategy.calcMaxInactive(person)).thenReturn(interval);
    final MaxInactiveFilter filter = new MaxInactiveFilter();
    ReflectionTestUtils.setField(filter, "personManager", personManager);
    ReflectionTestUtils.setField(filter, "maxInactiveStrategy", maxInactiveStrategy);
    filter.doFilter(req, resp, chain);
    verify(person, times(1)).setAttribute(eq(SESSION_MAX_INACTIVE_SET_ATTR), any(LocalDateTime.class));
    verify(session, times(1)).setMaxInactiveInterval(interval);
    verify(maxInactiveStrategy, times(1)).calcMaxInactive(person);
    verify(securityContext, times(1)).isAuthenticated();
    verify(person, times(1)).getSecurityContext();
    verify(person, times(1)).getAttribute(SESSION_MAX_INACTIVE_SET_ATTR);
    verify(person, times(2)).getAttribute(IPerson.USERNAME);
    verify(personManager, times(1)).getPerson(req);
    verifyNoMoreInteractions(resp);
    verify(chain, only()).doFilter(req, resp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LocalDateTime(java.time.LocalDateTime) ServletResponse(javax.servlet.ServletResponse) IPerson(org.apereo.portal.security.IPerson) IPersonManager(org.apereo.portal.security.IPersonManager) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) ISecurityContext(org.apereo.portal.security.ISecurityContext) Test(org.junit.Test)

Example 95 with IPerson

use of org.apereo.portal.security.IPerson in project uPortal by Jasig.

the class MaxInactiveFilterTest method timeSetInsideRefreshDurationWorkflow.

@Test
public void timeSetInsideRefreshDurationWorkflow() throws IOException, ServletException {
    final HttpSession session = mock(HttpSession.class);
    final HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getSession(false)).thenReturn(session);
    // no calls, used in doFilter()
    final ServletResponse resp = mock(ServletResponse.class);
    final FilterChain chain = mock(FilterChain.class);
    final ISecurityContext securityContext = mock(ISecurityContext.class);
    when(securityContext.isAuthenticated()).thenReturn(true);
    final IPerson person = mock(IPerson.class);
    when(person.getSecurityContext()).thenReturn(securityContext);
    final LocalDateTime lastTime = LocalDateTime.now(tz).minusMinutes(1);
    when(person.getAttribute(SESSION_MAX_INACTIVE_SET_ATTR)).thenReturn(lastTime);
    when(person.getAttribute(IPerson.USERNAME)).thenReturn("jsmith");
    final IPersonManager personManager = mock(IPersonManager.class);
    when(personManager.getPerson(req)).thenReturn(person);
    final IMaxInactiveStrategy maxInactiveStrategy = mock(IMaxInactiveStrategy.class);
    final MaxInactiveFilter filter = new MaxInactiveFilter();
    ReflectionTestUtils.setField(filter, "personManager", personManager);
    ReflectionTestUtils.setField(filter, "maxInactiveStrategy", maxInactiveStrategy);
    filter.doFilter(req, resp, chain);
    verify(securityContext, times(1)).isAuthenticated();
    verify(person, times(1)).getSecurityContext();
    verify(person, times(1)).getAttribute(SESSION_MAX_INACTIVE_SET_ATTR);
    verify(person, times(1)).getAttribute(IPerson.USERNAME);
    verify(personManager, times(1)).getPerson(req);
    verifyNoMoreInteractions(maxInactiveStrategy);
    verifyNoMoreInteractions(resp);
    verifyNoMoreInteractions(session);
    verify(chain, only()).doFilter(req, resp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LocalDateTime(java.time.LocalDateTime) ServletResponse(javax.servlet.ServletResponse) IPerson(org.apereo.portal.security.IPerson) IPersonManager(org.apereo.portal.security.IPersonManager) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) ISecurityContext(org.apereo.portal.security.ISecurityContext) Test(org.junit.Test)

Aggregations

IPerson (org.apereo.portal.security.IPerson)198 Test (org.junit.Test)52 PersonImpl (org.apereo.portal.security.provider.PersonImpl)45 ModelAndView (org.springframework.web.servlet.ModelAndView)43 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)34 HttpServletRequest (javax.servlet.http.HttpServletRequest)32 IUserInstance (org.apereo.portal.user.IUserInstance)27 HashMap (java.util.HashMap)25 HttpSession (javax.servlet.http.HttpSession)22 IAuthorizationPrincipal (org.apereo.portal.security.IAuthorizationPrincipal)22 ArrayList (java.util.ArrayList)20 EntityIdentifier (org.apereo.portal.EntityIdentifier)18 ISecurityContext (org.apereo.portal.security.ISecurityContext)17 IPersonAttributes (org.apereo.services.persondir.IPersonAttributes)17 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)15 List (java.util.List)14 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)12 Map (java.util.Map)11 Set (java.util.Set)11 IUserProfile (org.apereo.portal.IUserProfile)11