Beispiel #1
0
  @Override
  public void setSingleAttribute(String name, String value) {
    boolean found = false;
    List<UserAttributeEntity> toRemove = new ArrayList<>();
    for (UserAttributeEntity attr : user.getAttributes()) {
      if (attr.getName().equals(name)) {
        if (!found) {
          attr.setValue(value);
          found = true;
        } else {
          toRemove.add(attr);
        }
      }
    }

    for (UserAttributeEntity attr : toRemove) {
      em.remove(attr);
      user.getAttributes().remove(attr);
    }

    if (found) {
      return;
    }

    persistAttributeValue(name, value);
  }
Beispiel #2
0
 @Override
 public void addRequiredAction(RequiredAction action) {
   for (UserRequiredActionEntity attr : user.getRequiredActions()) {
     if (attr.getAction().equals(action)) {
       return;
     }
   }
   UserRequiredActionEntity attr = new UserRequiredActionEntity();
   attr.setAction(action);
   attr.setUser(user);
   em.persist(attr);
   user.getRequiredActions().add(attr);
 }
Beispiel #3
0
 @Override
 public void setAttribute(String name, String value) {
   for (UserAttributeEntity attr : user.getAttributes()) {
     if (attr.getName().equals(name)) {
       attr.setValue(value);
       return;
     }
   }
   UserAttributeEntity attr = new UserAttributeEntity();
   attr.setName(name);
   attr.setValue(value);
   attr.setUser(user);
   em.persist(attr);
   user.getAttributes().add(attr);
 }
Beispiel #4
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();
  }
Beispiel #5
0
  private List<CredentialEntity> getCredentialEntities(UserEntity userEntity, String credType) {
    List<CredentialEntity> credentialEntities = new ArrayList<CredentialEntity>();
    for (CredentialEntity entity : userEntity.getCredentials()) {
      if (entity.getType().equals(credType)) {
        credentialEntities.add(entity);
      }
    }

    // Avoiding direct use of credSecond.getCreatedDate() - credFirst.getCreatedDate() to prevent
    // Integer Overflow
    // Orders from most recent to least recent
    Collections.sort(
        credentialEntities,
        new Comparator<CredentialEntity>() {
          public int compare(CredentialEntity credFirst, CredentialEntity credSecond) {
            if (credFirst.getCreatedDate() > credSecond.getCreatedDate()) {
              return -1;
            } else if (credFirst.getCreatedDate() < credSecond.getCreatedDate()) {
              return 1;
            } else {
              return 0;
            }
          }
        });
    return credentialEntities;
  }
Beispiel #6
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();
  }
Beispiel #7
0
 @Override
 public Set<RequiredAction> getRequiredActions() {
   Set<RequiredAction> result = new HashSet<RequiredAction>();
   for (UserRequiredActionEntity attr : user.getRequiredActions()) {
     result.add(attr.getAction());
   }
   return result;
 }
Beispiel #8
0
 @Override
 public Map<String, List<String>> getAttributes() {
   MultivaluedHashMap<String, String> result = new MultivaluedHashMap<>();
   for (UserAttributeEntity attr : user.getAttributes()) {
     result.add(attr.getName(), attr.getValue());
   }
   return result;
 }
Beispiel #9
0
 @Override
 public Map<String, String> getAttributes() {
   Map<String, String> result = new HashMap<String, String>();
   for (UserAttributeEntity attr : user.getAttributes()) {
     result.put(attr.getName(), attr.getValue());
   }
   return result;
 }
Beispiel #10
0
 @Override
 public String getAttribute(String name) {
   for (UserAttributeEntity attr : user.getAttributes()) {
     if (attr.getName().equals(name)) {
       return attr.getValue();
     }
   }
   return null;
 }
Beispiel #11
0
 private void persistAttributeValue(String name, String value) {
   UserAttributeEntity attr = new UserAttributeEntity();
   attr.setId(KeycloakModelUtils.generateId());
   attr.setName(name);
   attr.setValue(value);
   attr.setUser(user);
   em.persist(attr);
   user.getAttributes().add(attr);
 }
Beispiel #12
0
  private CredentialEntity getCredentialEntity(UserEntity userEntity, String credType) {
    for (CredentialEntity entity : userEntity.getCredentials()) {
      if (entity.getType().equals(credType)) {
        return entity;
      }
    }

    return null;
  }
Beispiel #13
0
 @Override
 public List<String> getAttribute(String name) {
   List<String> result = new ArrayList<>();
   for (UserAttributeEntity attr : user.getAttributes()) {
     if (attr.getName().equals(name)) {
       result.add(attr.getValue());
     }
   }
   return result;
 }
Beispiel #14
0
 @Override
 public void removeAttribute(String name) {
   Iterator<UserAttributeEntity> it = user.getAttributes().iterator();
   while (it.hasNext()) {
     UserAttributeEntity attr = it.next();
     if (attr.getName().equals(name)) {
       it.remove();
       em.remove(attr);
     }
   }
 }
Beispiel #15
0
 @Override
 public void removeRequiredAction(RequiredAction action) {
   Iterator<UserRequiredActionEntity> it = user.getRequiredActions().iterator();
   while (it.hasNext()) {
     UserRequiredActionEntity attr = it.next();
     if (attr.getAction().equals(action)) {
       it.remove();
       em.remove(attr);
     }
   }
 }
