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;
}
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);
}
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;
}
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);
}
}
Aggregations