SessionManager { @Nullable public static SecurityContext createSecurityContextForSession(@Nonnull final HttpServletRequest httpServletRequest) { assert httpServletRequest != null; @Nullable final HttpSession httpSession = httpServletRequest.getSession(false); if (httpSession == null) { return null; } @Nullable final SessionData sessionData = getSessionData(httpSession); if (sessionData == null) { return null; } final SecurityContext securityContext = new SecurityContextImpl(new PrincipalImpl(sessionData.getUserId()), sessionData.getAuthenticationScheme()); return securityContext; } SessionManager();   SessionManager(@Nonnull final HttpServletRequest httpServletRequest); @Nonnull String startWebSession(@Nonnull final Principal principal); @Nonnull String startAppSession(@Nonnull final Principal principal); void terminateSession(); @Nullable String getCurrentSessionId(); @Nullable static SecurityContext createSecurityContextForSession(@Nonnull final HttpServletRequest httpServletRequest); static final String AUTHENTICATION_SCHEME_USERNAME; static final String AUTHENTICATION_SCHEME_APPTOKEN; }
@Test public void testCreateSecurityContextForSession() { LOG.info("testCreateSecurityContextForSession"); Mockito.when(httpServletRequest.getSession(false)).thenReturn(httpSession); @Nonnull final SessionManager.SessionData sessionData = Mockito.mock(SessionManager.SessionData.class); Mockito.doReturn(REFERENCE_PERSON_ID).when(sessionData).getUserId(); Mockito.doReturn(SessionManager.AUTHENTICATION_SCHEME_USERNAME).when(sessionData).getAuthenticationScheme(); Mockito.when(httpSession.getAttribute(SESSION_DATA_KEY)).thenReturn(sessionData); final SecurityContext securityContext = SessionManager.createSecurityContextForSession(httpServletRequest); assertThat(securityContext, is(notNullValue())); assertThat(securityContext, is(instanceOf(SecurityContextImpl.class))); assert securityContext != null; final String authenticationScheme = securityContext.getAuthenticationScheme(); assertThat(authenticationScheme, is(notNullValue())); assertThat(securityContext.getAuthenticationScheme(), is(SessionManager.AUTHENTICATION_SCHEME_USERNAME)); final Principal userPrincipal = securityContext.getUserPrincipal(); assertThat(userPrincipal, is(notNullValue())); assertThat(userPrincipal.getName(), is(REFERENCE_PERSON_ID)); verify(httpServletRequest).getSession(false); verify(httpSession).getAttribute(SESSION_DATA_KEY); }
@Test public void testCreateSecurityContextForSessionNoAuthenticatedUser() { LOG.info("testCreateSecurityContextForSessionNoAuthenticatedUser"); Mockito.when(httpServletRequest.getSession(false)).thenReturn(httpSession); Mockito.when(httpSession.getAttribute(SESSION_DATA_KEY)).thenReturn(null); assertThat(SessionManager.createSecurityContextForSession(httpServletRequest), is(nullValue())); verify(httpServletRequest).getSession(false); verify(httpSession).getAttribute(SESSION_DATA_KEY); }
@Test public void testCreateSecurityContextForSessionNoCurrentSession() { LOG.info("testCreateSecurityContextForSessionNoCurrentSession"); Mockito.when(httpServletRequest.getSession(false)).thenReturn(null); assertThat(SessionManager.createSecurityContextForSession(httpServletRequest), is(nullValue())); verify(httpServletRequest).getSession(false); }