Java Code Examples for org.keycloak.representations.idm.CredentialRepresentation#setTemporary()

The following examples show how to use org.keycloak.representations.idm.CredentialRepresentation#setTemporary() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeycloakRealmResourceManager.java    From quarkus with Apache License 2.0 7 votes vote down vote up
private static UserRepresentation createUser(String username, String... realmRoles) {
    UserRepresentation user = new UserRepresentation();

    user.setUsername(username);
    user.setEnabled(true);
    user.setCredentials(new ArrayList<>());
    user.setRealmRoles(Arrays.asList(realmRoles));

    CredentialRepresentation credential = new CredentialRepresentation();

    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue(username);
    credential.setTemporary(false);

    user.getCredentials().add(credential);

    return user;
}
 
Example 2
Source File: KeycloakRealmResourceManager.java    From quarkus with Apache License 2.0 7 votes vote down vote up
private static UserRepresentation createUser(String username, String... realmRoles) {
    UserRepresentation user = new UserRepresentation();

    user.setUsername(username);
    user.setEnabled(true);
    user.setCredentials(new ArrayList<>());
    user.setRealmRoles(Arrays.asList(realmRoles));

    CredentialRepresentation credential = new CredentialRepresentation();

    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue(username);
    credential.setTemporary(false);

    user.getCredentials().add(credential);

    return user;
}
 
Example 3
Source File: ApiUtil.java    From keycloak with Apache License 2.0 7 votes vote down vote up
public static void resetUserPassword(UserResource userResource, String newPassword, boolean temporary) {
    CredentialRepresentation newCredential = new CredentialRepresentation();
    newCredential.setType(PASSWORD);
    newCredential.setValue(newPassword);
    newCredential.setTemporary(temporary);
    userResource.resetPassword(newCredential);
}
 
Example 4
Source File: KeycloakRealmResourceManager.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static UserRepresentation createUser(String username, String... realmRoles) {
    UserRepresentation user = new UserRepresentation();

    user.setUsername(username);
    user.setEnabled(true);
    user.setCredentials(new ArrayList<>());
    user.setRealmRoles(Arrays.asList(realmRoles));
    user.setEmail(username + "@gmail.com");

    CredentialRepresentation credential = new CredentialRepresentation();

    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue(username);
    credential.setTemporary(false);

    user.getCredentials().add(credential);

    return user;
}
 
Example 5
Source File: BackwardsCompatibilityUserStorageTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private String addUserAndResetPassword(String username, String password) {
    // Save user and assert he is saved in the new storage
    UserRepresentation user = new UserRepresentation();
    user.setEnabled(true);
    user.setUsername(username);
    Response response = testRealmResource().users().create(user);
    String userId = ApiUtil.getCreatedId(response);

    Assert.assertEquals(backwardsCompProviderId, new StorageId(userId).getProviderId());

    // Update his password
    CredentialRepresentation passwordRep = new CredentialRepresentation();
    passwordRep.setType(CredentialModel.PASSWORD);
    passwordRep.setValue(password);
    passwordRep.setTemporary(false);

    testRealmResource().users().get(userId).resetPassword(passwordRep);

    return userId;
}
 
Example 6
Source File: ServiceAccountTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void failManagePassword() {
    UserResource serviceAccount = adminClient.realm("test").users().get(userId);
    UserRepresentation representation = serviceAccount.toRepresentation();

    CredentialRepresentation password = new CredentialRepresentation();
    password.setValue("password");
    password.setType(CredentialRepresentation.PASSWORD);
    password.setTemporary(false);

    representation.setCredentials(Arrays.asList(password));

    this.expectedException.expect(Matchers.allOf(Matchers.instanceOf(ClientErrorException.class), 
            Matchers.hasProperty("response", Matchers.hasProperty("status", Matchers.is(400)))));
    this.expectedException.reportMissingExceptionWithMessage("Should fail, should not be possible to manage credentials for service accounts");

    serviceAccount.update(representation);
}
 
Example 7
Source File: UserTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void resetUserInvalidPassword() {
    String userId = createUser("user1", "user1@localhost");

    try {
        CredentialRepresentation cred = new CredentialRepresentation();
        cred.setType(CredentialRepresentation.PASSWORD);
        cred.setValue(" ");
        cred.setTemporary(false);
        realm.users().get(userId).resetPassword(cred);
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        e.getResponse().close();
        assertAdminEvents.assertEmpty();
    }
}
 
