summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortailor <cygnus@janrain.com>2007-03-02 22:56:05 +0000
committertailor <cygnus@janrain.com>2007-03-02 22:56:05 +0000
commit9a50f5d16036586895b50ec56bc030e0e6ae1fbc (patch)
tree3ca0fd07f2bfb05d6d4561f39535abc1c413fc05
parent4a279473ce570b1e639e4c0fc9d64528ec9a9291 (diff)
downloadphp-openid-9a50f5d16036586895b50ec56bc030e0e6ae1fbc.zip
php-openid-9a50f5d16036586895b50ec56bc030e0e6ae1fbc.tar.gz
php-openid-9a50f5d16036586895b50ec56bc030e0e6ae1fbc.tar.bz2
[project @ Add getExpired to stores]
-rw-r--r--Auth/OpenID/Association.php2
-rw-r--r--Auth/OpenID/FileStore.php66
-rw-r--r--Auth/OpenID/Interface.php9
-rw-r--r--Auth/OpenID/MySQLStore.php3
-rw-r--r--Auth/OpenID/PostgreSQLStore.php3
-rw-r--r--Auth/OpenID/SQLStore.php18
-rw-r--r--Auth/OpenID/SQLiteStore.php3
-rw-r--r--Tests/Auth/OpenID/MemStore.php26
-rw-r--r--Tests/Auth/OpenID/StoreTest.php1
9 files changed, 110 insertions, 21 deletions
diff --git a/Auth/OpenID/Association.php b/Auth/OpenID/Association.php
index 89bc0cd..df14e15 100644
--- a/Auth/OpenID/Association.php
+++ b/Auth/OpenID/Association.php
@@ -221,7 +221,7 @@ class Auth_OpenID_Association {
sort($class_assoc_keys);
if ($keys != $class_assoc_keys) {
- trigger_error('Unexpected key values: ' . strval($keys),
+ trigger_error('Unexpected key values: ' . var_export($keys, true),
E_USER_WARNING);
return null;
}
diff --git a/Auth/OpenID/FileStore.php b/Auth/OpenID/FileStore.php
index 9a24030..9639611 100644
--- a/Auth/OpenID/FileStore.php
+++ b/Auth/OpenID/FileStore.php
@@ -373,23 +373,9 @@ class Auth_OpenID_FileStore extends Auth_OpenID_OpenIDStore {
* Remove expired entries from the database. This is potentially
* expensive, so only run when it is acceptable to take time.
*/
- function clean()
+ function _allAssocs()
{
- if (!$this->active) {
- trigger_error("FileStore no longer active", E_USER_ERROR);
- return null;
- }
-
- $nonces = Auth_OpenID_FileStore::_listdir($this->nonce_dir);
- $now = time();
-
- // Check all nonces for expiry
- foreach ($nonces as $nonce) {
- if (!Auth_OpenID_checkTimestamp($nonce, $now)) {
- $filename = $this->nonce_dir . DIRECTORY_SEPARATOR . $nonce;
- Auth_OpenID_FileStore::_removeIfPresent($filename);
- }
- }
+ $all_associations = array();
$association_filenames =
Auth_OpenID_FileStore::_listdir($this->association_dir);
@@ -412,12 +398,52 @@ class Auth_OpenID_FileStore extends Auth_OpenID_OpenIDStore {
$association_filename);
} else {
if ($association->getExpiresIn() == 0) {
- Auth_OpenID_FileStore::_removeIfPresent(
- $association_filename);
+ $all_associations[] = array($association_filename,
+ $association);
}
}
}
}
+
+ return $all_associations;
+ }
+
+ function clean()
+ {
+ if (!$this->active) {
+ trigger_error("FileStore no longer active", E_USER_ERROR);
+ return null;
+ }
+
+ $nonces = Auth_OpenID_FileStore::_listdir($this->nonce_dir);
+ $now = time();
+
+ // Check all nonces for expiry
+ foreach ($nonces as $nonce) {
+ if (!Auth_OpenID_checkTimestamp($nonce, $now)) {
+ $filename = $this->nonce_dir . DIRECTORY_SEPARATOR . $nonce;
+ Auth_OpenID_FileStore::_removeIfPresent($filename);
+ }
+ }
+
+ foreach ($this->_allAssocs() as $pair) {
+ list($assoc_filename, $assoc) = $pair;
+ if ($assoc->getExpiresIn() == 0) {
+ Auth_OpenID_FileStore::_removeIfPresent($assoc_filename);
+ }
+ }
+ }
+
+ function getExpired()
+ {
+ $urls = array();
+ foreach ($this->_allAssocs() as $pair) {
+ list($_, $assoc) = $pair;
+ if ($assoc->getExpiresIn() <= 0) {
+ $urls[] = $assoc->server_url;
+ }
+ }
+ return $urls;
}
/**
@@ -498,7 +524,9 @@ class Auth_OpenID_FileStore extends Auth_OpenID_OpenIDStore {
$handle = opendir($dir);
$files = array();
while (false !== ($filename = readdir($handle))) {
- $files[] = $filename;
+ if (!in_array($filename, array('.', '..'))) {
+ $files[] = $filename;
+ }
}
return $files;
}
diff --git a/Auth/OpenID/Interface.php b/Auth/OpenID/Interface.php
index 33e9797..ff460e9 100644
--- a/Auth/OpenID/Interface.php
+++ b/Auth/OpenID/Interface.php
@@ -133,6 +133,15 @@ class Auth_OpenID_OpenIDStore {
}
/**
+ * Return all server URLs that have expired associations.
+ */
+ function getExpired()
+ {
+ trigger_error("Auth_OpenID_OpenIDStore::getExpired ".
+ "not implemented", E_USER_ERROR);
+ }
+
+ /**
* Removes all entries from the store; implementation is optional.
*/
function reset()
diff --git a/Auth/OpenID/MySQLStore.php b/Auth/OpenID/MySQLStore.php
index 14a695c..4b2d29d 100644
--- a/Auth/OpenID/MySQLStore.php
+++ b/Auth/OpenID/MySQLStore.php
@@ -57,6 +57,9 @@ class Auth_OpenID_MySQLStore extends Auth_OpenID_SQLStore {
$this->sql['add_nonce'] =
"INSERT INTO %s (server_url, timestamp, salt) VALUES (?, ?, ?)";
+
+ $this->sql['get_expired'] =
+ "SELECT server_url FROM %s WHERE issued + lifetime < ?";
}
/**
diff --git a/Auth/OpenID/PostgreSQLStore.php b/Auth/OpenID/PostgreSQLStore.php
index cf0a5ef..ffbbc69 100644
--- a/Auth/OpenID/PostgreSQLStore.php
+++ b/Auth/OpenID/PostgreSQLStore.php
@@ -54,6 +54,9 @@ class Auth_OpenID_PostgreSQLStore extends Auth_OpenID_SQLStore {
$this->sql['remove_assoc'] =
"DELETE FROM %s WHERE server_url = ? AND handle = ?";
+ $this->sql['get_expired'] =
+ "SELECT server_url FROM %s WHERE issued + lifetime < ?";
+
$this->sql['add_nonce'] =
"INSERT INTO %s (server_url, timestamp, salt) VALUES ".
"(?, ?, ?)"
diff --git a/Auth/OpenID/SQLStore.php b/Auth/OpenID/SQLStore.php
index 2f775a1..5829f18 100644
--- a/Auth/OpenID/SQLStore.php
+++ b/Auth/OpenID/SQLStore.php
@@ -220,6 +220,7 @@ class Auth_OpenID_SQLStore extends Auth_OpenID_OpenIDStore {
'get_assoc',
'get_assocs',
'remove_assoc',
+ 'get_expired',
);
foreach ($required_sql_keys as $key) {
@@ -250,7 +251,8 @@ class Auth_OpenID_SQLStore extends Auth_OpenID_OpenIDStore {
'set_assoc',
'get_assoc',
'get_assocs',
- 'remove_assoc')
+ 'remove_assoc',
+ 'get_expired')
)
);
@@ -392,6 +394,20 @@ class Auth_OpenID_SQLStore extends Auth_OpenID_OpenIDStore {
return true;
}
+ function getExpired()
+ {
+ $sql = $this->sql['get_expired'];
+ $result = $this->connection->getAll($sql, array(time()));
+
+ $expired = array();
+
+ foreach ($result as $row) {
+ $expired[] = $row['server_url'];
+ }
+
+ return $expired;
+ }
+
function getAssociation($server_url, $handle = null)
{
if ($handle !== null) {
diff --git a/Auth/OpenID/SQLiteStore.php b/Auth/OpenID/SQLiteStore.php
index 130f8fa..95c4d1d 100644
--- a/Auth/OpenID/SQLiteStore.php
+++ b/Auth/OpenID/SQLiteStore.php
@@ -35,6 +35,9 @@ class Auth_OpenID_SQLiteStore extends Auth_OpenID_SQLStore {
"SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
"WHERE server_url = ?";
+ $this->sql['get_expired'] =
+ "SELECT server_url FROM %s WHERE issued + lifetime < ?";
+
$this->sql['get_assoc'] =
"SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
"WHERE server_url = ? AND handle = ?";
diff --git a/Tests/Auth/OpenID/MemStore.php b/Tests/Auth/OpenID/MemStore.php
index c9141f7..35f6a77 100644
--- a/Tests/Auth/OpenID/MemStore.php
+++ b/Tests/Auth/OpenID/MemStore.php
@@ -20,6 +20,32 @@ class Tests_Auth_OpenID_MemStore extends Auth_OpenID_OpenIDStore {
return serialize(array($server_url, $handle));
}
+ function getBest($assoc_list)
+ {
+ $best = null;
+ foreach ($assoc_list as $assoc) {
+ if (($best === null) ||
+ ($best->issued < $assoc->issued)) {
+ $best = $assoc;
+ }
+ }
+ return $best;
+ }
+
+ function getExpired()
+ {
+ $expired = array();
+ foreach ($this->assocs as $url => $assocs) {
+ $best = $this->getBest($assocs);
+ if (($best === null) ||
+ ($best->getExpiresIn() == 0)) {
+ $expired[] = $server_url;
+ }
+ }
+
+ return $expired;
+ }
+
function getAssocPairs()
{
$pairs = array();
diff --git a/Tests/Auth/OpenID/StoreTest.php b/Tests/Auth/OpenID/StoreTest.php
index a07255b..195a531 100644
--- a/Tests/Auth/OpenID/StoreTest.php
+++ b/Tests/Auth/OpenID/StoreTest.php
@@ -123,6 +123,7 @@ class Tests_Auth_OpenID_StoreTest extends PHPUnit_TestCase {
*/
function _testStore($store)
{
+ $this->assertTrue($store->getExpired() === array());
// Association functions
$now = time();