Search in sources :

Example 11 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)

Example 12 with Session

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

the class LoginHandlerTest method testLoginNoIdProviders.

@Test
public void testLoginNoIdProviders() {
    final IdProviders idProviders = IdProviders.from(IdProvider.create().displayName("system").key(IdProviderKey.from("system")).build());
    final AuthenticationInfo authInfo = TestDataFixtures.createAuthenticationInfo();
    Mockito.when(this.securityService.authenticate(Mockito.any())).thenReturn(authInfo);
    Mockito.when(this.securityService.getIdProviders()).thenReturn(idProviders);
    runFunction("/test/login-test.js", "loginNoIdProvider");
    final Session session = ContextAccessor.current().getLocalScope().getSession();
    final AuthenticationInfo sessionAuthInfo = session.getAttribute(AuthenticationInfo.class);
    assertEquals(authInfo, sessionAuthInfo);
}
Also used : IdProviders(com.enonic.xp.security.IdProviders) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Session(com.enonic.xp.session.Session) Test(org.junit.jupiter.api.Test)

Example 13 with Session

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

the class ContextMapper method getAttributes.

private Map<String, Object> getAttributes() {
    LocalScope localScope = this.context.getLocalScope();
    Map<String, Object> attributes = this.context.getAttributes();
    Map<String, Object> localAttributes = localScope.getAttributes();
    Session session = localScope.getSession();
    Map<String, Object> sessionAttributes = session == null ? Map.of() : session.getAttributes();
    return Stream.of(attributes, localAttributes, sessionAttributes).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, HashMap::new));
}
Also used : PrincipalMapper(com.enonic.xp.lib.common.PrincipalMapper) User(com.enonic.xp.security.User) LocalScope(com.enonic.xp.context.LocalScope) HashMap(java.util.HashMap) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Collectors(java.util.stream.Collectors) Session(com.enonic.xp.session.Session) Stream(java.util.stream.Stream) PrincipalKey(com.enonic.xp.security.PrincipalKey) Map(java.util.Map) Context(com.enonic.xp.context.Context) MapGenerator(com.enonic.xp.script.serializer.MapGenerator) MapSerializable(com.enonic.xp.script.serializer.MapSerializable) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) HashMap(java.util.HashMap) Map(java.util.Map) LocalScope(com.enonic.xp.context.LocalScope) Session(com.enonic.xp.session.Session)

Example 14 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)

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