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