? tokenauth.97820.patch
Index: tokenauth.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tokenauth/Attic/tokenauth.install,v
retrieving revision 1.3.4.11
diff -u -p -r1.3.4.11 tokenauth.install
--- tokenauth.install	27 Sep 2010 17:06:22 -0000	1.3.4.11
+++ tokenauth.install	27 Sep 2010 21:12:24 -0000
@@ -11,27 +11,36 @@
  */
 function tokenauth_schema() {
   $schema['tokenauth_tokens'] = array(
-   'description'     => 'Stores information about each user\'s token',
-   'fields'          => array(
-     'uid'             => array(
-       'description'     => 'The user\'s {users}.uid',
-       'type'            => 'int',
-       'size'            => 'normal',
-       'not null'        => TRUE,
-     ),
-     'token'        => array(
-       'description'     => 'The user specific token',
-       'type'            => 'varchar',
-       'length'          => 50,
-       'not null'        => TRUE,
-     ),
-   ),
-   'primary key'       => array('token'),
-   'unique keys'         => array(
-     'uid_key'           => array('uid')
-   ),
+    'description'     => 'Stores information about each user\'s token',
+    'fields'          => array(
+      'uid'             => array(
+        'description'     => 'The user\'s {users}.uid',
+        'type'            => 'int',
+        'size'            => 'normal',
+        'not null'        => TRUE,
+      ),
+      'token'        => array(
+        'description'     => 'The user specific token',
+        'type'            => 'varchar',
+        'length'          => 50,
+        'not null'        => TRUE,
+      ),
+      'issued'       => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+        'description' => t('Record the Unix timestamp the token was issued.'),
+      ),
+    ),
+    'primary key'       => array('token'),
+    'unique keys'         => array(
+      'uid_key'           => array('uid')
+    ),
+    'indexes' => array(
+      'issued' => array('issued'), 
+    ),
   );
- return $schema;
+  return $schema;
 }
 
 /**
@@ -65,4 +74,23 @@ function tokenauth_uninstall() {
   variable_del('tokenauth_pages');
   variable_del('tokenauth_reset');
   variable_del('tokenauth_text');
+  variable_del('tokenauth_lifespan');
 }
+
+/**
+ * Add date of issue for each token.
+ */
+function tokenauth_update_6006() {
+  $ret = array();
+  $spec = array(
+    'type' => 'int',
+    'not null' => TRUE,
+    'default' => 0,
+    'description' => t('Record the Unix timestamp the token was issued.'),
+  );
+  db_add_field($ret, 'tokenauth_tokens', 'issued', $spec, array(
+    'indexes' => array('issued' => array('issued'))
+  ));
+  db_query("UPDATE {tokenauth_tokens} SET issued=%d", time());
+  return $ret;
+}
\ No newline at end of file
Index: tokenauth.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tokenauth/Attic/tokenauth.module,v
retrieving revision 1.11.4.30
diff -u -p -r1.11.4.30 tokenauth.module
--- tokenauth.module	23 Sep 2010 17:10:01 -0000	1.11.4.30
+++ tokenauth.module	27 Sep 2010 21:12:24 -0000
@@ -167,6 +167,22 @@ function tokenauth_user($op, &$edit, &$a
   }
 }
 
