Search in sources :

Example 1 with SyncopeClient

use of org.apache.syncope.client.lib.SyncopeClient in project testcases by coheigea.

the class SyncopeDeployer method deployUserData.

@SuppressWarnings("unchecked")
public void deployUserData() {
    WebClient client = WebClient.create(address);
    client = client.type("application/xml");
    String authorizationHeader = "Basic " + Base64Utility.encode(("admin" + ":" + "password").getBytes());
    client.header("Authorization", authorizationHeader);
    client.accept("application/xml");
    // Create the groups first
    client = client.path("groups");
    PagedResult<GroupTO> existingGroups = (PagedResult<GroupTO>) client.get(PagedResult.class);
    GroupTO bossGroup = findOrCreateGroup("boss", existingGroups, client);
    GroupTO employeeGroup = findOrCreateGroup("employee", existingGroups, client);
    // Now create the users
    client = client.replacePath("users");
    PagedResult<UserTO> existingUsers = (PagedResult<UserTO>) client.get(PagedResult.class);
    if (!doesUserAlreadyExist("alice", existingUsers.getResult())) {
        UserTO user = new UserTO();
        user.setUsername("alice");
        user.setPassword("security");
        user.setRealm("/");
        MembershipTO membership = new MembershipTO();
        membership.setGroupKey(employeeGroup.getKey());
        // membership.setGroupName(employeeGroup.getName());
        user.getMemberships().add(membership);
        membership = new MembershipTO();
        // membership.setGroupName(bossGroup.getName());
        membership.setGroupKey(bossGroup.getKey());
        user.getMemberships().add(membership);
        client.post(user, ProvisioningResult.class);
    }
    if (!doesUserAlreadyExist("bob", existingUsers.getResult())) {
        UserTO user = new UserTO();
        user.setUsername("bob");
        user.setPassword("security");
        user.setRealm("/");
        MembershipTO membership = new MembershipTO();
        membership.setGroupKey(employeeGroup.getKey());
        // membership.setGroupName(employeeGroup.getName());
        user.getMemberships().add(membership);
        client.post(user, ProvisioningResult.class);
    }
    client.close();
    // Check via the client API that the users were created correctly
    SyncopeClientFactoryBean clientFactory = new SyncopeClientFactoryBean().setAddress(address);
    SyncopeClient syncopeClient = clientFactory.create("admin", "password");
    UserService userService = syncopeClient.getService(UserService.class);
    int count = userService.search(new AnyQuery.Builder().build()).getTotalCount();
    Assert.assertEquals(2, count);
}
Also used : UserService(org.apache.syncope.common.rest.api.service.UserService) WebClient(org.apache.cxf.jaxrs.client.WebClient) SyncopeClient(org.apache.syncope.client.lib.SyncopeClient) GroupTO(org.apache.syncope.common.lib.to.GroupTO) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) SyncopeClientFactoryBean(org.apache.syncope.client.lib.SyncopeClientFactoryBean) AnyQuery(org.apache.syncope.common.rest.api.beans.AnyQuery) PagedResult(org.apache.syncope.common.lib.to.PagedResult)

Example 2 with SyncopeClient

use of org.apache.syncope.client.lib.SyncopeClient in project syncope by apache.

the class SyncopeServices method testUsernameAndPassword.

public static void testUsernameAndPassword(final String username, final String password) {
    final Properties properties = new Properties();
    try (InputStream is = Files.newInputStream(Paths.get(InstallConfigFileTemplate.configurationFilePath()))) {
        properties.load(is);
    } catch (final IOException e) {
        LOG.error("Error opening properties file", e);
    }
    final SyncopeClient syncopeClient = new SyncopeClientFactoryBean().setAddress(properties.getProperty("syncope.rest.services")).create(username, password);
    syncopeClient.getService(SyncopeService.class).platform();
}
Also used : SyncopeService(org.apache.syncope.common.rest.api.service.SyncopeService) InputStream(java.io.InputStream) SyncopeClientFactoryBean(org.apache.syncope.client.lib.SyncopeClientFactoryBean) IOException(java.io.IOException) Properties(java.util.Properties) SyncopeClient(org.apache.syncope.client.lib.SyncopeClient)

Example 3 with SyncopeClient

use of org.apache.syncope.client.lib.SyncopeClient in project syncope by apache.

the class SyncopeServices method get.

public static <T> T get(final Class<T> clazz) {
    final Properties properties = new Properties();
    try (InputStream is = Files.newInputStream(Paths.get(InstallConfigFileTemplate.configurationFilePath()))) {
        properties.load(is);
    } catch (final IOException e) {
        LOG.error("Error opening properties file", e);
    }
    String syncopeAdminPassword = JasyptUtils.get().decrypt(properties.getProperty("syncope.admin.password"));
    SYNCOPE_ADDRESS = properties.getProperty("syncope.rest.services");
    String useGZIPCompression = properties.getProperty("useGZIPCompression");
    SyncopeClient syncopeClient = new SyncopeClientFactoryBean().setAddress(SYNCOPE_ADDRESS).setUseCompression(BooleanUtils.toBoolean(useGZIPCompression)).create(properties.getProperty("syncope.admin.user"), syncopeAdminPassword);
    LOG.debug("Creating service for {}", clazz.getName());
    return syncopeClient.getService(clazz);
}
Also used : InputStream(java.io.InputStream) SyncopeClientFactoryBean(org.apache.syncope.client.lib.SyncopeClientFactoryBean) IOException(java.io.IOException) Properties(java.util.Properties) SyncopeClient(org.apache.syncope.client.lib.SyncopeClient)

