Search in sources :

Example 16 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method sendResetPasswordEmailWithRedirectAndCustomLifespan.

@Test
@AuthServerContainerExclude(AuthServer.REMOTE)
public void sendResetPasswordEmailWithRedirectAndCustomLifespan() throws IOException {
    UserRepresentation userRep = new UserRepresentation();
    userRep.setEnabled(true);
    userRep.setUsername("user1");
    userRep.setEmail("user1@test.com");
    String id = createUser(userRep);
    UserResource user = realm.users().get(id);
    ClientRepresentation client = new ClientRepresentation();
    client.setClientId("myclient");
    client.setRedirectUris(new LinkedList<>());
    client.getRedirectUris().add("http://myclient.com/*");
    client.setName("myclient");
    client.setEnabled(true);
    Response response = realm.clients().create(client);
    String createdId = ApiUtil.getCreatedId(response);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.clientResourcePath(createdId), client, ResourceType.CLIENT);
    List<String> actions = new LinkedList<>();
    actions.add(UserModel.RequiredAction.UPDATE_PASSWORD.name());
    final int lifespan = (int) TimeUnit.DAYS.toSeconds(128);
    try {
        // test that an invalid redirect uri is rejected.
        user.executeActionsEmail("myclient", "http://unregistered-uri.com/", lifespan, actions);
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("Invalid redirect uri.", error.getErrorMessage());
    }
    user.executeActionsEmail("myclient", "http://myclient.com/home.html", lifespan, actions);
    assertAdminEvents.assertEvent(realmId, OperationType.ACTION, AdminEventPaths.userResourcePath(id) + "/execute-actions-email", ResourceType.USER);
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    MimeMessage message = greenMail.getReceivedMessages()[0];
    MailUtils.EmailBody body = MailUtils.getBody(message);
    assertTrue(body.getText().contains("This link will expire within 128 days"));
    assertTrue(body.getHtml().contains("This link will expire within 128 days"));
    String link = MailUtils.getPasswordResetEmailLink(message);
    String token = link.substring(link.indexOf("key=") + "key=".length());
    try {
        final AccessToken accessToken = TokenVerifier.create(token, AccessToken.class).getToken();
        assertEquals(lifespan, accessToken.getExpiration() - accessToken.getIssuedAt());
    } catch (VerificationException e) {
        throw new IOException(e);
    }
    driver.navigate().to(link);
    proceedPage.assertCurrent();
    assertThat(proceedPage.getInfo(), Matchers.containsString("Update Password"));
    proceedPage.clickProceedLink();
    passwordUpdatePage.assertCurrent();
    passwordUpdatePage.changePassword("new-pass", "new-pass");
    assertEquals("Your account has been updated.", driver.findElement(By.id("kc-page-title")).getText());
    String pageSource = driver.getPageSource();
    // check to make sure the back link is set.
    Assert.assertTrue(pageSource.contains("http://myclient.com/home.html"));
    driver.navigate().to(link);
    assertEquals("We are sorry...", PageUtils.getPageTitle(driver));
}
Also used : UserResource(org.keycloak.admin.client.resource.UserResource) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Response(javax.ws.rs.core.Response) MimeMessage(javax.mail.internet.MimeMessage) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) AccessToken(org.keycloak.representations.AccessToken) ClientErrorException(javax.ws.rs.ClientErrorException) VerificationException(org.keycloak.common.VerificationException) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) MailUtils(org.keycloak.testsuite.util.MailUtils) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test)

Example 17 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method createUserWithoutUsername.

