@Override public void updateUserProfile(String uniqueUserId, Map<String, String> updatedClaimsMap) throws UserPortalUIException { if (updatedClaimsMap == null || updatedClaimsMap.isEmpty()) { return; } List<Claim> updatedClaims = updatedClaimsMap.entrySet().stream() .filter(entry -> !StringUtils.isNullOrEmpty(entry.getKey())) .map(entry -> new Claim(IdentityMgtConstants.CLAIM_ROOT_DIALECT, entry.getKey(), entry.getValue())) .collect(Collectors.toList()); try { getRealmService().getIdentityStore().updateUserClaims(uniqueUserId, updatedClaims, null); } catch (IdentityStoreException | UserNotFoundException e) { String error = "Failed to updated user profile."; LOGGER.error(error, e); throw new UserPortalUIException(error); } }
@Override public String getPrimaryDomainName() throws UserPortalUIException { String primaryDomain; try { primaryDomain = getRealmService().getIdentityStore().getPrimaryDomainName(); } catch (IdentityStoreException e) { String error = "Failed to get the primary domain name."; LOGGER.error(error, e); throw new UserPortalUIException(error); } return primaryDomain; }
@Override public Set<String> getDomainNames() throws UserPortalUIException { Set<String> domainSet; try { domainSet = getRealmService().getIdentityStore().getDomainNames(); } catch (IdentityStoreException e) { String error = "Failed to get the domain names."; LOGGER.error(error, e); throw new UserPortalUIException(error); } return domainSet; }
/** * Add/Update multiple claims of a user. * * @param uniqueUserId Unique ID of the user. * @param claims Map of claim URIs and values to be updated * @param claimDialect Claim dialect of the claim URI. If dialect is null, root dialect will be used. * @throws IdentityStoreException If error occurs while updating the claim. * @throws UserNotFoundException If the user does not exist. */ public static void setClaimsInIdentityStore(String uniqueUserId, Map<String, String> claims, @Nullable String claimDialect) throws IdentityStoreException, UserNotFoundException { RealmService realmService = IdentityMgtDataHolder.getInstance().getRealmService(); IdentityStore identityStore = realmService.getIdentityStore(); if (identityStore == null) { throw new IdentityRuntimeException("Error while obtaining the identity store. Null reference to Identity " + "store."); } if (claimDialect == null) { claimDialect = IdentityMgtConstants.CLAIM_ROOT_DIALECT; } String dialect = claimDialect; List<Claim> claimsList = claims.entrySet().stream() .map((x) -> new Claim(dialect, x.getKey(), x.getValue())) .collect(Collectors.toList()); identityStore.updateUserClaims(uniqueUserId, claimsList, null); }
@Override public Map<String, String> isUserExist(Map<String, String> userClaims) throws UserPortalUIException { List<Claim> claimsList = new ArrayList<>(); Map<String, String> userExistsMeta; for (Map.Entry<String, String> entry : userClaims.entrySet()) { Claim claim = new Claim(); claim.setClaimUri(entry.getKey()); claim.setValue(entry.getValue()); claimsList.add(claim); } try { userExistsMeta = getRealmService().getIdentityStore().isUserExist(claimsList); } catch (IdentityStoreException e) { String error = "Error while checking whether the user exists."; LOGGER.error(error, e); throw new UserPortalUIException(error); } return userExistsMeta; }
@Override public void updatePassword(String username, char[] oldPassword, char[] newPassword, String domain) throws UserNotFoundException, UserPortalUIException { try { //validate the old password UUFUser uufUser = authenticate(username, oldPassword, domain); PasswordCallback passwordCallback = new PasswordCallback("password", false); passwordCallback.setPassword(newPassword); getRealmService().getIdentityStore().updateUserCredentials(uufUser.getUserId(), Collections.singletonList(passwordCallback)); } catch (IdentityStoreException e) { String error = "Failed to update user password."; LOGGER.error(error, e); throw new UserPortalUIException(error); } }
@Override public boolean isUserExist(Map<String, String> userClaims, String domain) throws UserPortalUIException { List<Claim> claimsList = new ArrayList<>(); boolean isUserExists; for (Map.Entry<String, String> entry : userClaims.entrySet()) { Claim claim = new Claim(); claim.setClaimUri(entry.getKey()); claim.setValue(entry.getValue()); claimsList.add(claim); } try { isUserExists = getRealmService().getIdentityStore().isUserExist(claimsList, domain); } catch (IdentityStoreException e) { String error = "Error while checking whether the user exists."; LOGGER.error(error, e); throw new UserPortalUIException(error); } return isUserExists; }
User user = realmService.getIdentityStore().getUser(userUniqueId);
@Override public List<Claim> getClaimsOfUser(String uniqueUserId, List<MetaClaim> metaClaims) throws UserPortalUIException { List<Claim> claimList = null; if (StringUtils.isNullOrEmpty(uniqueUserId)) { throw new UserPortalUIException("Invalid unique user id."); } if (metaClaims != null && !metaClaims.isEmpty()) { try { claimList = getRealmService().getIdentityStore().getClaimsOfUser(uniqueUserId, metaClaims); } catch (IdentityStoreException | UserNotFoundException e) { String error = "Failed to get claims of the user."; LOGGER.error(error, e); throw new UserPortalUIException(error); } } else { claimList = Collections.emptyList(); } return claimList; }
@Override public List<ChallengeQuestionSetEntry> getChallengeQuestionList(String userUniqueId) throws IdentityRecoveryException, IdentityStoreException, UserNotFoundException { List<ChallengeQuestionSetEntry> challengeQuestionSetEntryList = new ArrayList<ChallengeQuestionSetEntry>(); if (challengeQuestionManager == null || realmService == null) { throw new IdentityRecoveryException("Challenge question manager or Realm service is not available."); } User user = realmService.getIdentityStore().getUser(userUniqueId); List<ChallengeQuestion> challengeQuestions = challengeQuestionManager.getAllChallengeQuestionsForUser(user); Map<String, List<ChallengeQuestion>> groupedChallengeQuestionMap = challengeQuestions.stream() .collect(Collectors .groupingBy(ChallengeQuestion::getQuestionSetId)); for (Map.Entry<String, List<ChallengeQuestion>> entry : groupedChallengeQuestionMap.entrySet()) { ChallengeQuestionSetEntry challengeQuestionSetEntry = new ChallengeQuestionSetEntry(); challengeQuestionSetEntry.setChallengeQuestionSetId(encodeChallengeQuestionSetId(entry.getKey())); List<ChallengeQuestion> encodedSetIdChallengeQuestionsList = entry.getValue().stream(). map(challengeQuestion -> { challengeQuestion.setQuestionSetId(encodeChallengeQuestionSetId(challengeQuestion .getQuestionSetId())); return challengeQuestion; }).collect(Collectors.toList()); challengeQuestionSetEntry.setChallengeQuestionList(encodedSetIdChallengeQuestionsList); challengeQuestionSetEntryList.add(challengeQuestionSetEntry); } return challengeQuestionSetEntryList; }
public UserManager getUserManager() throws IdentityStoreException { CarbonUserManager carbonUserManager = null; RealmService realmService = (RealmService) IdentitySCIMDataHolder.getInstance().getRealmService(); if (realmService != null) { carbonUserManager = new CarbonUserManager(realmService.getIdentityStore()); return carbonUserManager; } else { String error = "Can not obtain carbon realm service.."; throw new IdentityStoreException(error); } }
/** * Add/Update a claim of a user. * * @param uniqueUserId Unique ID of the user. * @param claimUri Claim URI to be added or updated. * @param value Value of the claim. * @param claimDialect Claim dialect of the claim URI. If dialect is null, root dialect will be used. * @throws IdentityStoreException If error occurs while updating the claim. * @throws UserNotFoundException If the user does not exist. */ public static void setClaimInIdentityStore(String uniqueUserId, String claimUri, String value, @Nullable String claimDialect) throws IdentityStoreException, UserNotFoundException { RealmService realmService = IdentityMgtDataHolder.getInstance().getRealmService(); IdentityStore identityStore = realmService.getIdentityStore(); if (identityStore == null) { throw new IdentityRuntimeException("Error while obtaining the identity store. Null reference to Identity " + "store."); } if (claimDialect == null) { claimDialect = IdentityMgtConstants.CLAIM_ROOT_DIALECT; } List<Claim> claimsList = new ArrayList<>(1); claimsList.add(new Claim(claimDialect, claimUri, value)); identityStore.updateUserClaims(uniqueUserId, claimsList, null); }
@Override public void deleteChallengeQuestionForUser(String userUniqueId, String questionId, String questionSetId) throws IdentityRecoveryException, IdentityStoreException, UserNotFoundException, UserPortalUIException { if (challengeQuestionManager == null || realmService == null) { throw new IdentityRecoveryException("Challenge question manager or Realm service is not available."); } int minNumOfSecurityQuestions = challengeQuestionManager.getMinimumNoOfChallengeQuestionsToAnswer(); User user = realmService.getIdentityStore().getUser(userUniqueId); List<UserChallengeAnswer> existingAnswers = challengeQuestionManager.getChallengeAnswersOfUser(userUniqueId); if (minNumOfSecurityQuestions < existingAnswers.size()) { existingAnswers.removeIf(answer -> StringUtils.equals(answer.getQuestion().getQuestionId(), questionId) && StringUtils.equals(answer.getQuestion().getQuestionSetId(), new String(Base64.getDecoder().decode(questionSetId.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8))); challengeQuestionManager.setChallengesOfUser(user, existingAnswers); } else { String error = "Cannot delete security question. You need to have at least" + minNumOfSecurityQuestions + "security questions"; throw new UserPortalUIException(error); } }
private static List<User> getUserList(List<Claim> claims, int offset, int length) throws IdentityRecoveryException { RealmService realmService = IdentityRecoveryServiceDataHolder.getInstance().getRealmService(); IdentityStore identityStore = realmService.getIdentityStore(); try { return identityStore.listUsers(claims, offset, length); } catch (IdentityStoreException e) { String msg = "Unable to retrieve the user list from claim."; throw new IdentityRecoveryException(msg, e); } } }
claims = getRealmService().getIdentityStore().getClaimsOfUser(uniqueUserId, metaClaims); } catch (IdentityStoreException e) { LOGGER.error(String.format("Failed to get the user claims for user - %s", uniqueUserId), e);
@Override public UUFUser addUser(Map<String, String> userClaims, Map<String, String> credentials) throws UserPortalUIException { UserBean userBean = new UserBean(); List<Claim> claimsList = new ArrayList<>(); List<Callback> credentialsList = new ArrayList<>(); User identityUser; for (Map.Entry<String, String> credential : credentials.entrySet()) { PasswordCallback passwordCallback = new PasswordCallback("password", false); passwordCallback.setPassword(credential.getValue().toCharArray()); credentialsList.add(passwordCallback); } for (Map.Entry<String, String> entry : userClaims.entrySet()) { Claim claim = new Claim(); claim.setClaimUri(entry.getKey()); claim.setValue(entry.getValue()); claimsList.add(claim); } userBean.setClaims(claimsList); userBean.setCredentials(credentialsList); try { identityUser = getRealmService().getIdentityStore().addUser(userBean); } catch (IdentityStoreException e) { String error = "Error while adding user."; LOGGER.error(error, e); throw new UserPortalUIException(error); } return new UUFUser(null, identityUser.getUniqueUserId(), identityUser.getDomainName()); }
@Override public UUFUser addUser(Map<String, String> userClaims, Map<String, String> credentials, String domainName) throws UserPortalUIException { UserBean userBean = new UserBean(); List<Claim> claimsList = new ArrayList<>(); List<Callback> credentialsList = new ArrayList<>(); User identityUser; for (Map.Entry<String, String> credential : credentials.entrySet()) { PasswordCallback passwordCallback = new PasswordCallback("password", false); passwordCallback.setPassword(credential.getValue().toCharArray()); credentialsList.add(passwordCallback); } for (Map.Entry<String, String> entry : userClaims.entrySet()) { Claim claim = new Claim(); claim.setClaimUri(entry.getKey()); claim.setValue(entry.getValue()); claimsList.add(claim); } userBean.setClaims(claimsList); userBean.setCredentials(credentialsList); try { identityUser = getRealmService().getIdentityStore().addUser(userBean, domainName); } catch (IdentityStoreException e) { String error = "Error while adding user."; LOGGER.error(error, e); throw new UserPortalUIException(error); } return new UUFUser(null, identityUser.getUniqueUserId(), identityUser.getDomainName()); }
IdentityStore identityStore = realmService.getIdentityStore();
/** * Get user claim value from identity store manager * * @param uniqueUserID * @param claimuri * @return * @throws IdentityStoreException * @throws UserNotFoundException */ public static String getClaimFromIdentityStore(String uniqueUserID, String claimuri) throws IdentityStoreException, UserNotFoundException { RealmService realmService = IdentityRecoveryServiceDataHolder.getInstance().getRealmService(); IdentityStore identityStore = realmService.getIdentityStore(); String claimValue = ""; if (identityStore != null) { List<Claim> claimsList = identityStore.getClaimsOfUser(uniqueUserID); if (claimsList != null && !claimsList.isEmpty()) { for (Claim claim : claimsList) { if (claim.getClaimUri().equals(claimuri)) { claimValue = claim.getValue(); break; } } } } return claimValue; }
@Override public UUFUser authenticate(String username, char[] password, String domain) throws UserPortalUIException { try { //TODO if different claim is used, need identify that claim. Claim usernameClaim = new Claim(IdentityMgtConstants.CLAIM_ROOT_DIALECT, IdentityMgtConstants.USERNAME_CLAIM, username); PasswordCallback passwordCallback = new PasswordCallback("password", false); passwordCallback.setPassword(password); AuthenticationContext authenticationContext = getRealmService().getIdentityStore() .authenticate(usernameClaim, new Callback[]{passwordCallback}, domain); User identityUser = authenticationContext.getUser(); //TODO if another claim used, need to load username claim return new UUFUser(username, identityUser.getUniqueUserId(), identityUser.getDomainName()); } catch (AuthenticationFailure e) { String error = "Invalid credentials."; if (LOGGER.isDebugEnabled()) { LOGGER.debug(error, e); } throw new UserPortalUIException("Invalid credentials."); } catch (IdentityStoreException e) { String error = "Failed to authenticate user."; LOGGER.error(error, e); throw new UserPortalUIException(error); } }