Search in sources :

Example 1 with SessionManagementServerException

use of org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException in project carbon-identity-framework by wso2.

the class UserSessionDAOImpl method getSession.

public UserSession getSession(String sessionId) throws SessionManagementServerException {
    HashMap<String, String> propertiesMap = new HashMap<>();
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        List<Application> applicationList = getApplicationsForSessionID(sessionId);
        generateApplicationFromAppID(applicationList);
        String sqlStmt = isH2DB() ? SQLQueries.SQL_GET_PROPERTIES_FROM_SESSION_META_DATA_H2 : SQLQueries.SQL_GET_PROPERTIES_FROM_SESSION_META_DATA;
        jdbcTemplate.executeQuery(sqlStmt, ((resultSet, rowNumber) -> propertiesMap.put(resultSet.getString(1), resultSet.getString(2))), preparedStatement -> preparedStatement.setString(1, sessionId));
        UserSession userSession = new UserSession();
        userSession.setSessionId(sessionId);
        propertiesMap.forEach((key, value) -> {
            switch(key) {
                case SessionMgtConstants.USER_AGENT:
                    userSession.setUserAgent(value);
                    break;
                case SessionMgtConstants.IP_ADDRESS:
                    userSession.setIp(value);
                    break;
                case SessionMgtConstants.LAST_ACCESS_TIME:
                    userSession.setLastAccessTime(value);
                    break;
                case SessionMgtConstants.LOGIN_TIME:
                    userSession.setLoginTime(value);
                    break;
            }
        });
        if (!applicationList.isEmpty()) {
            userSession.setApplications(applicationList);
            return userSession;
        }
    } catch (DataAccessException e) {
        throw new SessionManagementServerException(SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_SESSION, SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_SESSION.getDescription(), e);
    }
    return null;
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) UserSession(org.wso2.carbon.identity.application.authentication.framework.model.UserSession) UserSessionDAO(org.wso2.carbon.identity.application.authentication.framework.dao.UserSessionDAO) HashMap(java.util.HashMap) SQLQueries(org.wso2.carbon.identity.application.authentication.framework.store.SQLQueries) PreparedStatement(java.sql.PreparedStatement) SessionMgtConstants(org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants) Collectors(java.util.stream.Collectors) SQLException(java.sql.SQLException) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) SessionManagementServerException(org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException) JdbcUtils(org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils) ResultSet(java.sql.ResultSet) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) Map(java.util.Map) Application(org.wso2.carbon.identity.application.authentication.framework.model.Application) Collections(java.util.Collections) SessionManagementServerException(org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException) HashMap(java.util.HashMap) UserSession(org.wso2.carbon.identity.application.authentication.framework.model.UserSession) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Application(org.wso2.carbon.identity.application.authentication.framework.model.Application) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 2 with SessionManagementServerException

use of org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException in project carbon-identity-framework by wso2.

the class UserSessionDAOImpl method generateApplicationFromAppID.

private void generateApplicationFromAppID(List<Application> applications) throws SessionManagementServerException {
    Map<String, List<Application>> appIdMap = applications.stream().collect(Collectors.groupingBy(Application::getAppId));
    String placeholder = String.join(", ", Collections.nCopies(appIdMap.keySet().size(), "?"));
    String sql = SQLQueries.SQL_GET_APPLICATION.replace(SCOPE_LIST_PLACEHOLDER, placeholder);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement ps = connection.prepareStatement(sql)) {
        int index = 1;
        for (String appId : appIdMap.keySet()) {
            ps.setString(index, appId);
            index++;
        }
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                for (Application application : appIdMap.get(rs.getString("ID"))) {
                    application.setAppName(rs.getString("APP_NAME"));
                    application.setResourceId(rs.getString("UUID"));
                }
            }
        }
    } catch (SQLException e) {
        throw new SessionManagementServerException(SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_SESSION, SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_SESSION.getDescription(), e);
    }
    /**
     * If application is not present in the SP_APP table but has a session associated with it that application
     * should not be considered for the session object.
     */
    applications.removeIf(application -> application.getAppName() == null);
}
Also used : SessionManagementServerException(org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.identity.application.authentication.framework.model.Application)

