From ab4113168f32c46946b20674c80ec79c19dbccd7 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sat, 2 Nov 2013 20:02:28 +0100 Subject: [PATCH] PuSH 0.4: No outgoing 'sync' verifications. Feed renewal script. No auto-renewal. Among other things (such as permanent subscriptions), Pubsubhubbub 0.4 removed the "sync" verification method. This means that any incoming PuSH subscription requests that follow the 0.4 spec won't really _require_that we handle it as a background process, but if we were to try direct verification of the subscription - and fail - there's no way we could pick up the ball again. So _essentially_ we require background processing with retries. This means we must implement something like the "poorman cron" or similar, so background processing can be handled on-demand/on-site-visit. This is how Friendica, Drupal etc. handles it and is necessary for environments where we can't run separate queue daemons. When the poorman-cron-ish thing is implemented, auto-renewal will work for all users. PuSH 0.4 spec: https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html More on PuSH 0.4 release (incl. breaking changes): https://groups.google.com/forum/#!msg/pubsubhubbub/7RPlYMds4RI/2mIHQTdV3aoJ --- plugins/OStatus/actions/pushcallback.php | 12 ++---- plugins/OStatus/actions/pushhub.php | 24 ++++------- plugins/OStatus/classes/FeedSub.php | 51 ++++++++++++++---------- plugins/OStatus/classes/HubSub.php | 6 +-- plugins/OStatus/scripts/renew-feeds.php | 43 ++++++++++++++++++++ 5 files changed, 86 insertions(+), 50 deletions(-) create mode 100644 plugins/OStatus/scripts/renew-feeds.php diff --git a/plugins/OStatus/actions/pushcallback.php b/plugins/OStatus/actions/pushcallback.php index 41f5f1691f..f68ef7693e 100644 --- a/plugins/OStatus/actions/pushcallback.php +++ b/plugins/OStatus/actions/pushcallback.php @@ -82,9 +82,8 @@ class PushCallbackAction extends Action $mode = $this->arg('hub_mode'); $topic = $this->arg('hub_topic'); $challenge = $this->arg('hub_challenge'); - $lease_seconds = $this->arg('hub_lease_seconds'); - $verify_token = $this->arg('hub_verify_token'); - common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds verify_token: $verify_token"); + $lease_seconds = $this->arg('hub_lease_seconds'); // Must be >0 for PuSH 0.4! + common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds"); if ($mode != 'subscribe' && $mode != 'unsubscribe') { // TRANS: Client exception. %s is an invalid value for hub.mode. @@ -92,16 +91,11 @@ class PushCallbackAction extends Action } $feedsub = FeedSub::getKV('uri', $topic); - if (!$feedsub) { + if (!$feedsub instanceof FeedSub) { // TRANS: Client exception. %s is an invalid feed name. throw new ClientException(sprintf(_m('Bad hub.topic feed "%s".'),$topic), 404); } - if ($feedsub->verify_token !== $verify_token) { - // TRANS: Client exception. %1$s the invalid token, %2$s is the topic for which the invalid token was given. - throw new ClientException(sprintf(_m('Bad hub.verify_token %1$s for %2$s.'),$token,$topic), 404); - } - if ($mode == 'subscribe') { // We may get re-sub requests legitimately. if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') { diff --git a/plugins/OStatus/actions/pushhub.php b/plugins/OStatus/actions/pushhub.php index 1818a07997..fb41c42ad3 100644 --- a/plugins/OStatus/actions/pushhub.php +++ b/plugins/OStatus/actions/pushhub.php @@ -89,20 +89,12 @@ class PushHubAction extends Action throw new ClientException(sprintf(_m('Unsupported hub.topic %s this hub only serves local user and group Atom feeds.'),$topic)); } - $verify = $this->arg('hub.verify'); // @fixme may be multiple - if ($verify != 'sync' && $verify != 'async') { - // TRANS: Client exception. %s is sync or async. - throw new ClientException(sprintf(_m('Invalid hub.verify "%s". It must be sync or async.'),$verify)); - } - $lease = $this->arg('hub.lease_seconds', null); if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) { // TRANS: Client exception. %s is the invalid lease value. throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease)); } - $token = $this->arg('hub.verify_token', null); - $secret = $this->arg('hub.secret', null); if ($secret != '' && strlen($secret) >= 200) { // TRANS: Client exception. %s is the invalid hub secret. @@ -110,7 +102,7 @@ class PushHubAction extends Action } $sub = HubSub::getByHashkey($topic, $callback); - if (!$sub) { + if (!$sub instanceof HubSub) { // Creating a new one! $sub = new HubSub(); $sub->topic = $topic; @@ -125,16 +117,14 @@ class PushHubAction extends Action } } - if (!common_config('queue', 'enabled')) { - // Won't be able to background it. - $verify = 'sync'; - } - if ($verify == 'async') { - $sub->scheduleVerify($mode, $token); - header('HTTP/1.1 202 Accepted'); - } else { + $verify = $this->arg('hub.verify'); // TODO: deprecated + $token = $this->arg('hub.verify_token', null); // TODO: deprecated + if ($verify == 'sync') { // pre-0.4 PuSH $sub->verify($mode, $token); header('HTTP/1.1 204 No Content'); + } else { // If $verify is not "sync", we might be using PuSH 0.4 + $sub->scheduleVerify($mode, $token); // If we were certain it's PuSH 0.4, token could be removed + header('HTTP/1.1 202 Accepted'); } } diff --git a/plugins/OStatus/classes/FeedSub.php b/plugins/OStatus/classes/FeedSub.php index 236145fe56..c055420ed9 100644 --- a/plugins/OStatus/classes/FeedSub.php +++ b/plugins/OStatus/classes/FeedSub.php @@ -30,8 +30,6 @@ if (!defined('STATUSNET')) { PuSH subscription flow: $profile->subscribe() - generate random verification token - save to verify_token sends a sub request to the hub... main/push/callback @@ -69,7 +67,6 @@ class FeedSub extends Managed_DataObject // PuSH subscription data public $huburi; public $secret; - public $verify_token; public $sub_state; // subscribe, active, unsubscribe, inactive public $sub_start; public $sub_end; @@ -85,7 +82,6 @@ class FeedSub extends Managed_DataObject 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'FeedSub local unique id'), 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'FeedSub uri'), 'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'), - 'verify_token' => array('type' => 'text', 'description' => 'FeedSub verify-token'), 'secret' => array('type' => 'text', 'description' => 'FeedSub stored secret'), 'sub_state' => array('type' => 'enum("subscribe","active","unsubscribe","inactive")', 'not null' => true, 'description' => 'subscription state'), 'sub_start' => array('type' => 'datetime', 'description' => 'subscription start'), @@ -168,10 +164,10 @@ class FeedSub extends Managed_DataObject * @return bool true on success, false on failure * @throws ServerException if feed state is not valid */ - public function subscribe($mode='subscribe') + public function subscribe() { if ($this->sub_state && $this->sub_state != 'inactive') { - common_log(LOG_WARNING, "Attempting to (re)start PuSH subscription to $this->uri in unexpected state $this->sub_state"); + common_log(LOG_WARNING, "Attempting to (re)start PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}"); } if (empty($this->huburi)) { if (common_config('feedsub', 'fallback_hub')) { @@ -202,7 +198,7 @@ class FeedSub extends Managed_DataObject */ public function unsubscribe() { if ($this->sub_state != 'active') { - common_log(LOG_WARNING, "Attempting to (re)end PuSH subscription to $this->uri in unexpected state $this->sub_state"); + common_log(LOG_WARNING, "Attempting to (re)end PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}"); } if (empty($this->huburi)) { if (common_config('feedsub', 'fallback_hub')) { @@ -248,10 +244,29 @@ class FeedSub extends Managed_DataObject } } + static public function renewalCheck() + { + $fs = new FeedSub(); + // the "" empty string check is because we historically haven't saved unsubscribed feeds as NULL + $fs->whereAdd('sub_end IS NOT NULL AND sub_end!="" AND sub_end < NOW() - INTERVAL 1 day'); + if ($fs->find() === false) { + throw new NoResultException($fs); + } + return $fs; + } + + public function renew() + { + $this->subscribe(); + } + + /** + * @return boolean true on successful sub/unsub, false on failure + */ protected function doSubscribe($mode) { + $this->query('BEGIN'); $orig = clone($this); - $this->verify_token = common_random_hexstr(16); if ($mode == 'subscribe') { $this->secret = common_random_hexstr(32); } @@ -264,8 +279,7 @@ class FeedSub extends Managed_DataObject $headers = array('Content-Type: application/x-www-form-urlencoded'); $post = array('hub.mode' => $mode, 'hub.callback' => $callback, - 'hub.verify' => 'sync', - 'hub.verify_token' => $this->verify_token, + 'hub.verify' => 'async', // TODO: deprecated, remove when noone uses PuSH <0.4 'hub.secret' => $this->secret, 'hub.topic' => $this->uri); $client = new HTTPClient(); @@ -286,30 +300,26 @@ class FeedSub extends Managed_DataObject $response = $client->post($hub, $headers, $post); $status = $response->getStatus(); if ($status == 202) { + $this->query('COMMIT'); common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback'); return true; - } else if ($status == 204) { - common_log(LOG_INFO, __METHOD__ . ': sub req ok and verified'); - return true; } else if ($status >= 200 && $status < 300) { common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody()); - return false; } else { common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody()); - return false; } + $this->query('ROLLBACK'); } catch (Exception $e) { + $this->query('ROLLBACK'); // wtf! common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->uri"); $orig = clone($this); - $this->verify_token = ''; $this->sub_state = 'inactive'; $this->update($orig); unset($orig); - - return false; } + return false; } /** @@ -318,7 +328,7 @@ class FeedSub extends Managed_DataObject * * @param int $lease_seconds provided hub.lease_seconds parameter, if given */ - public function confirmSubscribe($lease_seconds=0) + public function confirmSubscribe($lease_seconds) { $original = clone($this); @@ -327,7 +337,7 @@ class FeedSub extends Managed_DataObject if ($lease_seconds > 0) { $this->sub_end = common_sql_date(time() + $lease_seconds); } else { - $this->sub_end = null; + $this->sub_end = null; // Backwards compatibility to StatusNet (PuSH <0.4 supported permanent subs) } $this->modified = common_sql_now(); @@ -343,7 +353,6 @@ class FeedSub extends Managed_DataObject $original = clone($this); // @fixme these should all be null, but DB_DataObject doesn't save null values...????? - $this->verify_token = ''; $this->secret = ''; $this->sub_state = ''; $this->sub_start = ''; diff --git a/plugins/OStatus/classes/HubSub.php b/plugins/OStatus/classes/HubSub.php index e63831f576..30c32ac1aa 100644 --- a/plugins/OStatus/classes/HubSub.php +++ b/plugins/OStatus/classes/HubSub.php @@ -114,7 +114,7 @@ class HubSub extends Managed_DataObject } $data = array('sub' => clone($this), 'mode' => $mode, - 'token' => $token, + 'token' => $token, // let's put it in there if remote uses PuSH <0.4 'retries' => $retries); $qm = QueueManager::get(); $qm->enqueue($data, 'hubconf'); @@ -139,8 +139,8 @@ class HubSub extends Managed_DataObject if ($mode == 'subscribe') { $params['hub.lease_seconds'] = $this->lease; } - if ($token !== null) { - $params['hub.verify_token'] = $token; + if ($token !== null) { // TODO: deprecated in PuSH 0.4 + $params['hub.verify_token'] = $token; // let's put it in there if remote uses PuSH <0.4 } // Any existing query string parameters must be preserved diff --git a/plugins/OStatus/scripts/renew-feeds.php b/plugins/OStatus/scripts/renew-feeds.php new file mode 100644 index 0000000000..8f8ac3ee1d --- /dev/null +++ b/plugins/OStatus/scripts/renew-feeds.php @@ -0,0 +1,43 @@ +#!/usr/bin/env php +. + */ + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..')); + +$helptext = <<fetch()) { + echo "Renewing feed subscription\n\tExp.: {$sub->sub_end}\n\tFeed: {$sub->uri}\n\tHub: {$sub->huburi}\n"; + $sub->renew(); +} + +echo "Done!";