sqlstore.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. """
  2. This module contains C{L{OpenIDStore}} implementations that use
  3. various SQL databases to back them.
  4. Example of how to initialize a store database::
  5. python -c 'from openid.store import sqlstore; import pysqlite2.dbapi2;'
  6. 'sqlstore.SQLiteStore(pysqlite2.dbapi2.connect("cstore.db")).createTables()'
  7. """
  8. import re
  9. import time
  10. from openid.association import Association
  11. from openid.store.interface import OpenIDStore
  12. from openid.store import nonce
  13. def _inTxn(func):
  14. def wrapped(self, *args, **kwargs):
  15. return self._callInTransaction(func, self, *args, **kwargs)
  16. if hasattr(func, '__name__'):
  17. try:
  18. wrapped.__name__ = func.__name__[4:]
  19. except TypeError:
  20. pass
  21. if hasattr(func, '__doc__'):
  22. wrapped.__doc__ = func.__doc__
  23. return wrapped
  24. class SQLStore(OpenIDStore):
  25. """
  26. This is the parent class for the SQL stores, which contains the
  27. logic common to all of the SQL stores.
  28. The table names used are determined by the class variables
  29. C{L{associations_table}} and
  30. C{L{nonces_table}}. To change the name of the tables used, pass
  31. new table names into the constructor.
  32. To create the tables with the proper schema, see the
  33. C{L{createTables}} method.
  34. This class shouldn't be used directly. Use one of its subclasses
  35. instead, as those contain the code necessary to use a specific
  36. database.
  37. All methods other than C{L{__init__}} and C{L{createTables}}
  38. should be considered implementation details.
  39. @cvar associations_table: This is the default name of the table to
  40. keep associations in
  41. @cvar nonces_table: This is the default name of the table to keep
  42. nonces in.
  43. @sort: __init__, createTables
  44. """
  45. associations_table = 'oid_associations'
  46. nonces_table = 'oid_nonces'
  47. def __init__(self, conn, associations_table=None, nonces_table=None):
  48. """
  49. This creates a new SQLStore instance. It requires an
  50. established database connection be given to it, and it allows
  51. overriding the default table names.
  52. @param conn: This must be an established connection to a
  53. database of the correct type for the SQLStore subclass
  54. you're using.
  55. @type conn: A python database API compatible connection
  56. object.
  57. @param associations_table: This is an optional parameter to
  58. specify the name of the table used for storing
  59. associations. The default value is specified in
  60. C{L{SQLStore.associations_table}}.
  61. @type associations_table: C{str}
  62. @param nonces_table: This is an optional parameter to specify
  63. the name of the table used for storing nonces. The
  64. default value is specified in C{L{SQLStore.nonces_table}}.
  65. @type nonces_table: C{str}
  66. """
  67. self.conn = conn
  68. self.cur = None
  69. self._statement_cache = {}
  70. self._table_names = {
  71. 'associations': associations_table or self.associations_table,
  72. 'nonces': nonces_table or self.nonces_table,
  73. }
  74. self.max_nonce_age = 6 * 60 * 60 # Six hours, in seconds
  75. # DB API extension: search for "Connection Attributes .Error,
  76. # .ProgrammingError, etc." in
  77. # http://www.python.org/dev/peps/pep-0249/
  78. if (hasattr(self.conn, 'IntegrityError') and
  79. hasattr(self.conn, 'OperationalError')):
  80. self.exceptions = self.conn
  81. if not (hasattr(self.exceptions, 'IntegrityError') and
  82. hasattr(self.exceptions, 'OperationalError')):
  83. raise RuntimeError("Error using database connection module "
  84. "(Maybe it can't be imported?)")
  85. def blobDecode(self, blob):
  86. """Convert a blob as returned by the SQL engine into a str object.
  87. str -> str"""
  88. return blob
  89. def blobEncode(self, s):
  90. """Convert a str object into the necessary object for storing
  91. in the database as a blob."""
  92. return s
  93. def _getSQL(self, sql_name):
  94. try:
  95. return self._statement_cache[sql_name]
  96. except KeyError:
  97. sql = getattr(self, sql_name)
  98. sql %= self._table_names
  99. self._statement_cache[sql_name] = sql
  100. return sql
  101. def _execSQL(self, sql_name, *args):
  102. sql = self._getSQL(sql_name)
  103. # Kludge because we have reports of postgresql not quoting
  104. # arguments if they are passed in as unicode instead of str.
  105. # Currently the strings in our tables just have ascii in them,
  106. # so this ought to be safe.
  107. def unicode_to_str(arg):
  108. if isinstance(arg, str):
  109. return str(arg)
  110. else:
  111. return arg
  112. str_args = list(map(unicode_to_str, args))
  113. self.cur.execute(sql, str_args)
  114. def __getattr__(self, attr):
  115. # if the attribute starts with db_, use a default
  116. # implementation that looks up the appropriate SQL statement
  117. # as an attribute of this object and executes it.
  118. if attr[:3] == 'db_':
  119. sql_name = attr[3:] + '_sql'
  120. def func(*args):
  121. return self._execSQL(sql_name, *args)
  122. setattr(self, attr, func)
  123. return func
  124. else:
  125. raise AttributeError('Attribute %r not found' % (attr, ))
  126. def _callInTransaction(self, func, *args, **kwargs):
  127. """Execute the given function inside of a transaction, with an
  128. open cursor. If no exception is raised, the transaction is
  129. comitted, otherwise it is rolled back."""
  130. # No nesting of transactions
  131. self.conn.rollback()
  132. try:
  133. self.cur = self.conn.cursor()
  134. try:
  135. ret = func(*args, **kwargs)
  136. finally:
  137. self.cur.close()
  138. self.cur = None
  139. except:
  140. self.conn.rollback()
  141. raise
  142. else:
  143. self.conn.commit()
  144. return ret
  145. def txn_createTables(self):
  146. """
  147. This method creates the database tables necessary for this
  148. store to work. It should not be called if the tables already
  149. exist.
  150. """
  151. self.db_create_nonce()
  152. self.db_create_assoc()
  153. createTables = _inTxn(txn_createTables)
  154. def txn_storeAssociation(self, server_url, association):
  155. """Set the association for the server URL.
  156. Association -> NoneType
  157. """
  158. a = association
  159. self.db_set_assoc(server_url, a.handle,
  160. self.blobEncode(a.secret), a.issued, a.lifetime,
  161. a.assoc_type)
  162. storeAssociation = _inTxn(txn_storeAssociation)
  163. def txn_getAssociation(self, server_url, handle=None):
  164. """Get the most recent association that has been set for this
  165. server URL and handle.
  166. str -> NoneType or Association
  167. """
  168. if handle is not None:
  169. self.db_get_assoc(server_url, handle)
  170. else:
  171. self.db_get_assocs(server_url)
  172. rows = self.cur.fetchall()
  173. if len(rows) == 0:
  174. return None
  175. else:
  176. associations = []
  177. for values in rows:
  178. values = list(values)
  179. values[1] = self.blobDecode(values[1])
  180. assoc = Association(*values)
  181. if assoc.expiresIn == 0:
  182. self.txn_removeAssociation(server_url, assoc.handle)
  183. else:
  184. associations.append((assoc.issued, assoc))
  185. if associations:
  186. associations.sort()
  187. return associations[-1][1]
  188. else:
  189. return None
  190. getAssociation = _inTxn(txn_getAssociation)
  191. def txn_removeAssociation(self, server_url, handle):
  192. """Remove the association for the given server URL and handle,
  193. returning whether the association existed at all.
  194. (str, str) -> bool
  195. """
  196. self.db_remove_assoc(server_url, handle)
  197. return self.cur.rowcount > 0 # -1 is undefined
  198. removeAssociation = _inTxn(txn_removeAssociation)
  199. def txn_useNonce(self, server_url, timestamp, salt):
  200. """Return whether this nonce is present, and if it is, then
  201. remove it from the set.
  202. str -> bool"""
  203. if abs(timestamp - time.time()) > nonce.SKEW:
  204. return False
  205. try:
  206. self.db_add_nonce(server_url, timestamp, salt)
  207. except self.exceptions.IntegrityError:
  208. # The key uniqueness check failed
  209. return False
  210. else:
  211. # The nonce was successfully added
  212. return True
  213. useNonce = _inTxn(txn_useNonce)
  214. def txn_cleanupNonces(self):
  215. self.db_clean_nonce(int(time.time()) - nonce.SKEW)
  216. return self.cur.rowcount
  217. cleanupNonces = _inTxn(txn_cleanupNonces)
  218. def txn_cleanupAssociations(self):
  219. self.db_clean_assoc(int(time.time()))
  220. return self.cur.rowcount
  221. cleanupAssociations = _inTxn(txn_cleanupAssociations)
  222. class SQLiteStore(SQLStore):
  223. """
  224. This is an SQLite-based specialization of C{L{SQLStore}}.
  225. To create an instance, see C{L{SQLStore.__init__}}. To create the
  226. tables it will use, see C{L{SQLStore.createTables}}.
  227. All other methods are implementation details.
  228. """
  229. create_nonce_sql = """
  230. CREATE TABLE %(nonces)s (
  231. server_url VARCHAR,
  232. timestamp INTEGER,
  233. salt CHAR(40),
  234. UNIQUE(server_url, timestamp, salt)
  235. );
  236. """
  237. create_assoc_sql = """
  238. CREATE TABLE %(associations)s
  239. (
  240. server_url VARCHAR(2047),
  241. handle VARCHAR(255),
  242. secret BLOB(128),
  243. issued INTEGER,
  244. lifetime INTEGER,
  245. assoc_type VARCHAR(64),
  246. PRIMARY KEY (server_url, handle)
  247. );
  248. """
  249. set_assoc_sql = ('INSERT OR REPLACE INTO %(associations)s '
  250. '(server_url, handle, secret, issued, '
  251. 'lifetime, assoc_type) '
  252. 'VALUES (?, ?, ?, ?, ?, ?);')
  253. get_assocs_sql = ('SELECT handle, secret, issued, lifetime, assoc_type '
  254. 'FROM %(associations)s WHERE server_url = ?;')
  255. get_assoc_sql = (
  256. 'SELECT handle, secret, issued, lifetime, assoc_type '
  257. 'FROM %(associations)s WHERE server_url = ? AND handle = ?;')
  258. get_expired_sql = ('SELECT server_url '
  259. 'FROM %(associations)s WHERE issued + lifetime < ?;')
  260. remove_assoc_sql = ('DELETE FROM %(associations)s '
  261. 'WHERE server_url = ? AND handle = ?;')
  262. clean_assoc_sql = 'DELETE FROM %(associations)s WHERE issued + lifetime < ?;'
  263. add_nonce_sql = 'INSERT INTO %(nonces)s VALUES (?, ?, ?);'
  264. clean_nonce_sql = 'DELETE FROM %(nonces)s WHERE timestamp < ?;'
  265. def blobEncode(self, s):
  266. return memoryview(s)
  267. def useNonce(self, *args, **kwargs):
  268. # Older versions of the sqlite wrapper do not raise
  269. # IntegrityError as they should, so we have to detect the
  270. # message from the OperationalError.
  271. try:
  272. return super(SQLiteStore, self).useNonce(*args, **kwargs)
  273. except self.exceptions.OperationalError as why:
  274. if re.match('^columns .* are not unique$', str(why)):
  275. return False
  276. else:
  277. raise
  278. class MySQLStore(SQLStore):
  279. """
  280. This is a MySQL-based specialization of C{L{SQLStore}}.
  281. Uses InnoDB tables for transaction support.
  282. To create an instance, see C{L{SQLStore.__init__}}. To create the
  283. tables it will use, see C{L{SQLStore.createTables}}.
  284. All other methods are implementation details.
  285. """
  286. try:
  287. import MySQLdb as exceptions
  288. except ImportError:
  289. exceptions = None
  290. create_nonce_sql = """
  291. CREATE TABLE %(nonces)s (
  292. server_url BLOB NOT NULL,
  293. timestamp INTEGER NOT NULL,
  294. salt CHAR(40) NOT NULL,
  295. PRIMARY KEY (server_url(255), timestamp, salt)
  296. )
  297. ENGINE=InnoDB;
  298. """
  299. create_assoc_sql = """
  300. CREATE TABLE %(associations)s
  301. (
  302. server_url BLOB NOT NULL,
  303. handle VARCHAR(255) NOT NULL,
  304. secret BLOB NOT NULL,
  305. issued INTEGER NOT NULL,
  306. lifetime INTEGER NOT NULL,
  307. assoc_type VARCHAR(64) NOT NULL,
  308. PRIMARY KEY (server_url(255), handle)
  309. )
  310. ENGINE=InnoDB;
  311. """
  312. set_assoc_sql = ('REPLACE INTO %(associations)s '
  313. 'VALUES (%%s, %%s, %%s, %%s, %%s, %%s);')
  314. get_assocs_sql = ('SELECT handle, secret, issued, lifetime, assoc_type'
  315. ' FROM %(associations)s WHERE server_url = %%s;')
  316. get_expired_sql = ('SELECT server_url '
  317. 'FROM %(associations)s WHERE issued + lifetime < %%s;')
  318. get_assoc_sql = (
  319. 'SELECT handle, secret, issued, lifetime, assoc_type'
  320. ' FROM %(associations)s WHERE server_url = %%s AND handle = %%s;')
  321. remove_assoc_sql = ('DELETE FROM %(associations)s '
  322. 'WHERE server_url = %%s AND handle = %%s;')
  323. clean_assoc_sql = 'DELETE FROM %(associations)s WHERE issued + lifetime < %%s;'
  324. add_nonce_sql = 'INSERT INTO %(nonces)s VALUES (%%s, %%s, %%s);'
  325. clean_nonce_sql = 'DELETE FROM %(nonces)s WHERE timestamp < %%s;'
  326. class PostgreSQLStore(SQLStore):
  327. """
  328. This is a PostgreSQL-based specialization of C{L{SQLStore}}.
  329. To create an instance, see C{L{SQLStore.__init__}}. To create the
  330. tables it will use, see C{L{SQLStore.createTables}}.
  331. All other methods are implementation details.
  332. """
  333. try:
  334. import psycopg2
  335. except ImportError:
  336. from psycopg2cffi import compat
  337. compat.register()
  338. exceptions = None
  339. create_nonce_sql = """
  340. CREATE TABLE %(nonces)s (
  341. server_url VARCHAR(2047) NOT NULL,
  342. timestamp INTEGER NOT NULL,
  343. salt CHAR(40) NOT NULL,
  344. PRIMARY KEY (server_url, timestamp, salt)
  345. );
  346. """
  347. create_assoc_sql = """
  348. CREATE TABLE %(associations)s
  349. (
  350. server_url VARCHAR(2047) NOT NULL,
  351. handle VARCHAR(255) NOT NULL,
  352. secret BYTEA NOT NULL,
  353. issued INTEGER NOT NULL,
  354. lifetime INTEGER NOT NULL,
  355. assoc_type VARCHAR(64) NOT NULL,
  356. PRIMARY KEY (server_url, handle),
  357. CONSTRAINT secret_length_constraint CHECK (LENGTH(secret) <= 128)
  358. );
  359. """
  360. def db_set_assoc(self, server_url, handle, secret, issued, lifetime,
  361. assoc_type):
  362. """
  363. Set an association. This is implemented as a method because
  364. REPLACE INTO is not supported by PostgreSQL (and is not
  365. standard SQL).
  366. """
  367. result = self.db_get_assoc(server_url, handle)
  368. rows = self.cur.fetchall()
  369. if len(rows):
  370. # Update the table since this associations already exists.
  371. return self.db_update_assoc(secret, issued, lifetime, assoc_type,
  372. server_url, handle)
  373. else:
  374. # Insert a new record because this association wasn't
  375. # found.
  376. return self.db_new_assoc(server_url, handle, secret, issued,
  377. lifetime, assoc_type)
  378. new_assoc_sql = ('INSERT INTO %(associations)s '
  379. 'VALUES (%%s, %%s, %%s, %%s, %%s, %%s);')
  380. update_assoc_sql = ('UPDATE %(associations)s SET '
  381. 'secret = %%s, issued = %%s, '
  382. 'lifetime = %%s, assoc_type = %%s '
  383. 'WHERE server_url = %%s AND handle = %%s;')
  384. get_assocs_sql = ('SELECT handle, secret, issued, lifetime, assoc_type'
  385. ' FROM %(associations)s WHERE server_url = %%s;')
  386. get_expired_sql = ('SELECT server_url '
  387. 'FROM %(associations)s WHERE issued + lifetime < %%s;')
  388. get_assoc_sql = (
  389. 'SELECT handle, secret, issued, lifetime, assoc_type'
  390. ' FROM %(associations)s WHERE server_url = %%s AND handle = %%s;')
  391. remove_assoc_sql = ('DELETE FROM %(associations)s '
  392. 'WHERE server_url = %%s AND handle = %%s;')
  393. clean_assoc_sql = 'DELETE FROM %(associations)s WHERE issued + lifetime < %%s;'
  394. add_nonce_sql = 'INSERT INTO %(nonces)s VALUES (%%s, %%s, %%s);'
  395. clean_nonce_sql = 'DELETE FROM %(nonces)s WHERE timestamp < %%s;'
  396. def blobEncode(self, blob):
  397. from psycopg2 import Binary
  398. return Binary(blob)
  399. def blobDecode(self, blob):
  400. return blob.tobytes()