def init_session(cookie, user=None): '''Initiates the session. It uses the cookie passed in (ie the value of os.environ.get("HTTP_COOKIE", "") loaded into a Cookie.SimpleCookie). Before trying to validate the session and generating a new session cookie the function first calls clear_old_sessions(), ensuring only non-expired sessions are validated. If the session validates then it generates a new session cookie for that session. If anything else happens it creates a new session and generates a new cookie. The method returns the generated cookie and the session dictionary.''' clear_old_sessions() c = cookie ses_dict = {} if cookie.has_key('session') and cookie.has_key('session_sig'): cook = cookie['session'].value cook_dict = nDDB.decodeDDB(cook) try: #print "Content-type: text/html" #print logger.writeln('session_id: ', cook_dict['sessionID']) session = get_session(cook_dict['sessionID']) logger.writeln('database_session_dict: ', session) if session: cookie_check = check_cookie(session['msg_sig'], session['sig_id'], cook_dict['time'], cook) else: cookie_check = None logger.writeln('cookie_check: ', cookie_check) if cookie_check: sigID = genSigID() logger.writeln('sigID: ', sigID) epochtime = str(time.time()) logger.writeln('epochtime: ', epochtime) if user == None: user = session['usr_id'] logger.writeln('user: '******'session_id'], sigID, epochtime) logger.writeln('c: ', c) logger.writeln('msg_sig: ', msg_sig) ses_dict = update_session(session['session_id'], sigID, msg_sig, user) logger.writeln('ses_dict: ', ses_dict) else: c, ses_dict = make_new_session() except Exception, e: print "Content-type: text/html" print print '<h1>Cookie Session Error</h1>' print e, '<br>'*5 logger.writeln() logger.writeln('COOKIE SESSION ERROR: ', e) c, ses_dict = make_new_session()
def verify_session(): '''This method returns True if the current session is valid, False otherwise.''' cookie = Cookie.SimpleCookie() cookieHdr = os.environ.get("HTTP_COOKIE", "") cookie.load(cookieHdr) if cookie.has_key('session') and cookie.has_key('session_sig'): cook = cookie['session'].value cook_dict = nDDB.decodeDDB(cook) try: session = get_session(cook_dict['sessionID']) if session: return check_cookie(session['msg_sig'], session['sig_id'], cook_dict['time'], cook) return False except: return False else: return False