Search in sources :

Example 6 with Session

use of com.enonic.xp.session.Session in project xp by enonic.

the class AuthResource method isAuthenticated.

@GET
@Path("authenticated")
public LoginResultJson isAuthenticated() {
    final Session session = ContextAccessor.current().getLocalScope().getSession();
    if (session == null) {
        return new LoginResultJson(AuthenticationInfo.unAuthenticated());
    }
    final AuthenticationInfo authInfo = ContextAccessor.current().getAuthInfo();
    return new LoginResultJson(authInfo);
}
Also used : LoginResultJson(com.enonic.xp.admin.impl.rest.resource.auth.json.LoginResultJson) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Session(com.enonic.xp.session.Session) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 7 with Session

use of com.enonic.xp.session.Session in project xp by enonic.

the class LocalScopeImplTest method testAttributeByType_session.

@Test
public void testAttributeByType_session() {
    final SampleValue value = new SampleValue();
    final Session session = new SessionMock();
    session.setAttribute(value);
    final LocalScopeImpl context = new LocalScopeImpl();
    context.setSession(session);
    assertSame(session, context.getSession());
    assertSame(value, context.getAttribute(SampleValue.class));
    assertEquals(0, context.getAttributes().size());
}
Also used : Session(com.enonic.xp.session.Session) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 8 with Session

use of com.enonic.xp.session.Session in project xp by enonic.

the class LocalScopeImplTest method testAttributeByKey_session.

@Test
public void testAttributeByKey_session() {
    final Session session = new SessionMock();
    session.setAttribute("key1", "value1");
    final LocalScopeImpl context = new LocalScopeImpl();
    context.setSession(session);
    assertSame(session, context.getSession());
    assertEquals("value1", context.getAttribute("key1"));
    assertEquals(0, context.getAttributes().size());
}
Also used : Session(com.enonic.xp.session.Session) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 9 with Session

use of com.enonic.xp.session.Session in project xp by enonic.

the class LoginHandler method createSession.

private void createSession(final AuthenticationInfo authInfo) {
    final LocalScope localScope = this.context.get().getLocalScope();
    final Session session = localScope.getSession();
    if (session != null) {
        final var attributes = session.getAttributes();
        session.invalidate();
        final Session newSession = localScope.getSession();
        if (newSession != null) {
            attributes.forEach(newSession::setAttribute);
            session.setAttribute(authInfo);
            if (this.sessionTimeout != null) {
                setSessionTimeout();
            }
        }
    }
}
Also used : LocalScope(com.enonic.xp.context.LocalScope) HttpSession(javax.servlet.http.HttpSession) Session(com.enonic.xp.session.Session)

Example 10 with Session

use of com.enonic.xp.session.Session in project xp by enonic.

the class LoginHandlerTest method testLoginSuccessNoSession.

@Test
public void testLoginSuccessNoSession() {
    final AuthenticationInfo authInfo = AuthenticationInfo.create().user(TestDataFixtures.getTestUser()).principals(RoleKeys.ADMIN_LOGIN).build();
    Mockito.when(this.securityService.authenticate(Mockito.any())).thenReturn(authInfo);
    runFunction("/test/login-test.js", "loginSuccessNoSession");
    final AuthenticationInfo localScopeAuth = ContextAccessor.current().getLocalScope().getAttribute(AuthenticationInfo.class);
    final Session session = ContextAccessor.current().getLocalScope().getSession();
    final AuthenticationInfo sessionAuthInfo = session.getAttribute(AuthenticationInfo.class);
    assertEquals(authInfo, localScopeAuth);
    assertEquals(null, sessionAuthInfo);
}
Also used : AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Session(com.enonic.xp.session.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Session (com.enonic.xp.session.Session)14 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)11 Test (org.junit.jupiter.api.Test)10 SessionMock (com.enonic.xp.session.SessionMock)4 Context (com.enonic.xp.context.Context)3 User (com.enonic.xp.security.User)3 LocalScope (com.enonic.xp.context.LocalScope)2 IdProviders (com.enonic.xp.security.IdProviders)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 LoginResultJson (com.enonic.xp.admin.impl.rest.resource.auth.json.LoginResultJson)1 PublicApi (com.enonic.xp.annotation.PublicApi)1 Branch (com.enonic.xp.branch.Branch)1 PrincipalMapper (com.enonic.xp.lib.common.PrincipalMapper)1 RepositoryId (com.enonic.xp.repository.RepositoryId)1 MapGenerator (com.enonic.xp.script.serializer.MapGenerator)1 MapSerializable (com.enonic.xp.script.serializer.MapSerializable)1 IdProvider (com.enonic.xp.security.IdProvider)1