Search in sources :

Example 1 with UserSession

use of org.wso2.carbon.identity.application.authentication.framework.model.UserSession 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 UserSession

use of org.wso2.carbon.identity.application.authentication.framework.model.UserSession 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)

Aggregations

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