Example 8
Source File: UserTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void createUserWithTempolaryCredentials() {
    UserRepresentation user = new UserRepresentation();
    user.setUsername("user_temppw");
    user.setEmail("email.temppw@localhost");

    CredentialRepresentation password = new CredentialRepresentation();
    password.setValue("password");
    password.setType(CredentialRepresentation.PASSWORD);
    password.setTemporary(true);
    user.setCredentials(Arrays.asList(password));

    String userId = createUser(user);

    UserRepresentation userRep = realm.users().get(userId).toRepresentation();
    Assert.assertEquals(1, userRep.getRequiredActions().size());
    Assert.assertEquals(UserModel.RequiredAction.UPDATE_PASSWORD.toString(), userRep.getRequiredActions().get(0));
}
 
Example 9
Source File: UserTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
/**
 * See KEYCLOAK-11003
 */
@Test
public void createUserWithTemporaryPasswordWithAdditionalPasswordUpdateShouldRemoveUpdatePasswordRequiredAction() {

    String userId = createUser();

    CredentialRepresentation credTmp = new CredentialRepresentation();
    credTmp.setType(CredentialRepresentation.PASSWORD);
    credTmp.setValue("temp");
    credTmp.setTemporary(Boolean.TRUE);

    realm.users().get(userId).resetPassword(credTmp);

    CredentialRepresentation credPerm = new CredentialRepresentation();
    credPerm.setType(CredentialRepresentation.PASSWORD);
    credPerm.setValue("perm");
    credPerm.setTemporary(null);

    realm.users().get(userId).resetPassword(credPerm);

    UserRepresentation userRep = realm.users().get(userId).toRepresentation();

    Assert.assertFalse(userRep.getRequiredActions().contains(UserModel.RequiredAction.UPDATE_PASSWORD.name()));
}
 
Example 10
Source File: KeyCloakServiceImpl.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * This method will do the user password update.
 *
 * @param userId String
 * @param password String
 * @return boolean true/false
 */
@Override
public boolean doPasswordUpdate(String userId, String password) {
  boolean response = false;
  try {
    String fedUserId = getFederatedUserId(userId);
    UserResource resource =
        keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(fedUserId);
    CredentialRepresentation newCredential = new CredentialRepresentation();
    newCredential.setValue(password);
    newCredential.setType(CredentialRepresentation.PASSWORD);
    newCredential.setTemporary(true);
    resource.resetPassword(newCredential);
    response = true;
  } catch (Exception ex) {
    ProjectLogger.log(ex.getMessage(), ex);
  }
  return response;
}
 
Example 11
Source File: KeycloakTestResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static UserRepresentation createUser(String username, String... realmRoles) {
    UserRepresentation user = new UserRepresentation();

    user.setUsername(username);
    user.setEnabled(true);
    user.setCredentials(new ArrayList<>());
    user.setRealmRoles(Arrays.asList(realmRoles));

    CredentialRepresentation credential = new CredentialRepresentation();

    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue(username);
    credential.setTemporary(false);

    user.getCredentials().add(credential);

    return user;
}
 
Example 12
Source File: KeycloakDevModeRealmResourceManager.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static UserRepresentation createUser(String username, String... realmRoles) {
    UserRepresentation user = new UserRepresentation();

    user.setUsername(username);
    user.setEnabled(true);
    user.setCredentials(new ArrayList<>());
    user.setRealmRoles(Arrays.asList(realmRoles));

    CredentialRepresentation credential = new CredentialRepresentation();

    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue(username);
    credential.setTemporary(false);

    user.getCredentials().add(credential);

    return user;
}
 
Example 13
Source File: KeycloakTestResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static UserRepresentation createUser(String username, String... realmRoles) {
    UserRepresentation user = new UserRepresentation();

    user.setUsername(username);
    user.setEnabled(true);
    user.setCredentials(new ArrayList<>());
    user.setRealmRoles(Arrays.asList(realmRoles));

    CredentialRepresentation credential = new CredentialRepresentation();

    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue(username);
    credential.setTemporary(false);

    user.getCredentials().add(credential);

    return user;
}
 
Example 14
Source File: Users.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void setPasswordFor(UserRepresentation user, String password, boolean temporary) {
    List<CredentialRepresentation> credentials = new ArrayList<>();
    CredentialRepresentation pass = new CredentialRepresentation();
    pass.setType(PASSWORD);
    pass.setValue(password);
    pass.setTemporary(temporary);
    credentials.add(pass);
    user.setCredentials(credentials);
}
 
