Make it possible to use PluginLoader without store
[cascardo/ipsilon.git] / ipsilon / providers / openid / store.py
old mode 100755 (executable)
new mode 100644 (file)
index 66e0503..bf2898c
@@ -1,8 +1,6 @@
-#!/usr/bin/python
-#
-# Copyright (C) 2014  Ipsilon project Contributors, for licensee see COPYING
+# Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
 
-from ipsilon.util.data import Store
+from ipsilon.util.data import Store, UNIQUE_DATA_TABLE, OPTIONS_TABLE
 
 from openid import oidutil
 from openid.association import Association
@@ -67,15 +65,50 @@ class OpenIDStore(Store, OpenIDStoreInterface):
 
         return True
 
+    def _cleanup(self):
+        res1 = self.cleanupNonces()
+        res2 = self.cleanupAssociations()
+        return res1 + res2
+
     def cleanupNonces(self):
         nonces = self.get_unique_data('nonce')
+        cleaned = 0
         for iden in nonces:
             if nonces[iden]['timestamp'] < (time() - NonceSKEW):
+                cleaned += 1
                 self.del_unique_data('nonce', iden)
+        return cleaned
 
     def cleanupAssociations(self):
         assocs = self.get_unique_data('association')
+        cleaned = 0
         for iden in assocs:
             if ((int(assocs[iden]['issued']) + int(assocs[iden]['lifetime']))
                     < time()):
+                cleaned += 1
                 self.del_unique_data('association', iden)
+        return cleaned
+
+    def _initialize_schema(self):
+        q = self._query(self._db, 'association', UNIQUE_DATA_TABLE,
+                        trans=False)
+        q.create()
+        q._con.close()  # pylint: disable=protected-access
+
+    def _upgrade_schema(self, old_version):
+        if old_version == 1:
+            # In schema version 2, we added indexes and primary keys
+            # pylint: disable=protected-access
+            table = self._query(self._db, 'association', UNIQUE_DATA_TABLE,
+                                trans=False)._table
+            self._db.add_constraint(table.primary_key)
+            for index in table.indexes:
+                self._db.add_index(index)
+            table = self._query(self._db, 'openid_extensions', OPTIONS_TABLE,
+                                trans=False)._table
+            self._db.add_constraint(table.primary_key)
+            for index in table.indexes:
+                self._db.add_index(index)
+            return 2
+        else:
+            raise NotImplementedError()