Example 4 with SyncopeClient

use of org.apache.syncope.client.lib.SyncopeClient in project syncope by apache.

the class UserSelfITCase method passwordResetWithoutSecurityQuestion.

@Test
public void passwordResetWithoutSecurityQuestion() {
    // 0. disable security question for password reset
    configurationService.set(attrTO("passwordReset.securityQuestion", "false"));
    // 1. create an user with security question and answer
    UserTO user = UserITCase.getUniqueSampleTO("pwdResetNoSecurityQuestion@syncope.apache.org");
    createUser(user);
    // 2. verify that new user is able to authenticate
    SyncopeClient authClient = clientFactory.create(user.getUsername(), "password123");
    UserTO read = authClient.self().getRight();
    assertNotNull(read);
    // 3. request password reset (as anonymous) with no security answer
    SyncopeClient anonClient = clientFactory.create();
    anonClient.getService(UserSelfService.class).requestPasswordReset(user.getUsername(), null);
    // 4. get token (normally sent via e-mail, now reading as admin)
    String token = userService.read(read.getKey()).getToken();
    assertNotNull(token);
    // 5. confirm password reset
    try {
        anonClient.getService(UserSelfService.class).confirmPasswordReset("WRONG TOKEN", "newPassword");
        fail("This should not happen");
    } catch (SyncopeClientException e) {
        assertEquals(ClientExceptionType.NotFound, e.getType());
        assertTrue(e.getMessage().contains("WRONG TOKEN"));
    }
    anonClient.getService(UserSelfService.class).confirmPasswordReset(token, "newPassword123");
    // 6. verify that password was reset and token removed
    authClient = clientFactory.create(user.getUsername(), "newPassword123");
    read = authClient.self().getRight();
    assertNotNull(read);
    assertNull(read.getToken());
    // 7. re-enable security question for password reset
    configurationService.set(attrTO("passwordReset.securityQuestion", "true"));
}
Also used : UserSelfService(org.apache.syncope.common.rest.api.service.UserSelfService) UserTO(org.apache.syncope.common.lib.to.UserTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SyncopeClient(org.apache.syncope.client.lib.SyncopeClient) Test(org.junit.jupiter.api.Test)

Example 5 with SyncopeClient

use of org.apache.syncope.client.lib.SyncopeClient in project syncope by apache.

the class UserSelfITCase method mustChangePassword.

@Test
public void mustChangePassword() {
    // PRE: reset vivaldi's password
    UserPatch userPatch = new UserPatch();
    userPatch.setKey("b3cbc78d-32e6-4bd4-92e0-bbe07566a2ee");
    userPatch.setPassword(new PasswordPatch.Builder().value("password321").build());
    userService.update(userPatch);
    // 0. access as vivaldi -> succeed
    SyncopeClient vivaldiClient = clientFactory.create("vivaldi", "password321");
    Pair<Map<String, Set<String>>, UserTO> self = vivaldiClient.self();
    assertFalse(self.getRight().isMustChangePassword());
    // 1. update user vivaldi (3) requirig password update
    userPatch = new UserPatch();
    userPatch.setKey("b3cbc78d-32e6-4bd4-92e0-bbe07566a2ee");
    userPatch.setMustChangePassword(new BooleanReplacePatchItem.Builder().value(true).build());
    UserTO vivaldi = updateUser(userPatch).getEntity();
    assertTrue(vivaldi.isMustChangePassword());
    // 2. attempt to access -> fail
    try {
        vivaldiClient.getService(ResourceService.class).list();
        fail("This should not happen");
    } catch (ForbiddenException e) {
        assertNotNull(e);
        assertEquals("Please change your password first", e.getMessage());
    }
    // 3. change password
    vivaldiClient.getService(UserSelfService.class).changePassword("password123");
    // 4. verify it worked
    self = clientFactory.create("vivaldi", "password123").self();
    assertFalse(self.getRight().isMustChangePassword());
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) PasswordPatch(org.apache.syncope.common.lib.patch.PasswordPatch) UserSelfService(org.apache.syncope.common.rest.api.service.UserSelfService) ResourceService(org.apache.syncope.common.rest.api.service.ResourceService) BooleanReplacePatchItem(org.apache.syncope.common.lib.patch.BooleanReplacePatchItem) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) SyncopeClient(org.apache.syncope.client.lib.SyncopeClient) UserTO(org.apache.syncope.common.lib.to.UserTO) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

SyncopeClient (org.apache.syncope.client.lib.SyncopeClient)40 Test (org.junit.jupiter.api.Test)31 Response (javax.ws.rs.core.Response)15 UserTO (org.apache.syncope.common.lib.to.UserTO)15 UserSelfService (org.apache.syncope.common.rest.api.service.UserSelfService)15 AccessControlException (java.security.AccessControlException)12 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)11 Date (java.util.Date)10 HmacJwsSignatureProvider (org.apache.cxf.rs.security.jose.jws.HmacJwsSignatureProvider)10 JwsHeaders (org.apache.cxf.rs.security.jose.jws.JwsHeaders)10 JwsJwtCompactProducer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer)10 JwsSignatureProvider (org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider)10 NoneJwsSignatureProvider (org.apache.cxf.rs.security.jose.jws.NoneJwsSignatureProvider)10 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)10 Calendar (java.util.Calendar)9 IOException (java.io.IOException)8 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)8 AccessTokenService (org.apache.syncope.common.rest.api.service.AccessTokenService)8 JwsJwtCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer)7 SyncopeClientFactoryBean (org.apache.syncope.client.lib.SyncopeClientFactoryBean)7