@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 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 void setValue(CredentialEntity credentialEntity, UserCredentialModel cred) { UserCredentialValueModel encoded = PasswordHashManager.encode(session, realm, cred.getValue()); credentialEntity.setCreatedDate(Time.toMillis(Time.currentTime())); credentialEntity.setAlgorithm(encoded.getAlgorithm()); credentialEntity.setValue(encoded.getValue()); credentialEntity.setSalt(encoded.getSalt()); credentialEntity.setHashIterations(encoded.getHashIterations()); }
@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()); } }