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-11-04 21:24:36 +01:00
|
|
|
import os, sha, time, logging, psycopg2
|
2006-04-17 21:20:14 +02:00
|
|
|
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-19 11:19:10 +02:00
|
|
|
"""
|
|
|
|
The Sessionmanager provides methods for login and session handling.
|
|
|
|
"""
|
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-18 21:27:33 +02:00
|
|
|
cr = self._dbconn.cursor()
|
|
|
|
cr.execute('SELECT * FROM sysuser WHERE name=%(login)s AND md5pass=md5(%(password)s)' %
|
2006-11-04 21:24:36 +01:00
|
|
|
{'login': psycopg2.QuotedString(login),
|
|
|
|
'password' : psycopg2.QuotedString(password)})
|
2006-04-18 21:27:33 +02:00
|
|
|
self._dbconn.commit()
|
|
|
|
result = cr.fetchall()
|
|
|
|
if cr.rowcount == 1:
|
|
|
|
self._hashobj.update("%s,%s" % (time.time(), login))
|
|
|
|
sessionid = self._hashobj.hexdigest()
|
|
|
|
self._sessions[sessionid] = Session(sessionid, login)
|
|
|
|
self.updateSession(sessionid)
|
2006-04-19 11:19:10 +02:00
|
|
|
self.logger.info('New session with id %s created for %s' %
|
|
|
|
(sessionid, login))
|
2006-04-18 21:27:33 +02:00
|
|
|
return sessionid
|
2006-04-19 11:19:10 +02:00
|
|
|
self.logger.info('Login for %s failed' % login)
|
2006-04-18 21:27:33 +02:00
|
|
|
raise InvalidLoginError
|
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()))
|