示例#1
0
  @Override
  public void updateCredential(UserCredentialModel cred) {
    CredentialEntity credentialEntity = getCredentialEntity(user, cred.getType());

    if (credentialEntity == null) {
      credentialEntity = new CredentialEntity();
      credentialEntity.setId(KeycloakModelUtils.generateId());
      credentialEntity.setType(cred.getType());
      credentialEntity.setDevice(cred.getDevice());
      credentialEntity.setUser(user);
      em.persist(credentialEntity);
      user.getCredentials().add(credentialEntity);
    }
    if (cred.getType().equals(UserCredentialModel.PASSWORD)) {
      byte[] salt = getSalt();
      int hashIterations = 1;
      PasswordPolicy policy = realm.getPasswordPolicy();
      if (policy != null) {
        hashIterations = policy.getHashIterations();
        if (hashIterations == -1) hashIterations = 1;
      }
      credentialEntity.setValue(
          new Pbkdf2PasswordEncoder(salt).encode(cred.getValue(), hashIterations));
      credentialEntity.setSalt(salt);
      credentialEntity.setHashIterations(hashIterations);
    } else {
      credentialEntity.setValue(cred.getValue());
    }
    credentialEntity.setDevice(cred.getDevice());
    em.flush();
  }
示例#2
0
  @Override
  public void updateCredentialDirectly(UserCredentialValueModel credModel) {
    CredentialEntity credentialEntity = getCredentialEntity(user, credModel.getType());

    if (credentialEntity == null) {
      credentialEntity = new CredentialEntity();
      credentialEntity.setId(KeycloakModelUtils.generateId());
      credentialEntity.setType(credModel.getType());
      credentialEntity.setCreatedDate(credModel.getCreatedDate());
      credentialEntity.setUser(user);
      em.persist(credentialEntity);
      user.getCredentials().add(credentialEntity);
    }

    credentialEntity.setValue(credModel.getValue());
    credentialEntity.setSalt(credModel.getSalt());
    credentialEntity.setDevice(credModel.getDevice());
    credentialEntity.setHashIterations(credModel.getHashIterations());
    credentialEntity.setCounter(credModel.getCounter());
    credentialEntity.setAlgorithm(credModel.getAlgorithm());
    credentialEntity.setDigits(credModel.getDigits());
    credentialEntity.setPeriod(credModel.getPeriod());

    em.flush();
  }
示例#3
0
 private CredentialEntity setCredentials(UserEntity user, UserCredentialModel cred) {
   CredentialEntity credentialEntity = new CredentialEntity();
   credentialEntity.setId(KeycloakModelUtils.generateId());
   credentialEntity.setType(cred.getType());
   credentialEntity.setDevice(cred.getDevice());
   credentialEntity.setUser(user);
   return credentialEntity;
 }
示例#4
0
  private void updatePasswordCredential(UserCredentialModel cred) {
    CredentialEntity credentialEntity = getCredentialEntity(user, cred.getType());

    if (credentialEntity == null) {
      credentialEntity = setCredentials(user, cred);
      setValue(credentialEntity, cred);
      em.persist(credentialEntity);
      user.getCredentials().add(credentialEntity);
    } else {

      int expiredPasswordsPolicyValue = -1;
      PasswordPolicy policy = realm.getPasswordPolicy();
      if (policy != null) {
        expiredPasswordsPolicyValue = policy.getExpiredPasswords();
      }

      if (expiredPasswordsPolicyValue != -1) {
        user.getCredentials().remove(credentialEntity);
        credentialEntity.setType(UserCredentialModel.PASSWORD_HISTORY);
        user.getCredentials().add(credentialEntity);

        List<CredentialEntity> credentialEntities =
            getCredentialEntities(user, UserCredentialModel.PASSWORD_HISTORY);
        if (credentialEntities.size() > expiredPasswordsPolicyValue - 1) {
          user.getCredentials()
              .removeAll(
                  credentialEntities.subList(
                      expiredPasswordsPolicyValue - 1, credentialEntities.size()));
        }

        credentialEntity = setCredentials(user, cred);
        setValue(credentialEntity, cred);
        em.persist(credentialEntity);
        user.getCredentials().add(credentialEntity);
      } else {
        List<CredentialEntity> credentialEntities =
            getCredentialEntities(user, UserCredentialModel.PASSWORD_HISTORY);
        if (credentialEntities != null && credentialEntities.size() > 0) {
          user.getCredentials().removeAll(credentialEntities);
        }
        setValue(credentialEntity, cred);
      }
    }
  }