Changeset 1722

Show
Ignore:
Timestamp:
12/20/06 21:07:00 (2 years ago)
Author:
cygnus
Message:

OpenidPlugin:

Fix session behavior so openid data gets properly loaded and saved

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openidplugin/trunk/openidauth/auth.py

    r1695 r1722  
    55import time 
    66import thread 
     7import cPickle 
    78 
    89from openid.store import dumbstore 
     
    3031        """Whether we should ask the ID provider for the user's 
    3132        full name and email address.""") 
     33 
     34    openid_session_key = 'openid_session_data' 
    3235 
    3336    def __init__(self): 
     
    101104        return self._getTrustRoot(req) + self.env.href.openid_return() 
    102105  
     106    def _getConsumer(self, req): 
     107        s = self._get_session(req) 
     108        return consumer.Consumer(s, self._get_store()), s 
     109 
    103110    def _start_login(self, req, url): 
    104111        """Initiates OpenID login phase.""" 
    105         oidconsumer = consumer.Consumer(self._get_session(req), self._get_store()
     112        oidconsumer, session = self._getConsumer(req
    106113        try: 
    107114            authreq = oidconsumer.begin(url) 
     
    110117            return False 
    111118 
     119        self._commit_session(session, req) 
     120 
    112121        if self.require_personal_details: 
    113122            # tell the IdP that we need the user's email address 
     
    123132         
    124133        The IdP sends the user back to us.""" 
    125         oidconsumer = consumer.Consumer(self._get_session(req), self._get_store()
     134        oidconsumer, session = self._getConsumer(req
    126135        response = oidconsumer.complete(req.args) 
     136        self._commit_session(session, req) 
     137 
    127138        if response.status == consumer.SUCCESS: 
    128139            if response.getReturnTo().split('?')[0] == self._getReturnTo(req): 
     
    192203 
    193204    def _get_session(self, req): 
    194         """Returns a session dict that can store any kind of object. 
    195          
    196         This is a hack to get around the OpenID library's limitations.""" 
     205        """Returns a session dict that can store any kind of object.""" 
    197206 
    198207        # we must be thread-safe 
    199208        self.lock.acquire() 
    200          
    201         # first get rid of old sessions 
    202         now = int(time.time()) 
    203         for k, v in self.sessions.items(): 
    204             if v['_last_access'] + 3 * 60 < now: 
    205                 del self.sessions[k] 
    206          
    207         # now find an existing session or create a new one 
    208         session = {} 
    209         sid = req.session.sid 
    210         if self.sessions.has_key(sid): 
    211             session = self.sessions[sid] 
    212         else: 
    213             self.sessions[sid] = session 
    214  
    215         session['_last_access'] = now 
     209        try: 
     210            session = cPickle.loads(str(req.session[self.openid_session_key])) 
     211        except KeyError: 
     212            session = {} 
    216213        self.lock.release() 
    217214 
    218215        return session 
     216 
     217    def _commit_session(self, session, req): 
     218        req.session[self.openid_session_key] = str(cPickle.dumps(session)) 
    219219 
    220220    def _get_store(self):