Example 3 with SessionManagementServerException

use of org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException in project carbon-identity-framework by wso2.

the class UserSessionManagementServiceImpl method getActiveSessionList.

/**
 * Returns the active sessions from given list of session IDs.
 *
 * @param sessionIdList list of sessionIds
 * @return list of user sessions
 * @throws SessionManagementServerException if an error occurs when retrieving the UserSessions.
 */
private List<UserSession> getActiveSessionList(List<String> sessionIdList) throws SessionManagementServerException {
    List<UserSession> sessionsList = new ArrayList<>();
    for (String sessionId : sessionIdList) {
        if (sessionId != null) {
            SessionContext sessionContext = FrameworkUtils.getSessionContextFromCache(sessionId, FrameworkUtils.getLoginTenantDomainFromContext());
            if (sessionContext != null) {
                UserSessionDAO userSessionDTO = new UserSessionDAOImpl();
                UserSession userSession = userSessionDTO.getSession(sessionId);
                if (userSession != null) {
                    sessionsList.add(userSession);
                }
            }
        }
    }
    return sessionsList;
}
Also used : UserSession(org.wso2.carbon.identity.application.authentication.framework.model.UserSession) ArrayList(java.util.ArrayList) UserSessionDAOImpl(org.wso2.carbon.identity.application.authentication.framework.dao.impl.UserSessionDAOImpl) SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) UserSessionDAO(org.wso2.carbon.identity.application.authentication.framework.dao.UserSessionDAO)

Example 4 with SessionManagementServerException

use of org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException in project carbon-identity-framework by wso2.

the class UserSessionDAO method getFederatedAuthSessionDetails.

/**
 * Get federated user session details mapped for federated IDP sessionId.
 *
 * @param fedIdpSessionId sid claim in the logout token of the federated idp.
 * @return A FederatedUserSession containing federated authentication session details.
 * @throws SessionManagementServerException
 */
default FederatedUserSession getFederatedAuthSessionDetails(String fedIdpSessionId) throws SessionManagementServerException {
    FederatedUserSession federatedUserSession;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        federatedUserSession = jdbcTemplate.fetchSingleRecord(SQLQueries.SQL_GET_FEDERATED_AUTH_SESSION_INFO_BY_SESSION_ID, (resultSet, rowNumber) -> new FederatedUserSession(resultSet.getString(SessionMgtConstants.FEDERATED_IDP_SESSION_ID), resultSet.getString(SessionMgtConstants.FEDERATED_SESSION_ID), resultSet.getString(SessionMgtConstants.FEDERATED_IDP_NAME), resultSet.getString(SessionMgtConstants.FEDERATED_AUTHENTICATOR_ID), resultSet.getString(SessionMgtConstants.FEDERATED_PROTOCOL_TYPE)), preparedStatement -> preparedStatement.setString(1, fedIdpSessionId));
        return federatedUserSession;
    } catch (DataAccessException e) {
        throw new SessionManagementServerException(SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_FED_USER_SESSION, SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_FED_USER_SESSION.getDescription(), e);
    }
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) SessionManagementServerException(org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException) JdbcUtils(org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) FederatedUserSession(org.wso2.carbon.identity.application.authentication.framework.model.FederatedUserSession) UserSession(org.wso2.carbon.identity.application.authentication.framework.model.UserSession) SQLQueries(org.wso2.carbon.identity.application.authentication.framework.store.SQLQueries) SessionMgtConstants(org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants) SessionManagementServerException(org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException) FederatedUserSession(org.wso2.carbon.identity.application.authentication.framework.model.FederatedUserSession) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Aggregations

SessionManagementServerException (org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException)3 UserSession (org.wso2.carbon.identity.application.authentication.framework.model.UserSession)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)2 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)2 UserSessionDAO (org.wso2.carbon.identity.application.authentication.framework.dao.UserSessionDAO)2 Application (org.wso2.carbon.identity.application.authentication.framework.model.Application)2 SQLQueries (org.wso2.carbon.identity.application.authentication.framework.store.SQLQueries)2 JdbcUtils (org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils)2 SessionMgtConstants (org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 SessionContext (org.wso2.carbon.identity.application.authentication.framework.context.SessionContext)1