Example 15
Source File: PasswordHistoryPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public void resetUserPassword(UserResource userResource, String newPassword) {
    CredentialRepresentation newCredential = new CredentialRepresentation();
    newCredential.setType(PASSWORD);
    newCredential.setValue(newPassword);
    newCredential.setTemporary(false);
    userResource.resetPassword(newCredential);
}
 
Example 16
Source File: UserSetup.java    From keycloak-custom-protocol-mapper-example with Apache License 2.0 5 votes vote down vote up
private CredentialRepresentation createPassword(final String password) {
    CredentialRepresentation passwordCred = new CredentialRepresentation();
    passwordCred.setTemporary(false);
    passwordCred.setType(CredentialRepresentation.PASSWORD);
    passwordCred.setValue(password);
    return passwordCred;
}
 
Example 17
Source File: ManyUsersTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public UserRepresentation createUser(UsersResource users, UserRepresentation user) {
    // Add some additional attributes to user
    if (CREATE_OBJECTS) {
        Map<String, List<String>> attrs = new HashMap<>();
        attrs.put("attr1", Collections.singletonList("val1"));
        attrs.put("attr2", Collections.singletonList("val2"));
        user.setAttributes(attrs);
    }

    UserRepresentation userRep = super.createUser(users, user);

    // Add password
    if (CREATE_OBJECTS) {
        CredentialRepresentation password = new CredentialRepresentation();
        password.setType(CredentialRepresentation.PASSWORD);
        password.setValue("password");
        password.setTemporary(false);
        users.get(userRep.getId()).resetPassword(password);
    }

    // Add social link
    if (CREATE_SOCIAL_LINKS) {
        createSocialLink("facebook", users, userRep.getId());
    }

    return userRep;
}
 
Example 18
Source File: TestsHelper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static boolean createTestUser(String username, String password, String realmName) throws IOException {

        Keycloak keycloak = Keycloak.getInstance(
                keycloakBaseUrl,
                "master",
                username,
                password,
                "admin-cli");
        UserRepresentation userRepresentation = new UserRepresentation();
        userRepresentation.setUsername(username);
        userRepresentation.setEnabled(Boolean.TRUE);
        Response response = keycloak.realms().realm(realmName).users().create(userRepresentation);
        String userId = getCreatedId(response);
        response.close();
        CredentialRepresentation rep = new CredentialRepresentation();
        rep.setType(CredentialRepresentation.PASSWORD);
        rep.setValue(password);
        rep.setTemporary(false);
        keycloak.realms().realm(realmName).users().get(userId).resetPassword(rep);
        //add roles
        RoleRepresentation representation = new RoleRepresentation();
        representation.setName("user");

        keycloak.realms().realm(realmName).roles().create(representation);
        RoleRepresentation realmRole =  keycloak.realms().realm(realmName).roles().get("user").toRepresentation();
        keycloak.realms().realm(realmName).users().get(userId).roles().realmLevel().add(Arrays.asList(realmRole));
        return true;

    }
 
Example 19
Source File: FluentTestsHelper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test user.
 *
 * @param username A username to be created.
 * @param password A password for a user.
 * @return <code>this</code>
 */
public FluentTestsHelper createTestUser(String username, String password) {
    UserRepresentation userRepresentation = new UserRepresentation();
    userRepresentation.setUsername(username);
    userRepresentation.setEnabled(true);
    Response response = keycloak.realms().realm(testRealm).users().create(userRepresentation);
    String userId = getCreatedId(response);
    response.close();
    CredentialRepresentation rep = new CredentialRepresentation();
    rep.setType(CredentialRepresentation.PASSWORD);
    rep.setValue(password);
    rep.setTemporary(false);
    keycloak.realms().realm(testRealm).users().get(userId).resetPassword(rep);
    return this;
}
 
Example 20
Source File: UserTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void resetUserPassword() {
    String userId = createUser("user1", "user1@localhost");

    CredentialRepresentation cred = new CredentialRepresentation();
    cred.setType(CredentialRepresentation.PASSWORD);
    cred.setValue("password");
    cred.setTemporary(false);

    realm.users().get(userId).resetPassword(cred);
    assertAdminEvents.assertEvent(realmId, OperationType.ACTION, AdminEventPaths.userResetPasswordPath(userId), ResourceType.USER);

    String accountUrl = RealmsResource.accountUrl(UriBuilder.fromUri(getAuthServerRoot())).build(REALM_NAME).toString();

    driver.navigate().to(accountUrl);

    assertEquals("Log In", PageUtils.getPageTitle(driver));

    loginPage.login("user1", "password");

    assertTrue(driver.getTitle().contains("Account Management"));
}