@Test
public void createUserWithoutUsername() {
    UserRepresentation user = new UserRepresentation();
    user.setEmail("user1@localhost");
    try (Response response = realm.users().create(user)) {
        assertEquals(400, response.getStatus());
        ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User name is missing", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
}
Also used : Response(javax.ws.rs.core.Response) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 18 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method sendVerifyEmail.

@Test
@AuthServerContainerExclude(AuthServer.REMOTE)
public void sendVerifyEmail() throws IOException {
    UserRepresentation userRep = new UserRepresentation();
    userRep.setUsername("user1");
    String id = createUser(userRep);
    UserResource user = realm.users().get(id);
    try {
        user.sendVerifyEmail();
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User email missing", error.getErrorMessage());
    }
    try {
        userRep = user.toRepresentation();
        userRep.setEmail("user1@localhost");
        userRep.setEnabled(false);
        updateUser(user, userRep);
        user.sendVerifyEmail();
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User is disabled", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
    try {
        userRep.setEnabled(true);
        updateUser(user, userRep);
        user.sendVerifyEmail("invalidClientId");
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("Client doesn't exist", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
    user.sendVerifyEmail();
    assertAdminEvents.assertEvent(realmId, OperationType.ACTION, AdminEventPaths.userResourcePath(id) + "/send-verify-email", ResourceType.USER);
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    String link = MailUtils.getPasswordResetEmailLink(greenMail.getReceivedMessages()[0]);
    driver.navigate().to(link);
    proceedPage.assertCurrent();
    assertThat(proceedPage.getInfo(), Matchers.containsString("Verify Email"));
    proceedPage.clickProceedLink();
    Assert.assertEquals("Your account has been updated.", infoPage.getInfo());
    driver.navigate().to("about:blank");
    // It should be possible to use the same action token multiple times
    driver.navigate().to(link);
    proceedPage.assertCurrent();
    assertThat(proceedPage.getInfo(), Matchers.containsString("Verify Email"));
    proceedPage.clickProceedLink();
    Assert.assertEquals("Your account has been updated.", infoPage.getInfo());
}
Also used : ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserResource(org.keycloak.admin.client.resource.UserResource) ClientErrorException(javax.ws.rs.ClientErrorException) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test)

Example 19 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method createUserWithInvalidPolicyPassword.

@Test
public void createUserWithInvalidPolicyPassword() {
    RealmRepresentation rep = realm.toRepresentation();
    String passwordPolicy = rep.getPasswordPolicy();
    rep.setPasswordPolicy("length(8)");
    realm.update(rep);
    UserRepresentation user = new UserRepresentation();
    user.setUsername("user4");
    user.setEmail("user4@localhost");
    CredentialRepresentation rawPassword = new CredentialRepresentation();
    rawPassword.setValue("ABCD");
    rawPassword.setType(CredentialRepresentation.PASSWORD);
    user.setCredentials(Collections.singletonList(rawPassword));
    assertAdminEvents.clear();
    try (Response response = realm.users().create(user)) {
        assertEquals(400, response.getStatus());
        ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
        Assert.assertEquals("Password policy not met", error.getErrorMessage());
        rep.setPasswordPolicy(passwordPolicy);
        assertAdminEvents.assertEmpty();
        realm.update(rep);
    }
}
Also used : CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) Response(javax.ws.rs.core.Response) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 20 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method sendResetPasswordEmail.

@Test
public void sendResetPasswordEmail() {
    UserRepresentation userRep = new UserRepresentation();
    userRep.setUsername("user1");
    String id = createUser(userRep);
    UserResource user = realm.users().get(id);
    List<String> actions = new LinkedList<>();
    try {
        user.executeActionsEmail(actions);
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User email missing", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
    try {
        userRep = user.toRepresentation();
        userRep.setEmail("user1@localhost");
        userRep.setEnabled(false);
        updateUser(user, userRep);
        user.executeActionsEmail(actions);
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User is disabled", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
    try {
        userRep.setEnabled(true);
        updateUser(user, userRep);
        user.executeActionsEmail("invalidClientId", "invalidUri", actions);
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("Client doesn't exist", error.getErrorMessage());
        assertAdminEvents.assertEmpty();
    }
}
Also used : ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserResource(org.keycloak.admin.client.resource.UserResource) ClientErrorException(javax.ws.rs.ClientErrorException) LinkedList(java.util.LinkedList) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

ErrorRepresentation (org.keycloak.representations.idm.ErrorRepresentation)21 Test (org.junit.Test)14 Response (javax.ws.rs.core.Response)11 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)11 ClientErrorException (javax.ws.rs.ClientErrorException)7 UserResource (org.keycloak.admin.client.resource.UserResource)5 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)5 LinkedList (java.util.LinkedList)4 IOException (java.io.IOException)3 List (java.util.List)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Charset (java.nio.charset.Charset)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2