+/**
+ * Implementation of hook_cron().
+ */
+function tokenauth_cron() {
+watchdog('tokenauth', 'Running cron for tokenauth');
+  $lifespan = variable_get('tokenauth_lifespan', 0);
+  if (isset($lifespan) && $lifespan > 0) {
+    $expire = time() - $lifespan;
+    $result = db_query("SELECT uid FROM {tokenauth_tokens} WHERE issued < %d", $expire);
+    while ($row = db_fetch_object($result)) {
+      tokenauth_reset_user($row->uid);
+      watchdog('tokenauth', 'Generating new token for user !uid.', array('!uid' => $row->uid));
+    }
+  }
+}
+
 /// Token Integration ///
 
 /**
@@ -281,10 +297,11 @@ function tokenauth_reset_user($uid = NUL
   $entry = array(
     'uid' => $uid,
     'token' => isset($token) ? $token : user_password(variable_get('tokenauth_length', 10)),
+    'issued' => _tokenauth_get_timestamp(),
   );
   if (!$update) {
     // drupal_write_record mysteriously failing from tokenauth_enable().
-    $return = db_query("INSERT INTO {tokenauth_tokens} (uid,token) VALUES (%d,'%s')", $entry['uid'], $entry['token']);
+    $return = db_query("INSERT INTO {tokenauth_tokens} (uid,token,issued) VALUES (%d,'%s', %d)", $entry['uid'], $entry['token'], $entry['issued']);
   }
   else {
     $return = drupal_write_record('tokenauth_tokens', $entry, 'uid');
@@ -345,6 +362,17 @@ function tokenauth_get_user($token) {
 }
 
 /**
+ * Helper function to grab the request timestamp.
+ */
+function _tokenauth_get_timestamp() {
+  static $timestamp;
+  if (empty($timestamp)) {
+    $timestamp = time();
+  }
+  return $timestamp;
+}
+
+/**
  * Helper function to build a token reset form.
  */
 function tokenauth_reset_user_form($uid, $label = NULL, $token = NULL) {
Index: tokenauth.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tokenauth/Attic/tokenauth.pages.inc,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 tokenauth.pages.inc
--- tokenauth.pages.inc	2 Sep 2010 21:10:57 -0000	1.1.2.6
+++ tokenauth.pages.inc	27 Sep 2010 21:12:24 -0000
@@ -31,6 +31,15 @@ function tokenauth_admin_settings() {
     '#default_value' => variable_get('tokenauth_pages', "rss.xml\n*/feed\n*/opml"),
     '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. Read <a href=\"http://api.drupal.org/api/function/drupal_match_path/6\">drupal_match_path()</a> for the details.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
   );
+  $form['tokenauth_general']['tokenauth_lifespan'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Token lifespan'),
+    '#description' => t('Specify the number of days before the token is automatically reset. Leave as "0" for unlimited.'),
+    '#default_value' => _tokenauth_convert_time_unit(variable_get('tokenauth_lifespan', 0), FALSE),
+    '#size' => 4,
+    '#maxlength' => 4,
+    '#required' => TRUE,
+  );
 
   $form['tokenauth_text'] = array(
     '#type' => 'fieldset',
@@ -84,6 +93,11 @@ function tokenauth_admin_settings_valida
   if ($form_state['values']['tokenauth_length'] > 33) {
     form_set_error('tokenauth_length', t('The maximum token length is 32.'));
   }
+  if (!is_numeric($form_state['values']['tokenauth_lifespan']) || $form_state['values']['tokenauth_lifespan'] < 0) {
+    form_set_error('tokenauth_lifespan', t('You must enter a number of "0" or greater.'));
+  }
+  // Convert to Unixtime in admin pages to save performance on cron runs.
+  $form_state['values']['tokenauth_lifespan'] = _tokenauth_convert_time_unit($form_state['values']['tokenauth_lifespan']);
 }
 
 /**
@@ -142,3 +156,24 @@ function tokenauth_user_profile_form(&$f
   $form = array_merge($form, tokenauth_reset_user_form($account->uid, NULL, $token));
   return $form;
 }
+
+/**
+ * Convert to/from unixtime to facilitate UI in "days".
+ *
+ * This is done in the administration interface to save performance on cron.
+ *
+ * @param $time
+ *  Time before tokens should expire. May be in seconds or days dependening on whether it
+ *  is going into the database or coming out.
+ * @param $seconds
+ *  If TRUE, will convert the $time measurement into seconds, suitable for unixtime comparison.
+ *
+ * @return
+ *  Total lifespan in seconds or days before tokens should be expired.
+ */
+function _tokenauth_convert_time_unit($time, $seconds = TRUE) {
+  if ($seconds) {
+    return $time * 86400;
+  }
+  return $time / 86400;
+}
