/** * Perform session invalidation to avoid replay attacks. */ /*public class SessionInvalidator implements Runnable { public void run() { while (true) { try { for (ThriftSession thriftSession : authenticatedSessions.values()) { long currentTime = System.currentTimeMillis(); long createdTime = thriftSession.getCreatedAt(); if ((currentTime - createdTime) > 50000) { authenticatedSessions.remove(thriftSession.getSessionId()); } } Thread.sleep(50000); } catch (InterruptedException e) { e.printStackTrace(); } } } }*/ private boolean isSessionValid(ThriftSession thriftSession) { //check whether the session is expired. return ((System.currentTimeMillis() - thriftSession.getLastAccess()) < thriftSessionTimeOut); }
@Override public void addSession(ThriftSession session) { thriftSessionMap.put(session.getSessionId(), session); }
@Override public void updateLastAccessTime(String sessionId, long lastAccessTime) { ThriftSession thriftSession = thriftSessionMap.get(sessionId); if (thriftSession != null) { thriftSession.setLastAccess(lastAccessTime); } }
PreparedStatement prepStmt = null; ResultSet rSet = null; ThriftSession thriftSession = new ThriftSession(); try { connection = ThriftAuthenticationDatabaseUtil.getDBConnection(); thriftSession.setSessionId(rSet.getString(1)); thriftSession.setUserName(rSet.getString(2)); thriftSession.setCreatedAt(rSet.getLong(3)); thriftSession.setLastAccess(rSet.getLong(4));
String sqlStmt = null; if (!isSessionExisting(session.getSessionId())) { try { connection = ThriftAuthenticationDatabaseUtil.getDBConnection(); sqlStmt = ThriftAuthenticationConstants.ADD_THRIFT_SESSION_SQL; prepStmt = connection.prepareStatement(sqlStmt); prepStmt.setString(1, session.getSessionId()); prepStmt.setString(2, session.getUserName()); prepStmt.setLong(3, session.getCreatedAt()); prepStmt.setLong(4, session.getLastAccess());
(authenticatedSessions.get(sessionId)).setLastAccess(lastAccessTime); try { if (isSessionValid(thriftSession)) { thriftSession.setLastAccess(System.currentTimeMillis()); onSuccessLogin(thriftSession); authenticatedSessions.put(thriftSession.getSessionId(), thriftSession); thriftSessionDAO.updateLastAccessTime(sessionId, thriftSession.getLastAccess()); return true; } else { ThriftSession thriftSession = thriftSessionDAO.getSession(sessionId); if (isSessionValid(thriftSession)) { thriftSession.setLastAccess(System.currentTimeMillis()); onSuccessLogin(thriftSession); authenticatedSessions.put(thriftSession.getSessionId(), thriftSession); thriftSessionDAO.updateLastAccessTime(sessionId, thriftSession.getLastAccess()); return true; } else {
private void onSuccessLogin(ThriftSession authSession) throws IdentityException { PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); try { carbonContext.setUsername((String) (authSession.getAttribute(ServerConstants.AUTHENTICATION_SERVICE_USERNAME))); carbonContext.setTenantDomain((String) (authSession.getAttribute(MultitenantConstants.TENANT_DOMAIN))); carbonContext.setTenantId((Integer) (authSession.getAttribute(MultitenantConstants.TENANT_ID))); } catch (Exception e) { String authErrorMsg = "Error populating current carbon context from thrift auth session: " + e.getMessage(); throw new IdentityException(authErrorMsg); } }
private void callOnSuccessAdminLogin(ThriftSession session) throws Exception { if (realmService != null) { String tenantDomain = MultitenantUtils.getTenantDomain(session.getUserName()); int tenantId = realmService.getTenantManager().getTenantId(tenantDomain); CarbonAuthenticationUtil.onSuccessAdminLogin(session, session.getUserName(), tenantId, tenantDomain, ""); } }
PreparedStatement prepStmt = null; ResultSet rSet = null; ThriftSession thriftSession = new ThriftSession(); try { connection = ThriftAuthenticationDatabaseUtil.getDBConnection(); thriftSession.setSessionId(rSet.getString(1)); thriftSession.setUserName(rSet.getString(2)); thriftSession.setCreatedAt(rSet.getLong(3)); thriftSession.setLastAccess(rSet.getLong(4));
String sqlStmt = null; if (!isSessionExisting(session.getSessionId())) { try { connection = ThriftAuthenticationDatabaseUtil.getDBConnection(); sqlStmt = ThriftAuthenticationConstants.ADD_THRIFT_SESSION_SQL; prepStmt = connection.prepareStatement(sqlStmt); prepStmt.setString(1, session.getSessionId()); prepStmt.setString(2, session.getUserName()); prepStmt.setLong(3, session.getCreatedAt()); prepStmt.setLong(4, session.getLastAccess());
(authenticatedSessions.get(sessionId)).setLastAccess(lastAccessTime); try { if (isSessionValid(thriftSession)) { thriftSession.setLastAccess(System.currentTimeMillis()); onSuccessLogin(thriftSession); authenticatedSessions.put(thriftSession.getSessionId(), thriftSession); sessionDAO.updateLastAccessTime(sessionId, thriftSession.getLastAccess()); return true; } else { ThriftSession thriftSession = sessionDAO.getSession(sessionId); if (isSessionValid(thriftSession)) { thriftSession.setLastAccess(System.currentTimeMillis()); onSuccessLogin(thriftSession); authenticatedSessions.put(thriftSession.getSessionId(), thriftSession); sessionDAO.updateLastAccessTime(sessionId, thriftSession.getLastAccess()); return true; } else {
private void onSuccessLogin(ThriftSession authSession) throws IdentityException { PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); try { carbonContext.setUsername((String) (authSession.getAttribute(ServerConstants.AUTHENTICATION_SERVICE_USERNAME))); carbonContext.setTenantDomain((String) (authSession.getAttribute(MultitenantConstants.TENANT_DOMAIN))); carbonContext.setTenantId((Integer) (authSession.getAttribute(MultitenantConstants.TENANT_ID))); } catch (Exception e) { String authErrorMsg = "Error populating current carbon context from thrift auth session: " + e.getMessage(); throw IdentityException.error(authErrorMsg); } }
private void callOnSuccessAdminLogin(ThriftSession session) throws Exception { if (realmService != null) { String tenantDomain = MultitenantUtils.getTenantDomain(session.getUserName()); int tenantId = realmService.getTenantManager().getTenantId(tenantDomain); CarbonAuthenticationUtil.onSuccessAdminLogin(session, session.getUserName(), tenantId, tenantDomain, ""); } }
PreparedStatement prepStmt = null; ResultSet rSet = null; ThriftSession thriftSession = new ThriftSession(); try { connection = ThriftAuthenticationDatabaseUtil.getDBConnection(); thriftSession.setSessionId(rSet.getString(1)); thriftSession.setUserName(rSet.getString(2)); thriftSession.setCreatedAt(rSet.getLong(3)); thriftSession.setLastAccess(rSet.getLong(4));
String sqlStmt = null; if (!isSessionExisting(session.getSessionId())) { try { connection = ThriftAuthenticationDatabaseUtil.getDBConnection(); sqlStmt = ThriftAuthenticationConstants.ADD_THRIFT_SESSION_SQL; prepStmt = connection.prepareStatement(sqlStmt); prepStmt.setString(1, session.getSessionId()); prepStmt.setString(2, session.getUserName()); prepStmt.setLong(3, session.getCreatedAt()); prepStmt.setLong(4, session.getLastAccess());
(authenticatedSessions.get(sessionId)).setLastAccess(lastAccessTime); try { if (isSessionValid(thriftSession)) { thriftSession.setLastAccess(System.currentTimeMillis()); onSuccessLogin(thriftSession); authenticatedSessions.put(thriftSession.getSessionId(), thriftSession); sessionDAO.updateLastAccessTime(sessionId, thriftSession.getLastAccess()); return true; } else { ThriftSession thriftSession = sessionDAO.getSession(sessionId); if (isSessionValid(thriftSession)) { thriftSession.setLastAccess(System.currentTimeMillis()); onSuccessLogin(thriftSession); authenticatedSessions.put(thriftSession.getSessionId(), thriftSession); sessionDAO.updateLastAccessTime(sessionId, thriftSession.getLastAccess()); return true; } else {
@Override public void addSession(ThriftSession session) { thriftSessionMap.put(session.getSessionId(), session); }
private void onSuccessLogin(ThriftSession authSession) throws IdentityException { PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); try { carbonContext.setUsername((String) (authSession.getAttribute(ServerConstants.AUTHENTICATION_SERVICE_USERNAME))); carbonContext.setTenantDomain((String) (authSession.getAttribute(MultitenantConstants.TENANT_DOMAIN))); carbonContext.setTenantId((Integer) (authSession.getAttribute(MultitenantConstants.TENANT_ID))); } catch (Exception e) { String authErrorMsg = "Error populating current carbon context from thrift auth session: " + e.getMessage(); throw IdentityException.error(authErrorMsg); } }
private void callOnSuccessAdminLogin(ThriftSession session) throws Exception { if (realmService != null) { String tenantDomain = MultitenantUtils.getTenantDomain(session.getUserName()); int tenantId = realmService.getTenantManager().getTenantId(tenantDomain); CarbonAuthenticationUtil.onSuccessAdminLogin(session, session.getUserName(), tenantId, tenantDomain, ""); } }
@Override public void updateLastAccessTime(String sessionId, long lastAccessTime) { ThriftSession thriftSession = thriftSessionMap.get(sessionId); if (thriftSession != null) { thriftSession.setLastAccess(lastAccessTime); } }