Search in sources :

Example 1 with UserSessionException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException in project carbon-identity-framework by wso2.

the class UserSessionStore method storeSessionMetaData.

/**
 * Method to store session meta data as a batch
 *
 * @param sessionId id of the authenticated session
 * @param metaData  map of metadata type and value of the session
 * @throws UserSessionException while storing session meta data
 */
public void storeSessionMetaData(String sessionId, Map<String, String> metaData) throws UserSessionException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        String sqlStmt = isH2DB() ? SQLQueries.SQL_INSERT_SESSION_META_DATA_H2 : SQLQueries.SQL_INSERT_SESSION_META_DATA;
        jdbcTemplate.executeBatchInsert(sqlStmt, (preparedStatement -> {
            for (Map.Entry<String, String> entry : metaData.entrySet()) {
                preparedStatement.setString(1, sessionId);
                preparedStatement.setString(2, entry.getKey());
                preparedStatement.setString(3, entry.getValue());
                preparedStatement.addBatch();
            }
        }), sessionId);
        if (log.isDebugEnabled()) {
            log.debug("Inserted metadata for session id: " + sessionId);
        }
    } catch (DataAccessException e) {
        throw new UserSessionException("Error while storing metadata of session:" + sessionId + " in table " + IDN_AUTH_SESSION_META_DATA_TABLE + ".", e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SessionMgtConstants(org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DuplicatedAuthUserException(org.wso2.carbon.identity.application.authentication.framework.exception.DuplicatedAuthUserException) SQLException(java.sql.SQLException) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) JdbcUtils(org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils) ResultSet(java.sql.ResultSet) Map(java.util.Map) IdentityTenantUtil(org.wso2.carbon.identity.core.util.IdentityTenantUtil) User(org.wso2.carbon.identity.application.common.model.User) IdPManagementUtil(org.wso2.carbon.idp.mgt.util.IdPManagementUtil) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) FrameworkUtils(org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 2 with UserSessionException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException in project carbon-identity-framework by wso2.

the class UserSessionStore method storeFederatedAuthSessionInfo.

/**
 * Store session details of a given session context key to map the session context key with
 * the federated IdP's session ID.
 *
 * @param sessionContextKey Session Context Key.
 * @param authHistory       History of the authentication flow.
 * @throws UserSessionException Error while storing session details.
 */
public void storeFederatedAuthSessionInfo(String sessionContextKey, AuthHistory authHistory) throws UserSessionException {
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (PreparedStatement prepStmt = connection.prepareStatement(SQLQueries.SQL_STORE_FEDERATED_AUTH_SESSION_INFO)) {
            prepStmt.setString(1, authHistory.getIdpSessionIndex());
            prepStmt.setString(2, sessionContextKey);
            prepStmt.setString(3, authHistory.getIdpName());
            prepStmt.setString(4, authHistory.getAuthenticatorName());
            prepStmt.setString(5, authHistory.getRequestType());
            prepStmt.execute();
        } catch (SQLException e1) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new UserSessionException("Error while adding session details of the session index:" + sessionContextKey + ", IdP:" + authHistory.getIdpName(), e1);
        }
    } catch (SQLException e) {
        throw new UserSessionException("Error while adding session details of the session index:" + sessionContextKey + ", IdP:" + authHistory.getIdpName(), e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException)

Example 3 with UserSessionException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException in project carbon-identity-framework by wso2.

the class UserSessionStore method storeUserSessionData.

/**
 * Method to store user id and session id mapping in the database table IDN_AUTH_USER_SESSION_MAPPING.
 *
 * @param userId    Id of the user
 * @param sessionId Id of the authenticated session
 * @throws UserSessionException if an error occurs when storing the mapping in the database
 */
public void storeUserSessionData(String userId, String sessionId) throws UserSessionException {
    try (Connection connection = IdentityDatabaseUtil.getSessionDBConnection(true)) {
        try (PreparedStatement preparedStatement = connection.prepareStatement(SQLQueries.SQL_INSERT_USER_SESSION_STORE_OPERATION)) {
            preparedStatement.setString(1, userId);
            preparedStatement.setString(2, sessionId);
            preparedStatement.executeUpdate();
            IdentityDatabaseUtil.commitTransaction(connection);
            if (log.isDebugEnabled()) {
                log.debug("Stored user session data for user " + userId + " with session id: " + sessionId);
            }
        } catch (SQLException e1) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new UserSessionException("Error while storing mapping between user Id: " + userId + " and session Id: " + sessionId, e1);
        }
    } catch (SQLException e) {
        throw new UserSessionException("Error while storing mapping between user Id: " + userId + " and session Id: " + sessionId, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException)

Example 4 with UserSessionException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException in project carbon-identity-framework by wso2.

the class UserSessionStore method isExistingMapping.

/**
 * Method to check whether a given user already has a mapping with a given session id.
 *
 * @param user      user object
 * @param sessionId id of the authenticated session
 * @return the boolean decision
 * @throws UserSessionException if an error occurs when retrieving the mapping from the database
 */
public boolean isExistingMapping(User user, int idpId, String sessionId) throws UserSessionException {
    boolean isExisting = false;
    int tenantId = IdentityTenantUtil.getTenantId(user.getTenantDomain());
    try (Connection connection = IdentityDatabaseUtil.getSessionDBConnection(false)) {
        try (PreparedStatement preparedStatement = connection.prepareStatement(SQLQueries.SQL_GET_SESSION_MAPPING_BY_USER)) {
            preparedStatement.setString(1, sessionId);
            preparedStatement.setString(2, user.getUserName());
            preparedStatement.setInt(3, tenantId);
            preparedStatement.setString(4, (user.getUserStoreDomain() == null) ? FEDERATED_USER_DOMAIN : user.getUserStoreDomain().toUpperCase());
            preparedStatement.setInt(5, idpId);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    isExisting = true;
                }
            }
        } catch (SQLException ex) {
            throw new UserSessionException("Error while retrieving existing mapping between user : " + user.getLoggableUserId() + " and session Id: " + sessionId + ".", ex);
        }
    } catch (SQLException e) {
        throw new UserSessionException("Error while retrieving existing mapping between user : " + user.getLoggableUserId() + " and session Id: " + sessionId + ".", e);
    }
    return isExisting;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException)

Example 5 with UserSessionException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException in project carbon-identity-framework by wso2.

the class UserSessionStore method isExistingAppSession.

/**
 * Method to check whether the particular app session is already exists in the database.
 *
 * @param sessionId   id of the authenticated session
 * @param subject     user name of app
 * @param appID       id of application
 * @param inboundAuth protocol used in app
 * @return whether the app session is already available or not
 * @throws UserSessionException while retrieving existing session data
 */
public boolean isExistingAppSession(String sessionId, String subject, int appID, String inboundAuth) throws UserSessionException {
    Integer recordCount;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        recordCount = jdbcTemplate.fetchSingleRecord(SQLQueries.SQL_CHECK_IDN_AUTH_SESSION_APP_INFO, (resultSet, rowNumber) -> resultSet.getInt(1), preparedStatement -> {
            preparedStatement.setString(1, sessionId);
            preparedStatement.setString(2, subject);
            preparedStatement.setInt(3, appID);
            preparedStatement.setString(4, inboundAuth);
        });
    } catch (DataAccessException e) {
        throw new UserSessionException("Error while retrieving application data of session id: " + sessionId + ", subject: " + subject + ", app Id: " + appID + ", protocol: " + inboundAuth + ".", e);
    }
    return recordCount != null;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SessionMgtConstants(org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DuplicatedAuthUserException(org.wso2.carbon.identity.application.authentication.framework.exception.DuplicatedAuthUserException) SQLException(java.sql.SQLException) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) JdbcUtils(org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils) ResultSet(java.sql.ResultSet) Map(java.util.Map) IdentityTenantUtil(org.wso2.carbon.identity.core.util.IdentityTenantUtil) User(org.wso2.carbon.identity.application.common.model.User) IdPManagementUtil(org.wso2.carbon.idp.mgt.util.IdPManagementUtil) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) FrameworkUtils(org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Aggregations

UserSessionException (org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException)24 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)14 SQLException (java.sql.SQLException)14 ResultSet (java.sql.ResultSet)10 ArrayList (java.util.ArrayList)6 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)6 AuthHistory (org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory)6 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)5 DuplicatedAuthUserException (org.wso2.carbon.identity.application.authentication.framework.exception.DuplicatedAuthUserException)5 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)4 HashSet (java.util.HashSet)4 List (java.util.List)4 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)4 Map (java.util.Map)3 Set (java.util.Set)3 TimeUnit (java.util.concurrent.TimeUnit)3 StringUtils (org.apache.commons.lang.StringUtils)3 Log (org.apache.commons.logging.Log)3 LogFactory (org.apache.commons.logging.LogFactory)3