use of org.wso2.carbon.identity.mgt.dto.UserChallengesDTO in project carbon-identity-framework by wso2.
the class ChallengeQuestionProcessor method getChallengeQuestionsOfUser.
/**
* // TODO manage oder
*
* @param userName
* @param tenantId
* @param adminService
* @return
*/
public UserChallengesDTO[] getChallengeQuestionsOfUser(String userName, int tenantId, boolean adminService) {
List<UserChallengesDTO> challengesDTOs = new ArrayList<UserChallengesDTO>();
try {
if (log.isDebugEnabled()) {
log.debug("Retrieving Challenge question from the user profile.");
}
List<String> challengesUris = getChallengeQuestionUris(userName, tenantId);
for (int i = 0; i < challengesUris.size(); i++) {
String challengesUri = challengesUris.get(i).trim();
String challengeValue = Utils.getClaimFromUserStoreManager(userName, tenantId, challengesUri);
String[] challengeValues = challengeValue.split(IdentityMgtConfig.getInstance().getChallengeQuestionSeparator());
if (challengeValues != null && challengeValues.length == 2) {
UserChallengesDTO dto = new UserChallengesDTO();
dto.setId(challengesUri);
dto.setQuestion(challengeValues[0].trim());
if (adminService) {
dto.setAnswer(challengeValues[1].trim());
}
dto.setOrder(i);
dto.setPrimary(false);
challengesDTOs.add(dto);
}
}
} catch (Exception e) {
String msg = "No associated challenge question found for the user";
if (log.isDebugEnabled()) {
log.debug(msg, e);
}
}
if (!challengesDTOs.isEmpty()) {
return challengesDTOs.toArray(new UserChallengesDTO[challengesDTOs.size()]);
} else {
return new UserChallengesDTO[0];
}
}
use of org.wso2.carbon.identity.mgt.dto.UserChallengesDTO in project carbon-identity-framework by wso2.
the class ChallengeQuestionProcessor method getUserChallengeQuestions.
/**
* Retrieves challenge questions associated with the user.
*
* @param userName username of the user
* @param tenantId tenant user belongs to
* @return an array of UserChallengesDTO instances which holds the challenge questions
* @throws IdentityException
*/
public UserChallengesDTO[] getUserChallengeQuestions(String userName, int tenantId) throws IdentityException {
List<UserChallengesDTO> userChallengesDTOList = new ArrayList<>();
try {
if (log.isDebugEnabled()) {
log.debug("Retrieving Challenge questions from the user profile.");
}
List<String> challengeQuestionUris = getChallengeQuestionUris(userName, tenantId);
String[] challengeQuestionUriArray = new String[challengeQuestionUris.size()];
Map<String, String> challengeQuestionClaimValues = Utils.getClaimsFromUserStoreManager(userName, tenantId, challengeQuestionUris.toArray(challengeQuestionUriArray));
for (Map.Entry<String, String> challengeQuestionClaimValue : challengeQuestionClaimValues.entrySet()) {
String[] challengeQuestionItems = challengeQuestionClaimValue.getValue().split(IdentityMgtConfig.getInstance().getChallengeQuestionSeparator());
UserChallengesDTO dto = new UserChallengesDTO();
dto.setId(challengeQuestionClaimValue.getKey());
dto.setQuestion(challengeQuestionItems[0]);
userChallengesDTOList.add(dto);
}
} catch (Exception e) {
throw IdentityException.error("No associated challenge questions found for the user.", e);
}
UserChallengesDTO[] userChallengesDTOs = new UserChallengesDTO[userChallengesDTOList.size()];
return userChallengesDTOList.toArray(userChallengesDTOs);
}
use of org.wso2.carbon.identity.mgt.dto.UserChallengesDTO in project carbon-identity-framework by wso2.
the class ChallengeQuestionProcessor method setChallengesOfUser.
/**
* @param userName
* @param tenantId
* @param challengesDTOs
* @throws IdentityException
*/
public void setChallengesOfUser(String userName, int tenantId, UserChallengesDTO[] challengesDTOs) throws IdentityException {
try {
if (log.isDebugEnabled()) {
log.debug("Challenge Question from the user profile.");
}
List<String> challengesUris = new ArrayList<String>();
String challengesUrisValue = "";
String separator = IdentityMgtConfig.getInstance().getChallengeQuestionSeparator();
Map<String, String> oldClaims = new HashMap<String, String>();
Map<String, String> newClaims = new HashMap<String, String>();
String[] requestclaims = new String[challengesDTOs.length];
int x = 0;
for (UserChallengesDTO claimDto : challengesDTOs) {
requestclaims[x++] = claimDto.getId();
}
// Getting user store manager here to reduce the calls for claim retrieval.
// TODO need to put into a new method in a new release version. Used to avoid API changes in patch.
org.wso2.carbon.user.core.UserStoreManager userStoreManager = null;
RealmService realmService = IdentityMgtServiceComponent.getRealmService();
try {
if (realmService.getTenantUserRealm(tenantId) != null) {
userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) realmService.getTenantUserRealm(tenantId).getUserStoreManager();
}
} catch (Exception e) {
String msg = "Error retrieving the user store manager for the tenant";
log.error(msg, e);
throw IdentityException.error(msg, e);
}
if (userStoreManager != null) {
oldClaims = userStoreManager.getUserClaimValues(userName, requestclaims, null);
}
if (!ArrayUtils.isEmpty(challengesDTOs)) {
for (UserChallengesDTO dto : challengesDTOs) {
if (dto.getId() != null && dto.getQuestion() != null && dto.getAnswer() != null) {
String oldClaimValue = oldClaims.get(dto.getId());
if ((oldClaimValue != null) && oldClaimValue.contains(separator)) {
String oldAnswer = oldClaimValue.split(separator)[1];
if (!oldAnswer.trim().equals(dto.getAnswer().trim())) {
String claimValue = dto.getQuestion().trim() + separator + Utils.doHash(dto.getAnswer().trim().toLowerCase());
if (!oldClaimValue.equals(claimValue)) {
newClaims.put(dto.getId().trim(), claimValue);
}
}
} else {
String claimValue = dto.getQuestion().trim() + separator + Utils.doHash(dto.getAnswer().trim().toLowerCase());
newClaims.put(dto.getId().trim(), claimValue);
}
challengesUris.add(dto.getId().trim());
}
}
for (String challengesUri : challengesUris) {
if ("".equals(challengesUrisValue)) {
challengesUrisValue = challengesUri;
} else {
challengesUrisValue = challengesUrisValue + IdentityMgtConfig.getInstance().getChallengeQuestionSeparator() + challengesUri;
}
}
newClaims.put("http://wso2.org/claims/challengeQuestionUris", challengesUrisValue);
// Single call to save all challenge questions.
userStoreManager.setUserClaimValues(userName, newClaims, UserCoreConstants.DEFAULT_PROFILE);
}
} catch (org.wso2.carbon.user.api.UserStoreException e) {
String msg = "No associated challenge question found for the user";
throw IdentityException.error(msg, e);
}
}
use of org.wso2.carbon.identity.mgt.dto.UserChallengesDTO in project carbon-identity-framework by wso2.
the class ChallengeQuestionProcessor method verifyPrimaryChallengeQuestion.
/**
* @param userName
* @param tenantId
* @param userChallengesDTOs
* @return
* @throws UserStoreException
*/
public boolean verifyPrimaryChallengeQuestion(String userName, int tenantId, UserChallengesDTO[] userChallengesDTOs) {
boolean verification = false;
try {
if (log.isDebugEnabled()) {
log.debug("Challenge Question from the user profile for user " + userName);
}
String claimValue = Utils.getClaimFromUserStoreManager(userName, tenantId, "http://wso2.org/claims/primaryChallengeQuestion");
if (claimValue == null) {
log.debug("No associated challenge question found for the user " + userName);
return false;
}
String[] challenges = claimValue.split(IdentityMgtConfig.getInstance().getChallengeQuestionSeparator());
Map<String, String> challengeMap = new HashMap<String, String>();
for (int i = 0; i < challenges.length; i = i + 2) {
challengeMap.put(challenges[i], challenges[i + 1]);
}
for (UserChallengesDTO userChallengesDTO : userChallengesDTOs) {
for (Map.Entry<String, String> entry : challengeMap.entrySet()) {
String challengeQuestion = entry.getKey();
if (challengeQuestion.equals(userChallengesDTO.getQuestion().trim())) {
String challengeAnswer = entry.getValue();
if (challengeAnswer.equals(Utils.doHash(userChallengesDTO.getAnswer().trim().toLowerCase()))) {
verification = true;
} else {
return false;
}
}
}
}
} catch (Exception e) {
log.debug("No associated challenge question found for the user " + userName, e);
}
return verification;
}
use of org.wso2.carbon.identity.mgt.dto.UserChallengesDTO in project carbon-identity-framework by wso2.
the class ChallengeQuestionProcessor method getUserChallengeQuestion.
public UserChallengesDTO getUserChallengeQuestion(String userName, int tenantId, String challengesUri) throws IdentityMgtServiceException {
UserChallengesDTO dto = null;
try {
if (log.isDebugEnabled()) {
log.debug("Retrieving Challenge question from the user profile.");
}
String challengeValue = Utils.getClaimFromUserStoreManager(userName, tenantId, challengesUri);
if (challengeValue != null) {
String[] challengeValues = challengeValue.split(IdentityMgtConfig.getInstance().getChallengeQuestionSeparator());
if (challengeValues != null && challengeValues.length == 2) {
dto = new UserChallengesDTO();
dto.setId(challengesUri);
dto.setQuestion(challengeValues[0].trim());
}
} else {
dto = new UserChallengesDTO();
dto.setError("Challenge questions have not been answered by the user: " + userName);
}
} catch (Exception e) {
String errorMsg = "Error while getting the challenge questions for the user: " + userName;
if (log.isDebugEnabled()) {
log.debug(errorMsg, e);
}
dto = new UserChallengesDTO();
dto.setError(errorMsg);
throw new IdentityMgtServiceException(errorMsg, e);
}
return dto;
}
Aggregations