Search in sources :

Example 1 with IdentityRuntimeException

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

the class WorkflowPendingUserAuthnHandler method validatePendingApproval.

/**
 * Validate whether the user account approval is pending.
 *
 * @param username Username.
 * @throws IdentityEventException If an error occurred while validating pending approval.
 */
private void validatePendingApproval(String username, int tenantId) throws IdentityEventException {
    boolean isPendingApproval;
    try {
        Entity entity = new Entity(MultitenantUtils.getTenantAwareUsername(username), WFConstant.WORKFLOW_ENTITY_TYPE, tenantId);
        WorkflowManagementService workflowManagementService = new WorkflowManagementServiceImpl();
        isPendingApproval = workflowManagementService.entityHasPendingWorkflowsOfType(entity, WFConstant.WORKFLOW_REQUEST_TYPE);
    } catch (WorkflowException e) {
        throw new IdentityEventException("Error occurred while checking the pending approvals for " + "the account of the user: " + username, e);
    } catch (IdentityRuntimeException e) {
        throw new IdentityEventException("Can't find the tenant domain for the user: " + username, e);
    }
    if (isPendingApproval) {
        IdentityErrorMsgContext customErrorMessageContext = new IdentityErrorMsgContext(IdentityCoreConstants.USER_ACCOUNT_PENDING_APPROVAL_ERROR_CODE);
        IdentityUtil.setIdentityErrorMsg(customErrorMessageContext);
        throw new IdentityEventException(WorkflowErrorConstants.ErrorMessages.ERROR_CODE_USER_ACCOUNT_PENDING_APPROVAL.getCode(), WorkflowErrorConstants.ErrorMessages.ERROR_CODE_USER_ACCOUNT_PENDING_APPROVAL.getMessage());
    }
}
Also used : Entity(org.wso2.carbon.identity.workflow.mgt.bean.Entity) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) WorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) IdentityErrorMsgContext(org.wso2.carbon.identity.core.model.IdentityErrorMsgContext) WorkflowManagementService(org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService) WorkflowManagementServiceImpl(org.wso2.carbon.identity.workflow.mgt.WorkflowManagementServiceImpl)

Example 2 with IdentityRuntimeException

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

the class SessionDataStore method removeSessionData.

