Changeset 1723

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

OpenidPlugin:

Pickle openid state data into trac session and remove lock

Files:

Legend:

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

    r1722 r1723  
    11# Copyright 2006, Waldemar Kornewald <wkornew@gmx.net> 
     2# with modifications by Jonathan Daugherty <cygnus@janrain.com> 
    23# Distributed under the terms of the MIT License. 
    34 
     
    78import cPickle 
    89 
    9 from openid.store import dumbstore 
     10from openid.store import sqlstore 
    1011from openid.consumer import consumer 
    1112from yadis.discover import DiscoveryFailure 
     
    1920from trac.util import escape, hex_entropy, TracError, Markup 
    2021 
     22class TracOpenIDStore(sqlstore.SQLStore): 
     23    """ 
     24    An SQLStore subclass for storing OpenID association data.  This 
     25    doesn't use the Trac database schema specification idiom, because 
     26    at the time of this writing, the trac sqlite backend ignores size 
     27    specifications on columns, which are needed for these tables. 
     28    """ 
     29 
     30    create_nonce_sql = """ 
     31    CREATE TABLE %(nonces)s 
     32    ( 
     33        nonce CHAR(8) UNIQUE PRIMARY KEY, 
     34        expires INTEGER 
     35    )""" 
     36 
     37    create_assoc_sql = """ 
     38    CREATE TABLE %(associations)s 
     39    ( 
     40        server_url BLOB, 
     41        handle VARCHAR(255), 
     42        secret BLOB, 
     43        issued INTEGER, 
     44        lifetime INTEGER, 
     45        assoc_type VARCHAR(64), 
     46        PRIMARY KEY (server_url(255), handle) 
     47    )""" 
     48 
     49    create_settings_sql = """ 
     50    CREATE TABLE %(settings)s 
     51    ( 
     52        setting VARCHAR(128) UNIQUE PRIMARY KEY, 
     53        value BLOB 
     54    )""" 
     55 
     56    create_auth_sql = 'INSERT INTO %(settings)s VALUES ("auth_key", %%s);' 
     57    get_auth_sql = 'SELECT value FROM %(settings)s WHERE setting = "auth_key";' 
     58 
     59    set_assoc_sql = ('REPLACE INTO %(associations)s ' 
     60                     'VALUES (%%s, %%s, %%s, %%s, %%s, %%s);') 
     61    get_assocs_sql = ('SELECT handle, secret, issued, lifetime, assoc_type' 
     62                      ' FROM %(associations)s WHERE server_url = %%s;') 
     63    get_assoc_sql = ( 
     64        'SELECT handle, secret, issued, lifetime, assoc_type' 
     65        ' FROM %(associations)s WHERE server_url = %%s AND handle = %%s;') 
     66    remove_assoc_sql = ('DELETE FROM %(associations)s ' 
     67                        'WHERE server_url = %%s AND handle = %%s;') 
     68 
     69    add_nonce_sql = 'REPLACE INTO %(nonces)s VALUES (%%s, %%s);' 
     70    get_nonce_sql = 'SELECT * FROM %(nonces)s WHERE nonce = %%s;' 
     71    remove_nonce_sql = 'DELETE FROM %(nonces)s WHERE nonce = %%s;' 
     72 
     73    def blobDecode(self, blob): 
     74        """ 
     75        Decode a blob from the database. 
     76        """ 
     77        return str(blob) 
     78 
     79    def blobEncode(self, s): 
     80        """ 
     81        Encode a blob so it can be inserted safely. 
     82        """ 
     83        return buffer(s) 
    2184 
    2285class OpenIDLoginModule(Component): 
     
    3295        full name and email address.""") 
    3396 
     97    # This key is used to store pickled OpenID state information in 
     98    # the trac session. 
    3499    openid_session_key = 'openid_session_data' 
    35100 
    36101    def __init__(self): 
    37         self.lock = thread.allocate_lock() 
     102        db = self.env.get_db_cnx() 
     103        self.store = TracOpenIDStore(db) 
     104        try: 
     105            # Try to create the OpenID store tables. 
     106            self.store.createTables() 
     107        except: 
     108            # Assume they already exist if there was a failure. 
     109            pass 
     110        db.commit() 
    38111 
    39112    # IAuthenticator methods 
     
    106179    def _getConsumer(self, req): 
    107180        s = self._get_session(req) 
    108         return consumer.Consumer(s, self._get_store()), s 
     181        return consumer.Consumer(s, self.store), s 
    109182 
    110183    def _start_login(self, req, url): 
     
    204277    def _get_session(self, req): 
    205278        """Returns a session dict that can store any kind of object.""" 
    206  
    207         # we must be thread-safe 
    208         self.lock.acquire() 
    209279        try: 
    210             session = cPickle.loads(str(req.session[self.openid_session_key])) 
     280            return cPickle.loads(str(req.session[self.openid_session_key])) 
    211281        except KeyError: 
    212             session = {} 
    213         self.lock.release() 
    214  
    215         return session 
     282            return {} 
    216283 
    217284    def _commit_session(self, session, req): 
    218285        req.session[self.openid_session_key] = str(cPickle.dumps(session)) 
    219  
    220     def _get_store(self): 
    221         return dumbstore.DumbStore('afsnjtq4tq9n3klt1gngasd9fasn43')