Search in sources :

Example 6 with User

use of com.enonic.xp.security.User in project xp by enonic.

the class ContextScriptTest method initialize.

@Override
protected void initialize() throws Exception {
    super.initialize();
    final SecurityService securityService = Mockito.mock(SecurityService.class);
    addService(SecurityService.class, securityService);
    final User user = User.create().login(PrincipalKey.ofSuperUser().getId()).displayName("Super User").key(PrincipalKey.ofSuperUser()).build();
    final AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN, RoleKeys.EVERYONE).build();
    Mockito.when(securityService.authenticate(Mockito.any())).thenReturn(authInfo);
}
Also used : User(com.enonic.xp.security.User) SecurityService(com.enonic.xp.security.SecurityService) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 7 with User

use of com.enonic.xp.security.User in project xp by enonic.

the class IdProviderFilterTest method testExecuteAuthenticated.

@Test
public void testExecuteAuthenticated() throws Exception {
    final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.system(), "user1")).displayName("User 1").email("user1@enonic.com").login("user1").build();
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN_LOGIN).build();
    ContextBuilder.create().authInfo(authenticationInfo).build().callWith(() -> {
        final HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
        final HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class);
        final FilterChain filterChain = Mockito.mock(FilterChain.class);
        idProviderFilter.doHandle(httpServletRequest, httpServletResponse, filterChain);
        Mockito.verify(idProviderControllerService, Mockito.times(0)).execute(Mockito.any());
        return null;
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.enonic.xp.security.User) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test)

Example 8 with User

use of com.enonic.xp.security.User in project xp by enonic.

the class AuthResourceTest method testAuthenticated_authenticated.

@Test
public void testAuthenticated_authenticated() throws Exception {
    final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.system(), "user1")).displayName("User 1").modifiedTime(Instant.now(clock)).email("user1@enonic.com").login("user1").build();
    final LocalScope localScope = ContextAccessor.current().getLocalScope();
    final AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN_LOGIN).build();
    localScope.setAttribute(authInfo);
    localScope.setSession(new SessionMock());
    String jsonString = request().path("auth/authenticated").get().getAsString();
    assertJson("authenticated_success.json", jsonString);
}
Also used : User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) LocalScope(com.enonic.xp.context.LocalScope) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 9 with User

use of com.enonic.xp.security.User in project xp by enonic.

the class LogAuditLogParams method getUserKey.

private PrincipalKey getUserKey() {
    final Context context = ContextAccessor.current();
    final User user = context.getAuthInfo().getUser() != null ? context.getAuthInfo().getUser() : User.ANONYMOUS;
    return user.getKey();
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User)

Example 10 with User

use of com.enonic.xp.security.User in project xp by enonic.

the class AuthenticationInfoTest method testSerialization.

@Test
void testSerialization() throws Exception {
    final User user = User.create().login("userlogin").displayName("my user").key(PrincipalKey.ofUser(IdProviderKey.from("myidprovider"), "userid")).email("user@email").description("description").modifiedTime(Instant.now(clock)).build();
    final IdProviderKey idProvider = IdProviderKey.from("myStore");
    final PrincipalKey group1 = PrincipalKey.ofGroup(idProvider, "group1");
    final PrincipalKey group2 = PrincipalKey.from("group:myStore:group2");
    final PrincipalKey role1 = PrincipalKey.from("role:administrators");
    final AuthenticationInfo info = AuthenticationInfo.create().user(user).principals(group1).principals(PrincipalKeys.from(group2, role1)).build();
    final byte[] serializedObject = serialize(info);
    final AuthenticationInfo deserializedObject = (AuthenticationInfo) deserialize(serializedObject);
    assertEquals(deserializedObject, info);
}
Also used : User(com.enonic.xp.security.User) IdProviderKey(com.enonic.xp.security.IdProviderKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Aggregations

User (com.enonic.xp.security.User)63 Test (org.junit.jupiter.api.Test)40 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)22 PropertyTree (com.enonic.xp.data.PropertyTree)17 PrincipalKey (com.enonic.xp.security.PrincipalKey)17 Context (com.enonic.xp.context.Context)14 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)11 CreateUserParams (com.enonic.xp.security.CreateUserParams)10 IdProviderKey (com.enonic.xp.security.IdProviderKey)10 Node (com.enonic.xp.node.Node)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ContextBuilder (com.enonic.xp.context.ContextBuilder)6 UpdateUserParams (com.enonic.xp.security.UpdateUserParams)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Mockito (org.mockito.Mockito)6 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)4 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)4 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)4 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)4 TaskId (com.enonic.xp.task.TaskId)4