public void removeSessionData(String key, String type, long nanoTime) {
    if (!enablePersist) {
        return;
    }
    if (tempDataCleanupEnabled && maxTempDataPoolSize > 0 && isTempCache(type)) {
        tempAuthnContextDataDeleteQueue.push(new SessionContextDO(key, type, null, nanoTime));
        return;
    }
    Connection connection = null;
    try {
        connection = IdentityDatabaseUtil.getSessionDBConnection(true);
    } catch (IdentityRuntimeException e) {
        log.error(e.getMessage(), e);
        return;
    }
    PreparedStatement preparedStatement = null;
    long timeoutNano = nanoTime + getCleanupTimeout(type, MultitenantConstants.INVALID_TENANT_ID);
    try {
        preparedStatement = connection.prepareStatement(getSessionStoreDBQuery(sqlInsertDELETE, type));
        preparedStatement.setString(1, key);
        preparedStatement.setString(2, type);
        preparedStatement.setString(3, OPERATION_DELETE);
        preparedStatement.setLong(4, nanoTime);
        preparedStatement.setLong(5, timeoutNano);
        preparedStatement.executeUpdate();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (Exception e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        log.error("Error while storing DELETE operation session data", e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, null, preparedStatement);
    }
    if (log.isDebugEnabled()) {
        log.debug("Removed SessionContextData from DB. key : " + key + " type : " + type);
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) SQLException(java.sql.SQLException) SessionSerializerException(org.wso2.carbon.identity.application.authentication.framework.exception.SessionSerializerException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) IOException(java.io.IOException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 3 with IdentityRuntimeException

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

the class SessionDataStore method removeExpiredSessionData.

/**
 * Removes the records related to expired sessions from DB.
 */
private void removeExpiredSessionData(String sqlQuery) {
    if (log.isDebugEnabled()) {
        log.debug("DB query for removing expired data: " + sqlQuery);
    }
    long currentTime = FrameworkUtils.getCurrentStandardNano();
    try (Connection connection = IdentityDatabaseUtil.getSessionDBConnection(true)) {
        boolean deleteCompleted = false;
        int totalDeletedEntries = 0;
        while (!deleteCompleted) {
            try (PreparedStatement statement = connection.prepareStatement(sqlQuery)) {
                statement.setLong(1, currentTime);
                int noOfDeletedRecords = statement.executeUpdate();
                deleteCompleted = noOfDeletedRecords < deleteChunkSize;
                totalDeletedEntries += noOfDeletedRecords;
                // Commit the chunk deletion.
                IdentityDatabaseUtil.commitTransaction(connection);
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Removed %d expired session records.", noOfDeletedRecords));
                }
            }
        }
        if (log.isDebugEnabled()) {
            log.debug(String.format("Deleted total of %d entries", totalDeletedEntries));
        }
    } catch (SQLException | IdentityRuntimeException e) {
        log.error("Error while removing session data from the database for nano time: " + currentTime, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 4 with IdentityRuntimeException

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

the class SessionDataStore method removeInvalidatedSTOREOperations.

/**
 * Removes STORE records related to DELETE records in IDN_AUTH_SESSION_STORE table
 */
private void removeInvalidatedSTOREOperations() {
    Connection connection = null;
    PreparedStatement statement = null;
    try {
        connection = IdentityDatabaseUtil.getSessionDBConnection(true);
    } catch (IdentityRuntimeException e) {
        log.error(e.getMessage(), e);
        return;
    }
    try {
        if (StringUtils.isBlank(sqlDeleteSTORETask)) {
            String driverName = connection.getMetaData().getDriverName();
            if (driverName.contains(MYSQL_DATABASE) || driverName.contains(MARIA_DATABASE)) {
                sqlDeleteSTORETask = SQL_DELETE_STORE_OPERATIONS_TASK_MYSQL;
            } else {
                sqlDeleteSTORETask = SQL_DELETE_STORE_OPERATIONS_TASK;
            }
        }
        statement = connection.prepareStatement(sqlDeleteSTORETask);
        statement.execute();
        IdentityDatabaseUtil.commitTransaction(connection);
        return;
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        log.error("Error while removing STORE operation data from the database. ", e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, null, statement);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 5 with IdentityRuntimeException

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

the class SessionDataStore method persistSessionData.

public void persistSessionData(String key, String type, Object entry, long nanoTime, int tenantId) {
    if (!enablePersist) {
        return;
    }
    Connection connection = null;
    try {
        connection = IdentityDatabaseUtil.getSessionDBConnection(true);
    } catch (IdentityRuntimeException e) {
        log.error(e.getMessage(), e);
        return;
    }
    long validityPeriodNano = 0L;
    if (entry instanceof CacheEntry) {
        validityPeriodNano = ((CacheEntry) entry).getValidityPeriod();
    }
    if (validityPeriodNano == 0L) {
        validityPeriodNano = getCleanupTimeout(type, tenantId);
    }
    PreparedStatement preparedStatement = null;
    try {
        String sqlQuery = getSessionStoreDBQuery(sqlInsertSTORE, type);
        preparedStatement = connection.prepareStatement(sqlQuery);
        preparedStatement.setString(1, key);
        preparedStatement.setString(2, type);
        preparedStatement.setString(3, OPERATION_STORE);
        setBlobObject(preparedStatement, entry, 4);
        preparedStatement.setLong(5, nanoTime);
        preparedStatement.setLong(6, nanoTime + validityPeriodNano);
        preparedStatement.setInt(7, tenantId);
        preparedStatement.executeUpdate();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException | IOException | SessionSerializerException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        log.error("Error while storing session data", e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, null, preparedStatement);
    }
    if (log.isDebugEnabled()) {
        log.debug("Persisted SessionContextData to DB. key : " + key + " type : " + type);
    }
}
Also used : SessionSerializerException(org.wso2.carbon.identity.application.authentication.framework.exception.SessionSerializerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) IOException(java.io.IOException) CacheEntry(org.wso2.carbon.identity.core.cache.CacheEntry)

Aggregations

IdentityRuntimeException (org.wso2.carbon.identity.base.IdentityRuntimeException)17 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)13 IOException (java.io.IOException)8 ResultSet (java.sql.ResultSet)5 SessionSerializerException (org.wso2.carbon.identity.application.authentication.framework.exception.SessionSerializerException)4 Registry (org.wso2.carbon.registry.core.Registry)4 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)4 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)3 ArrayList (java.util.ArrayList)2 IdentityEventException (org.wso2.carbon.identity.event.IdentityEventException)2 IdentityEventMessageContext (org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)2 Event (org.wso2.carbon.identity.event.event.Event)2 FunctionLibrary (org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1