Modified:
trunk/Lux/Auth/Adapter/Psql.php
Log:
Lux_Auth_Adapter_Psql: [FIX] Now deletes token from database in
_processLogout(). Thanks, Raymond Kolbe.
* Moved cookie parsing into a method
Modified: trunk/Lux/Auth/Adapter/Psql.php
==============================================================================
--- trunk/Lux/Auth/Adapter/Psql.php (original)
+++ trunk/Lux/Auth/Adapter/Psql.php Sun Apr 27 09:17:05 2008
@@ -190,111 +190,106 @@
protected function _processCookieLogin()
{
// get cookie
- $cookie =
$this->_request->cookie($this->_config['cookie_name'], false);
+ $cookie = $this->_getCookie();
- if ($cookie) {
- // parse cookie
- list($identifier, $token) = explode(':', $cookie);
+ if (! $cookie) {
+ // no cookie, or could not parse cookie
+ return false;
+ }
+
+ // get a selection tool using the dependency object
+ $select = Solar::factory(
+ 'Solar_Sql_Select',
+ array('sql' => $this->_sql)
+ );
+
+ $identifier_col = $this->_config['token_identifier_col'];
+ $token_col = $this->_config['token_token_col'];
+ $timeout_col = $this->_config['token_timeout_col'];
+
+ // build select
+ $select->from($this->_config['token_table'])
+ ->cols(array($this->_config['token_handle_col']))
+ ->multiWhere(array(
+ "$identifier_col = ?" => $cookie['identifier'],
+ "$token_col = ?" => $cookie['token'],
+ "$timeout_col > ?" => time(),
+ ));
+
+ // fetch one row
+ $token_found = $select->fetch('one');
+
+ if ($token_found) {
+
+ // now we need to fetch info from auth table
+
+ $cols = array();
+
+ // always fetch the handle
+ $cols[] = $this->_config['handle_col'];
+
+ // list of optional columns as (property => field)
+ $optional = array(
+ 'email' => 'email_col',
+ 'moniker' => 'moniker_col',
+ 'uri' => 'uri_col',
+ 'uid' => 'uid_col',
+ );
- // sanity check
- if (empty($identifier) || empty($token)) {
- return false;
+ // get optional columns
+ foreach ($optional as $key => $val) {
+ if ($this->_config[$val]) {
+ $cols[] = $this->_config[$val];
+ }
}
+ // use user handle from the token table
+ $handle = $token_found[$this->_config['token_handle_col']];
+
// get a selection tool using the dependency object
$select = Solar::factory(
'Solar_Sql_Select',
array('sql' => $this->_sql)
);
- $identifier_col = $this->_config['token_identifier_col'];
- $token_col = $this->_config['token_token_col'];
- $timeout_col = $this->_config['token_timeout_col'];
-
- // build select
- $select->from($this->_config['token_table'])
- ->cols(array($this->_config['token_handle_col']))
- ->multiWhere(array(
- "$identifier_col = ?" => $identifier,
- "$token_col = ?" => $token,
- "$timeout_col > ?" => time(),
- ));
-
- // fetch one row
- $token_found = $select->fetch('one');
-
- if ($token_found) {
+ // build the select
+ $select->from($this->_config['table'])
+ ->cols($cols)
+ ->where("{$this->_config['handle_col']} = ?", $handle)
+ ->multiWhere($this->_config['where'])
+ ->limit(2);
+
+ // fetch all
+ $rows = $select->fetchAll();
+
+ // did we found this user in the auth table?
+ if (count($rows) == 1) {
+
+ // remove old token
+ $this->_deleteToken($cookie['token']);
- // now we need to fetch info from auth table
+ // make a new token and set the cookie
+ $this->_newCookie($handle);
- $cols = array();
+ // set base info
+ $info = array('handle' => $handle);
- // always fetch the handle
- $cols[] = $this->_config['handle_col'];
-
- // list of optional columns as (property => field)
- $optional = array(
- 'email' => 'email_col',
- 'moniker' => 'moniker_col',
- 'uri' => 'uri_col',
- 'uid' => 'uid_col',
- );
-
- // get optional columns
+ // set optional info from optional cols
+ $row = current($rows);
foreach ($optional as $key => $val) {
if ($this->_config[$val]) {
- $cols[] = $this->_config[$val];
+ $info[$key] = $row[$this->_config[$val]];
}
}
- // use user handle from the token table
- $handle = $token_found[$this->_config['token_handle_col']];
-
- // get a selection tool using the dependency object
- $select = Solar::factory(
- 'Solar_Sql_Select',
- array('sql' => $this->_sql)
- );
-
- // build the select
- $select->from($this->_config['table'])
- ->cols($cols)
- ->where("{$this->_config['handle_col']} = ?", $handle)
- ->multiWhere($this->_config['where'])
- ->limit(2);
-
- // fetch all
- $rows = $select->fetchAll();
-
- // user that used a cookie was found in the real auth table
- if (count($rows) == 1) {
-
- // remove old token
- $this->_deleteToken($token);
-
- // make a new token and set the cookie
- $this->_newCookie($handle);
-
- // set base info
- $info = array('handle' => $handle);
-
- // set optional info from optional cols
- $row = current($rows);
- foreach ($optional as $key => $val) {
- if ($this->_config[$val]) {
- $info[$key] = $row[$this->_config[$val]];
- }
- }
-
- // successful login, treat result as user info
- $this->reset('VALID', $info);
- return true;
-
- } else {
- // user that used a cookied was **not** found
- // in the real auth table. fail authentication!
- return false;
- }
+ // successful login, treat result as user info
+ $this->reset('VALID', $info);
+ return true;
+
+ } else {
+ // user that used a cookie was **not** found
+ // in the real auth table. fail authentication!
+ return false;
}
}
@@ -356,19 +351,27 @@
}
/**
- *
+ *
* Adapter-specific logout processing.
- *
+ *
* @return string A status code string for reset().
- *
+ *
*/
protected function _processLogout()
{
// first, log us out
$status = parent::_processLogout();
- // delete auth cookie
- $this->_setCookie('DELETED', time());
+ // get cookie
+ $cookie = $this->_getCookie();
+
+ if ($cookie) {
+ // remove token from database
+ $this->_deteleToken($cookie['token']);
+
+ // delete auth cookie
+ $this->_setCookie('DELETED', time());
+ }
// return status from parent
return $status;
@@ -396,6 +399,37 @@
$this->_config['cookie_domain'],
$this->_config['cookie_secure'],
$this->_config['cookie_httponly']
+ );
+ }
+
+ /**
+ *
+ * Gets cookie and breaks it down into an identifier and a token
+ *
+ * @return mixed Array of identifier and token,
+ * otherwise boolean false
+ *
+ */
+ protected function _getCookie()
+ {
+ // get cookie
+ $cookie =
$this->_request->cookie($this->_config['cookie_name'], false);
+
+ if (! $cookie) {
+ return false;
+ }
+
+ // parse cookie
+ list($identifier, $token) = explode(':', $cookie);
+
+ // sanity check
+ if (empty($identifier) || empty($token)) {
+ return false;
+ }
+
+ return array(
+ 'identifier' => $identifier,
+ 'token' => $token,
);
}