2006-04-17 21:20:14 +02:00
|
|
|
"""
|
|
|
|
Session manager class for gnuviech-admin tool backend
|
|
|
|
(c) 2006 Jan Dittberner <jan@dittberner.info>
|
|
|
|
$Id$
|
|
|
|
"""
|
2006-04-15 22:00:23 +02:00
|
|
|
import Settings
|
2006-04-17 21:20:14 +02:00
|
|
|
import os, sha, time, logging
|
|
|
|
from threading import Timer
|
|
|
|
|
|
|
|
SESSIONTIMEOUT=120 # 2 minutes
|
2006-04-15 22:00:23 +02:00
|
|
|
|
|
|
|
class InvalidLoginError(Exception):
|
2006-04-17 21:20:14 +02:00
|
|
|
"""
|
|
|
|
Exception class for invalid logins.
|
|
|
|
"""
|
2006-04-15 22:00:23 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
class InvalidSessionError(Exception):
|
2006-04-17 21:20:14 +02:00
|
|
|
"""
|
|
|
|
Exception class for invalid sessions.
|
|
|
|
"""
|
2006-04-15 22:00:23 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
class Session:
|
2006-04-17 21:20:14 +02:00
|
|
|
def __init__(self, id, login):
|
2006-04-15 22:00:23 +02:00
|
|
|
self.id = id
|
|
|
|
self.login = login
|
2006-04-17 21:20:14 +02:00
|
|
|
self._timeoutTimer = None
|
|
|
|
|
|
|
|
def settimeoutTimer(self, timeoutTimer):
|
|
|
|
self._timeoutTimer = timeoutTimer
|
|
|
|
self._timeoutTimer.start()
|
|
|
|
|
|
|
|
def gettimeoutTimer(self):
|
|
|
|
return self._timeoutTimer
|
2006-04-15 22:00:23 +02:00
|
|
|
|
|
|
|
class SessionManager:
|
2006-04-17 21:20:14 +02:00
|
|
|
def __init__(self, dbconn):
|
|
|
|
self._sessions = {}
|
|
|
|
self._dbconn = dbconn
|
|
|
|
self._hashobj = sha.new(str(time.time()))
|
|
|
|
self.logger = logging.getLogger('SessionManager')
|
|
|
|
|
|
|
|
def listSessions(self):
|
|
|
|
return self._sessions.keys()
|
2006-04-15 22:00:23 +02:00
|
|
|
|
|
|
|
def newSession(self, login, password):
|
2006-04-17 21:20:14 +02:00
|
|
|
self._hashobj.update("%s,%s" % (time.time(), login))
|
|
|
|
sessionid = self._hashobj.hexdigest()
|
|
|
|
self._sessions[sessionid] = Session(sessionid, login)
|
|
|
|
self.updateSession(sessionid)
|
|
|
|
return sessionid
|
2006-04-15 22:00:23 +02:00
|
|
|
|
2006-04-17 21:20:14 +02:00
|
|
|
def updateSession(self, sessionid):
|
|
|
|
self.logger.debug("update session %s" % sessionid)
|
|
|
|
try:
|
|
|
|
session = self.getSession(sessionid)
|
|
|
|
except InvalidSessionError, ev:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if session.gettimeoutTimer() is not None:
|
|
|
|
session.gettimeoutTimer().cancel()
|
|
|
|
session.settimeoutTimer(Timer(SESSIONTIMEOUT, self.deleteSession,
|
|
|
|
args=[sessionid]))
|
|
|
|
|
2006-04-15 22:00:23 +02:00
|
|
|
def getSession(self, sessionid):
|
2006-04-17 21:20:14 +02:00
|
|
|
if self._sessions.has_key(sessionid):
|
|
|
|
return self._sessions[sessionid]
|
2006-04-15 22:00:23 +02:00
|
|
|
raise InvalidSessionError()
|
|
|
|
|
|
|
|
def deleteSession(self, sessionid):
|
2006-04-17 21:20:14 +02:00
|
|
|
self.logger.debug("delete session %s" % sessionid)
|
|
|
|
try:
|
|
|
|
session = self.getSession(sessionid)
|
|
|
|
except InvalidSessionError:
|
|
|
|
print "invalid session"
|
|
|
|
else:
|
|
|
|
if session.gettimeoutTimer() is not None:
|
|
|
|
session.gettimeoutTimer().cancel()
|
|
|
|
del(self._sessions[sessionid])
|
|
|
|
self.logger.debug("%d sessions remaining" % len(self.listSessions()))
|