Search in sources :

Example 61 with AuthenticationInfo

use of com.enonic.xp.security.auth.AuthenticationInfo in project xp by enonic.

the class LoginHandlerTest method testExamples.

@Test
public void testExamples() {
    final AuthenticationInfo authInfo = TestDataFixtures.createAuthenticationInfo();
    final IdProviders idProviders = IdProviders.from(IdProvider.create().displayName("system").key(IdProviderKey.from("system")).build());
    Mockito.when(this.securityService.authenticate(Mockito.any())).thenReturn(authInfo);
    Mockito.when(this.securityService.getIdProviders()).thenReturn(idProviders);
    runScript("/lib/xp/examples/auth/login.js");
}
Also used : IdProviders(com.enonic.xp.security.IdProviders) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test)

Example 62 with AuthenticationInfo

use of com.enonic.xp.security.auth.AuthenticationInfo 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 63 with AuthenticationInfo

use of com.enonic.xp.security.auth.AuthenticationInfo in project xp by enonic.

the class LoginHandlerTest method testLoginSuccess.

@Test
public void testLoginSuccess() {
    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", "loginSuccess");
    final Session session = ContextAccessor.current().getLocalScope().getSession();
    final AuthenticationInfo sessionAuthInfo = session.getAttribute(AuthenticationInfo.class);
    assertEquals(authInfo, sessionAuthInfo);
}
Also used : AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Session(com.enonic.xp.session.Session) Test(org.junit.jupiter.api.Test)

Example 64 with AuthenticationInfo

use of com.enonic.xp.security.auth.AuthenticationInfo in project xp by enonic.

the class ContextMapperTest method test.

@Test
public void test() {
    User user = User.create().login(PrincipalKey.ofSuperUser().getId()).displayName("Super User").key(PrincipalKey.ofSuperUser()).build();
    AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN, RoleKeys.EVERYONE).build();
    Context context = ContextBuilder.create().repositoryId(RepositoryId.from("repository.id")).branch(Branch.create().value("master").build()).authInfo(authInfo).attribute("attrAsString", "value").attribute("attrAsInteger", Integer.MAX_VALUE).attribute("attrAsLong", Long.MIN_VALUE).attribute("attrAsBoolean", true).attribute("authInfoDetails", authInfo).attribute("testMapper", new TestMapper()).build();
    context.getLocalScope().setAttribute("attrAsString", "localValue");
    context.getLocalScope().setAttribute("attr1", "localValue");
    context.getLocalScope().setSession(new SessionMock());
    context.getLocalScope().getSession().setAttribute("attrAsString", "sessionValue");
    context.getLocalScope().getSession().setAttribute("attr2", "sessionValue");
    JsonMapGenerator generator = new JsonMapGenerator();
    new ContextMapper(context).serialize(generator);
    JsonNode actualJson = (JsonNode) generator.getRoot();
    JsonNode attributes = actualJson.get("attributes");
    assertNull(attributes.get("authInfoDetails"));
    assertNull(attributes.get(Branch.class.getName()));
    assertNull(attributes.get(RepositoryId.class.getName()));
    assertNull(attributes.get(AuthenticationInfo.class.getName()));
    assertEquals("value", attributes.get("attrAsString").asText());
    assertEquals(Integer.MAX_VALUE, attributes.get("attrAsInteger").asInt());
    assertTrue(attributes.get("attrAsBoolean").asBoolean());
    assertEquals(Long.MIN_VALUE, attributes.get("attrAsLong").asLong());
    assertNotNull(attributes.get("testMapper"));
    assertEquals("localValue", attributes.get("attr1").asText());
    assertEquals("sessionValue", attributes.get("attr2").asText());
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 65 with AuthenticationInfo

use of com.enonic.xp.security.auth.AuthenticationInfo in project xp by enonic.

the class AuthHelper method authenticate.

private AuthenticationInfo authenticate(final String user, final String password, final boolean rememberMe) {
    AuthenticationInfo authInfo = null;
    if (isValidEmail(user)) {
        final EmailPasswordAuthToken emailAuthToken = new EmailPasswordAuthToken();
        emailAuthToken.setEmail(user);
        emailAuthToken.setPassword(password);
        emailAuthToken.setRememberMe(rememberMe);
        authInfo = securityService.authenticate(emailAuthToken);
    }
    if (authInfo == null || !authInfo.isAuthenticated()) {
        final UsernamePasswordAuthToken usernameAuthToken = new UsernamePasswordAuthToken();
        usernameAuthToken.setUsername(user);
        usernameAuthToken.setPassword(password);
        usernameAuthToken.setRememberMe(rememberMe);
        authInfo = securityService.authenticate(usernameAuthToken);
    }
    return authInfo;
}
Also used : UsernamePasswordAuthToken(com.enonic.xp.security.auth.UsernamePasswordAuthToken) EmailPasswordAuthToken(com.enonic.xp.security.auth.EmailPasswordAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Aggregations

AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)65 Test (org.junit.jupiter.api.Test)44 User (com.enonic.xp.security.User)17 Context (com.enonic.xp.context.Context)16 Session (com.enonic.xp.session.Session)9 CreateUserParams (com.enonic.xp.security.CreateUserParams)7 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)6 IdProviders (com.enonic.xp.security.IdProviders)6 EmailPasswordAuthToken (com.enonic.xp.security.auth.EmailPasswordAuthToken)6 PropertyTree (com.enonic.xp.data.PropertyTree)5 AccessControlList (com.enonic.xp.security.acl.AccessControlList)5 UsernamePasswordAuthToken (com.enonic.xp.security.auth.UsernamePasswordAuthToken)5 VerifiedUsernameAuthToken (com.enonic.xp.security.auth.VerifiedUsernameAuthToken)5 PrincipalKey (com.enonic.xp.security.PrincipalKey)4 SessionMock (com.enonic.xp.session.SessionMock)4 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)2 Content (com.enonic.xp.content.Content)2 ProjectName (com.enonic.xp.project.ProjectName)2 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)2 IdProvider (com.enonic.xp.security.IdProvider)2