Beispiel #16
0
  @Override
  public void updateConsent(UserConsentModel consent) {
    String clientId = consent.getClient().getId();

    UserConsentEntity consentEntity = getGrantedConsentEntity(clientId);
    if (consentEntity == null) {
      throw new ModelException(
          "Consent not found for client [" + clientId + "] and user [" + user.getId() + "]");
    }

    updateGrantedConsentEntity(consentEntity, consent);
  }
Beispiel #17
0
  @Override
  public void addConsent(UserConsentModel consent) {
    String clientId = consent.getClient().getId();

    UserConsentEntity consentEntity = getGrantedConsentEntity(clientId);
    if (consentEntity != null) {
      throw new ModelDuplicateException(
          "Consent already exists for client [" + clientId + "] and user [" + user.getId() + "]");
    }

    consentEntity = new UserConsentEntity();
    consentEntity.setId(KeycloakModelUtils.generateId());
    consentEntity.setUser(user);
    consentEntity.setClientId(clientId);
    em.persist(consentEntity);
    em.flush();

    updateGrantedConsentEntity(consentEntity, consent);
  }
Beispiel #18
0
  @Override
  public List<UserCredentialValueModel> getCredentialsDirectly() {
    List<CredentialEntity> credentials = new ArrayList<CredentialEntity>(user.getCredentials());
    List<UserCredentialValueModel> result = new ArrayList<UserCredentialValueModel>();

    if (credentials != null) {
      for (CredentialEntity credEntity : credentials) {
        UserCredentialValueModel credModel = new UserCredentialValueModel();
        credModel.setType(credEntity.getType());
        credModel.setDevice(credEntity.getDevice());
        credModel.setValue(credEntity.getValue());
        credModel.setSalt(credEntity.getSalt());
        credModel.setHashIterations(credEntity.getHashIterations());

        result.add(credModel);
      }
    }

    return result;
  }
Beispiel #19
0
  @Override
  public void updateCredential(UserCredentialModel cred) {

    if (cred.getType().equals(UserCredentialModel.PASSWORD)) {
      updatePasswordCredential(cred);
    } else if (UserCredentialModel.isOtp(cred.getType())) {
      updateOtpCredential(cred);

    } else {
      CredentialEntity credentialEntity = getCredentialEntity(user, cred.getType());

      if (credentialEntity == null) {
        credentialEntity = setCredentials(user, cred);
        credentialEntity.setValue(cred.getValue());
        em.persist(credentialEntity);
        user.getCredentials().add(credentialEntity);
      } else {
        credentialEntity.setValue(cred.getValue());
      }
    }
    em.flush();
  }
Beispiel #20
0
  private void updateOtpCredential(UserCredentialModel cred) {
    CredentialEntity credentialEntity = getCredentialEntity(user, cred.getType());

    if (credentialEntity == null) {
      credentialEntity = setCredentials(user, cred);

      credentialEntity.setValue(cred.getValue());
      OTPPolicy otpPolicy = realm.getOTPPolicy();
      credentialEntity.setAlgorithm(otpPolicy.getAlgorithm());
      credentialEntity.setDigits(otpPolicy.getDigits());
      credentialEntity.setCounter(otpPolicy.getInitialCounter());
      credentialEntity.setPeriod(otpPolicy.getPeriod());
      em.persist(credentialEntity);
      user.getCredentials().add(credentialEntity);
    } else {
      OTPPolicy policy = realm.getOTPPolicy();
      credentialEntity.setDigits(policy.getDigits());
      credentialEntity.setCounter(policy.getInitialCounter());
      credentialEntity.setAlgorithm(policy.getAlgorithm());
      credentialEntity.setValue(cred.getValue());
      credentialEntity.setPeriod(policy.getPeriod());
    }
  }
Beispiel #21
0
  @Override
  public List<UserCredentialValueModel> getCredentialsDirectly() {
    List<CredentialEntity> credentials = new ArrayList<>(user.getCredentials());
    List<UserCredentialValueModel> result = new ArrayList<>();

    for (CredentialEntity credEntity : credentials) {
      UserCredentialValueModel credModel = new UserCredentialValueModel();
      credModel.setType(credEntity.getType());
      credModel.setDevice(credEntity.getDevice());
      credModel.setValue(credEntity.getValue());
      credModel.setCreatedDate(credEntity.getCreatedDate());
      credModel.setSalt(credEntity.getSalt());
      credModel.setHashIterations(credEntity.getHashIterations());
      credModel.setCounter(credEntity.getCounter());
      credModel.setAlgorithm(credEntity.getAlgorithm());
      credModel.setDigits(credEntity.getDigits());
      credModel.setPeriod(credEntity.getPeriod());

      result.add(credModel);
    }

    return result;
  }
Beispiel #22
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);
      }
    }
  }
Beispiel #23
0
 @Override
 public void setEnabled(boolean enabled) {
   user.setEnabled(enabled);
 }
Beispiel #24
0
 @Override
 public boolean isTotp() {
   return user.isTotp();
 }
Beispiel #25
0
 @Override
 public boolean isEnabled() {
   return user.isEnabled();
 }
Beispiel #26
0
 @Override
 public void setUsername(String username) {
   user.setUsername(username);
 }
Beispiel #27
0
 @Override
 public String getUsername() {
   return user.getUsername();
 }
Beispiel #28
0
 @Override
 public String getId() {
   return user.getId();
 }
Beispiel #29
0
 @Override
 public void setFederationLink(String link) {
   user.setFederationLink(link);
 }
Beispiel #30
0
 @Override
 public String getFederationLink() {
   return user.getFederationLink();
 }