Search in sources :

Example 11 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class IdentityProviderManager method getExpressionNodes.

/**
 * Get the filter node as a list.
 *
 * @param filter value of the filter.
 * @return node tree.
 * @throws IdentityProviderManagementClientException Error when validate filters.
 */
private List<ExpressionNode> getExpressionNodes(String filter) throws IdentityProviderManagementClientException {
    // Filter example : name sw "te" and name ew "st" and isEnabled eq "true".
    List<ExpressionNode> expressionNodes = new ArrayList<>();
    FilterTreeBuilder filterTreeBuilder;
    try {
        if (StringUtils.isNotBlank(filter)) {
            filterTreeBuilder = new FilterTreeBuilder(filter);
            Node rootNode = filterTreeBuilder.buildTree();
            setExpressionNodeList(rootNode, expressionNodes);
        }
    } catch (IOException | IdentityException e) {
        String message = "Error occurred while validate filter, filter: " + filter;
        throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message, e);
    }
    return expressionNodes;
}
Also used : FilterTreeBuilder(org.wso2.carbon.identity.core.model.FilterTreeBuilder) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) OperationNode(org.wso2.carbon.identity.core.model.OperationNode) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) Node(org.wso2.carbon.identity.core.model.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 12 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class UserInformationRecoveryService method getUserChallengeQuestion.

/**
 * To get the challenge question for the user.
 *
 * @param userName
 * @param confirmation
 * @param questionId   - Question id returned from the getUserChanllegneQuestionIds
 *                     method.
 * @return Populated question bean with the question details and the key.
 * @throws IdentityMgtServiceException
 */
public UserChallengesDTO getUserChallengeQuestion(String userName, String confirmation, String questionId) throws IdentityMgtServiceException {
    UserDTO userDTO = null;
    UserChallengesDTO userChallengesDTO = new UserChallengesDTO();
    if (log.isDebugEnabled()) {
        log.debug("User challenge question request received with username :" + userName);
    }
    try {
        userDTO = Utils.processUserId(userName);
    } catch (IdentityException e) {
        return handleChallengesError(VerificationBean.ERROR_CODE_INVALID_USER + " Error validating user : " + userName, null);
    }
    try {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            carbonContext.setTenantId(userDTO.getTenantId());
            carbonContext.setTenantDomain(userDTO.getTenantDomain());
        }
        RecoveryProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor();
        VerificationBean bean;
        try {
            bean = processor.verifyConfirmationCode(20, userDTO.getUserId(), confirmation);
            if (bean.isVerified()) {
                bean = processor.updateConfirmationCode(40, userDTO.getUserId(), userDTO.getTenantId());
            } else if (processor.verifyConfirmationCode(30, userDTO.getUserId(), confirmation).isVerified()) {
                bean = processor.updateConfirmationCode(40, userDTO.getUserId(), userDTO.getTenantId());
            } else {
                bean.setVerified(false);
            }
        } catch (IdentityException e) {
            userChallengesDTO = UserIdentityManagementUtil.getCustomErrorMessagesForChallengQuestions(e, userName);
            if (userChallengesDTO == null) {
                userChallengesDTO = handleChallengesError(VerificationBean.ERROR_CODE_INVALID_CODE + " Invalid confirmation code for user : " + userName, e);
            }
            return userChallengesDTO;
        }
        if (bean.isVerified()) {
            userChallengesDTO = processor.getQuestionProcessor().getUserChallengeQuestion(userDTO.getUserId(), userDTO.getTenantId(), questionId);
            userChallengesDTO.setKey(bean.getKey());
            userChallengesDTO.setVerfied(true);
            if (log.isDebugEnabled()) {
                log.debug("User challenge question retrieved successfully");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Verification failed for user. Error : " + bean.getError());
            }
            userChallengesDTO.setError(VerificationBean.ERROR_CODE_INVALID_USER + " " + bean.getError());
        }
    } finally {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return userChallengesDTO;
}
Also used : VerificationBean(org.wso2.carbon.identity.mgt.beans.VerificationBean) UserChallengesDTO(org.wso2.carbon.identity.mgt.dto.UserChallengesDTO) UserDTO(org.wso2.carbon.identity.mgt.dto.UserDTO) RecoveryProcessor(org.wso2.carbon.identity.mgt.RecoveryProcessor) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 13 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class JDBCUserRecoveryDataStore method store.

/**
 * Stores identity data set.
 *
 * @throws IdentityException
 */
@Override
@Deprecated
public void store(UserRecoveryDataDO[] recoveryDataDOs) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.STORE_META_DATA);
        for (UserRecoveryDataDO dataDO : recoveryDataDOs) {
            prepStmt.setString(1, dataDO.getUserName().toLowerCase());
            prepStmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
            prepStmt.setString(3, dataDO.getCode().toLowerCase());
            prepStmt.setString(4, dataDO.getSecret());
            prepStmt.setString(5, dataDO.getExpireTime());
            prepStmt.addBatch();
        }
        prepStmt.executeBatch();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw IdentityException.error("Error while storing user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
Also used : UserRecoveryDataDO(org.wso2.carbon.identity.mgt.dto.UserRecoveryDataDO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 14 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class JDBCUserRecoveryDataStore method load.

/**
 * This method should return only one result. An exception will be thrown if
 * duplicate entries found.
 * This can be used to check if the given metada exist in the database or to
 * check the validity.
 *
 * @return
 * @throws IdentityException
 */
/**
 * @param userName
 * @param tenantId
 * @return
 * @throws IdentityException
 */
@Override
@Deprecated
public UserRecoveryDataDO[] load(String userName, int tenantId) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement prepStmt = null;
    ResultSet results = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.LOAD_USER_METADATA);
        prepStmt.setString(1, userName.toLowerCase());
        prepStmt.setInt(2, IdentityTenantUtil.getTenantIdOfUser(userName));
        results = prepStmt.executeQuery();
        List<UserRecoveryDataDO> metada = new ArrayList<UserRecoveryDataDO>();
        while (results.next()) {
            metada.add(new UserRecoveryDataDO(results.getString(1), results.getInt(2), results.getString(3), results.getString(4)));
        }
        UserRecoveryDataDO[] resultMetadata = new UserRecoveryDataDO[metada.size()];
        return metada.toArray(resultMetadata);
    } catch (SQLException e) {
        throw IdentityException.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(results);
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
Also used : UserRecoveryDataDO(org.wso2.carbon.identity.mgt.dto.UserRecoveryDataDO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 15 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class RegistryRecoveryDataStore method invalidate.

@Override
public void invalidate(String code) throws IdentityException {
    Registry registry = null;
    try {
        registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
        registry.beginTransaction();
        String secretKeyPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_DATA + RegistryConstants.PATH_SEPARATOR + code.toLowerCase();
        if (registry.resourceExists(secretKeyPath)) {
            registry.delete(secretKeyPath);
        }
    } catch (RegistryException e) {
        log.error(e);
        throw IdentityException.error("Error while invalidating user recovery data for code : " + code);
    } finally {
        if (registry != null) {
            try {
                registry.commitTransaction();
            } catch (RegistryException e) {
                log.error("Error while processing registry transaction", e);
            }
        }
    }
}
Also used : Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

IdentityException (org.wso2.carbon.identity.base.IdentityException)132 UserStoreException (org.wso2.carbon.user.api.UserStoreException)62 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)38 Test (org.testng.annotations.Test)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)35 HashMap (java.util.HashMap)30 ArrayList (java.util.ArrayList)29 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)23 IdentityMgtServiceException (org.wso2.carbon.identity.mgt.IdentityMgtServiceException)22 Resource (org.wso2.carbon.registry.core.Resource)22 PreparedStatement (java.sql.PreparedStatement)21 UserDTO (org.wso2.carbon.identity.mgt.dto.UserDTO)20 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)19 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)18 VerificationBean (org.wso2.carbon.identity.mgt.beans.VerificationBean)18 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)17 DCRDataHolder (org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder)17 Map (java.util.Map)15 RecoveryProcessor (org.wso2.carbon.identity.mgt.RecoveryProcessor)15