Changeset 1723
- Timestamp:
- 12/20/06 23:04:00 (2 years ago)
- Files:
-
- openidplugin/trunk/openidauth/auth.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openidplugin/trunk/openidauth/auth.py
r1722 r1723 1 1 # Copyright 2006, Waldemar Kornewald <wkornew@gmx.net> 2 # with modifications by Jonathan Daugherty <cygnus@janrain.com> 2 3 # Distributed under the terms of the MIT License. 3 4 … … 7 8 import cPickle 8 9 9 from openid.store import dumbstore10 from openid.store import sqlstore 10 11 from openid.consumer import consumer 11 12 from yadis.discover import DiscoveryFailure … … 19 20 from trac.util import escape, hex_entropy, TracError, Markup 20 21 22 class 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) 21 84 22 85 class OpenIDLoginModule(Component): … … 32 95 full name and email address.""") 33 96 97 # This key is used to store pickled OpenID state information in 98 # the trac session. 34 99 openid_session_key = 'openid_session_data' 35 100 36 101 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() 38 111 39 112 # IAuthenticator methods … … 106 179 def _getConsumer(self, req): 107 180 s = self._get_session(req) 108 return consumer.Consumer(s, self. _get_store()), s181 return consumer.Consumer(s, self.store), s 109 182 110 183 def _start_login(self, req, url): … … 204 277 def _get_session(self, req): 205 278 """Returns a session dict that can store any kind of object.""" 206 207 # we must be thread-safe208 self.lock.acquire()209 279 try: 210 session =cPickle.loads(str(req.session[self.openid_session_key]))280 return cPickle.loads(str(req.session[self.openid_session_key])) 211 281 except KeyError: 212 session = {} 213 self.lock.release() 214 215 return session 282 return {} 216 283 217 284 def _commit_session(self, session, req): 218 285 req.session[self.openid_session_key] = str(cPickle.dumps(session)) 219 220 def _get_store(self):221 return dumbstore.DumbStore('afsnjtq4tq9n3klt1gngasd9fasn43')
