@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); }
@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); }
@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); }
@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(); }
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; }
@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(); }
@Override public Set<RequiredAction> getRequiredActions() { Set<RequiredAction> result = new HashSet<RequiredAction>(); for (UserRequiredActionEntity attr : user.getRequiredActions()) { result.add(attr.getAction()); } return result; }
@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; }
@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; }
@Override public String getAttribute(String name) { for (UserAttributeEntity attr : user.getAttributes()) { if (attr.getName().equals(name)) { return attr.getValue(); } } return null; }
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); }
private CredentialEntity getCredentialEntity(UserEntity userEntity, String credType) { for (CredentialEntity entity : userEntity.getCredentials()) { if (entity.getType().equals(credType)) { return entity; } } return null; }
@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; }
@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); } } }
@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); } } }
@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); }
@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); }
@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; }
@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(); }
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()); } }
@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; }
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); } } }
@Override public void setEnabled(boolean enabled) { user.setEnabled(enabled); }
@Override public boolean isTotp() { return user.isTotp(); }
@Override public boolean isEnabled() { return user.isEnabled(); }
@Override public void setUsername(String username) { user.setUsername(username); }
@Override public String getUsername() { return user.getUsername(); }
@Override public String getId() { return user.getId(); }
@Override public void setFederationLink(String link) { user.setFederationLink(link); }
@Override public String getFederationLink() { return user.getFederationLink(); }