summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugins/OStatus/OStatusPlugin.php6
-rw-r--r--plugins/OStatus/actions/hostmeta.php16
-rw-r--r--plugins/OStatus/actions/ostatusinit.php4
-rw-r--r--plugins/OStatus/actions/xrd.php (renamed from plugins/OStatus/actions/webfinger.php)12
-rw-r--r--plugins/OStatus/classes/Magicsig.php3
-rw-r--r--plugins/OStatus/classes/Ostatus_profile.php8
-rw-r--r--plugins/OStatus/lib/discovery.php303
-rw-r--r--plugins/OStatus/lib/magicenvelope.php13
-rw-r--r--plugins/OStatus/lib/salmon.php8
9 files changed, 348 insertions, 25 deletions
diff --git a/plugins/OStatus/OStatusPlugin.php b/plugins/OStatus/OStatusPlugin.php
index 7f75b7b2b..91d055498 100644
--- a/plugins/OStatus/OStatusPlugin.php
+++ b/plugins/OStatus/OStatusPlugin.php
@@ -43,8 +43,8 @@ class OStatusPlugin extends Plugin
// Discovery actions
$m->connect('.well-known/host-meta',
array('action' => 'hostmeta'));
- $m->connect('main/webfinger',
- array('action' => 'webfinger'));
+ $m->connect('main/xrd',
+ array('action' => 'xrd'));
$m->connect('main/ostatus',
array('action' => 'ostatusinit'));
$m->connect('main/ostatus?nickname=:nickname',
@@ -644,7 +644,7 @@ class OStatusPlugin extends Plugin
function onStartUserGroupHomeUrl($group, &$url)
{
- return $this->onStartUserGroupPermalink($group, &$url);
+ return $this->onStartUserGroupPermalink($group, $url);
}
function onStartUserGroupPermalink($group, &$url)
diff --git a/plugins/OStatus/actions/hostmeta.php b/plugins/OStatus/actions/hostmeta.php
index 850b8a0fe..3d00b98ae 100644
--- a/plugins/OStatus/actions/hostmeta.php
+++ b/plugins/OStatus/actions/hostmeta.php
@@ -31,12 +31,18 @@ class HostMetaAction extends Action
{
parent::handle();
- $w = new Webfinger();
-
-
$domain = common_config('site', 'server');
- $url = common_local_url('webfinger');
+ $url = common_local_url('xrd');
$url.= '?uri={uri}';
- print $w->getHostMeta($domain, $url);
+
+ $xrd = new XRD();
+
+ $xrd = new XRD();
+ $xrd->host = $domain;
+ $xrd->links[] = array('rel' => Discovery::LRDD_REL,
+ 'template' => $url,
+ 'title' => array('Resource Descriptor'));
+
+ print $xrd->toXML();
}
}
diff --git a/plugins/OStatus/actions/ostatusinit.php b/plugins/OStatus/actions/ostatusinit.php
index 3f2f6368f..5c8575595 100644
--- a/plugins/OStatus/actions/ostatusinit.php
+++ b/plugins/OStatus/actions/ostatusinit.php
@@ -131,9 +131,9 @@ class OStatusInitAction extends Action
function connectWebfinger($acct)
{
- $w = new Webfinger;
+ $disco = new Discovery;
- $result = $w->lookup($acct);
+ $result = $disco->lookup($acct);
if (!$result) {
$this->clientError(_m("Couldn't look up OStatus account profile."));
}
diff --git a/plugins/OStatus/actions/webfinger.php b/plugins/OStatus/actions/xrd.php
index e292ccec9..2a754dcfe 100644
--- a/plugins/OStatus/actions/webfinger.php
+++ b/plugins/OStatus/actions/xrd.php
@@ -24,7 +24,7 @@
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
-class WebfingerAction extends Action
+class XrdAction extends Action
{
public $uri;
@@ -40,11 +40,11 @@ class WebfingerAction extends Action
function handle()
{
- $acct = Webfinger::normalize($this->uri);
+ $acct = Discovery::normalize($this->uri);
$xrd = new XRD();
- list($nick, $domain) = explode('@', urldecode($acct));
+ list($nick, $domain) = explode('@', substr(urldecode($acct), 5));
$nick = common_canonical_nickname($nick);
$this->user = User::staticGet('nickname', $nick);
@@ -55,11 +55,11 @@ class WebfingerAction extends Action
$xrd->subject = $this->uri;
$xrd->alias[] = common_profile_url($nick);
- $xrd->links[] = array('rel' => Webfinger::PROFILEPAGE,
+ $xrd->links[] = array('rel' => Discovery::PROFILEPAGE,
'type' => 'text/html',
'href' => common_profile_url($nick));
- $xrd->links[] = array('rel' => Webfinger::UPDATESFROM,
+ $xrd->links[] = array('rel' => Discovery::UPDATESFROM,
'href' => common_local_url('ApiTimelineUser',
array('id' => $this->user->id,
'format' => 'atom')),
@@ -91,7 +91,7 @@ class WebfingerAction extends Action
if (!$magickey) {
// No keypair yet, let's generate one.
$magickey = new Magicsig();
- $magickey->generate();
+ $magickey->generate($this->user->id);
}
$xrd->links[] = array('rel' => Magicsig::PUBLICKEYREL,
diff --git a/plugins/OStatus/classes/Magicsig.php b/plugins/OStatus/classes/Magicsig.php
index 681aec184..02882d19b 100644
--- a/plugins/OStatus/classes/Magicsig.php
+++ b/plugins/OStatus/classes/Magicsig.php
@@ -90,7 +90,7 @@ class Magicsig extends Memcached_DataObject
return parent::insert();
}
- public function generate($key_length = 512)
+ public function generate($user_id, $key_length = 512)
{
PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
@@ -101,6 +101,7 @@ class Magicsig extends Memcached_DataObject
$this->_rsa = new Crypt_RSA($params);
PEAR::popErrorHandling();
+ $this->user_id = $user_id;
$this->insert();
}
diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php
index 9c344feb7..4a9aafce1 100644
--- a/plugins/OStatus/classes/Ostatus_profile.php
+++ b/plugins/OStatus/classes/Ostatus_profile.php
@@ -1285,9 +1285,9 @@ class Ostatus_profile extends Memcached_DataObject
// Now, try some discovery
- $wf = new Webfinger();
+ $disco = new Discovery();
- $result = $wf->lookup($addr);
+ $result = $disco->lookup($addr);
if (!$result) {
self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
@@ -1296,13 +1296,13 @@ class Ostatus_profile extends Memcached_DataObject
foreach ($result->links as $link) {
switch ($link['rel']) {
- case Webfinger::PROFILEPAGE:
+ case Discovery::PROFILEPAGE:
$profileUrl = $link['href'];
break;
case 'salmon':
$salmonEndpoint = $link['href'];
break;
- case Webfinger::UPDATESFROM:
+ case Discovery::UPDATESFROM:
$feedUrl = $link['href'];
break;
case Webfinger::HCARD:
diff --git a/plugins/OStatus/lib/discovery.php b/plugins/OStatus/lib/discovery.php
new file mode 100644
index 000000000..8aba31328
--- /dev/null
+++ b/plugins/OStatus/lib/discovery.php
@@ -0,0 +1,303 @@
+<?php
+/**
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * A sample module to show best practices for StatusNet plugins
+ *
+ * PHP version 5
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @package StatusNet
+ * @author James Walker <james@status.net>
+ * @copyright 2010 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link http://status.net/
+ */
+
+/**
+ * This class implements LRDD-based service discovery based on the "Hammer Draft"
+ * (including webfinger)
+ *
+ * @see http://groups.google.com/group/webfinger/browse_thread/thread/9f3d93a479e91bbf
+ */
+class Discovery
+{
+
+ const LRDD_REL = 'lrdd';
+ const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
+ const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
+
+ public $methods = array();
+
+ public function __construct()
+ {
+ $this->registerMethod('Discovery_LRDD_Host_Meta');
+ $this->registerMethod('Discovery_LRDD_Link_Header');
+ $this->registerMethod('Discovery_LRDD_Link_HTML');
+ }
+
+
+ public function registerMethod($class)
+ {
+ $this->methods[] = $class;
+ }
+
+ /**
+ * Given a "user id" make sure it's normalized to either a webfinger
+ * acct: uri or a profile HTTP URL.
+ */
+ public static function normalize($user_id)
+ {
+ if (substr($user_id, 0, 5) == 'http:' ||
+ substr($user_id, 0, 6) == 'https:' ||
+ substr($user_id, 0, 5) == 'acct:') {
+ return $user_id;
+ }
+
+ if (strpos($user_id, '@') !== FALSE) {
+ return 'acct:' . $user_id;
+ }
+
+ return 'http://' . $user_id;
+ }
+
+ public static function isWebfinger($user_id)
+ {
+ $uri = Discovery::normalize($user_id);
+
+ return (substr($uri, 0, 5) == 'acct:');
+ }
+
+ /**
+ * This implements the actual lookup procedure
+ */
+ public function lookup($id)
+ {
+ // Normalize the incoming $id to make sure we have a uri
+ $uri = $this->normalize($id);
+
+ foreach ($this->methods as $class) {
+ $links = call_user_func(array($class, 'discover'), $uri);
+
+ if ($link = Discovery::getService($links, Discovery::LRDD_REL)) {
+ // Load the LRDD XRD
+ if ($link['template']) {
+ $xrd_uri = Discovery::applyTemplate($link['template'], $uri);
+ } else {
+ $xrd_uri = $link['href'];
+ }
+
+ $xrd = $this->fetchXrd($xrd_uri);
+ if ($xrd) {
+ return $xrd;
+ }
+ }
+ }
+
+ throw new Exception('Unable to find services for '. $id);
+ }
+
+ public static function getService($links, $service) {
+ foreach ($links as $link) {
+ if ($link['rel'] == $service) {
+ return $link;
+ }
+ }
+ }
+
+
+ public static function applyTemplate($template, $id)
+ {
+ $template = str_replace('{uri}', urlencode($id), $template);
+
+ return $template;
+ }
+
+
+ public static function fetchXrd($url)
+ {
+ try {
+ $client = new HTTPClient();
+ $response = $client->get($url);
+ } catch (HTTP_Request2_Exception $e) {
+ return false;
+ }
+
+ if ($response->getStatus() != 200) {
+ return false;
+ }
+
+ return XRD::parse($response->getBody());
+ }
+}
+
+interface Discovery_LRDD
+{
+ public function discover($uri);
+}
+
+class Discovery_LRDD_Host_Meta implements Discovery_LRDD
+{
+ public function discover($uri)
+ {
+ if (Discovery::isWebfinger($uri)) {
+ // We have a webfinger acct: - start with host-meta
+ list($name, $domain) = explode('@', $id);
+ } else {
+ $domain = @parse_url($uri, PHP_URL_HOST);
+ }
+
+ $url = 'http://'. $domain .'/.well-known/host-meta';
+
+ $xrd = Discovery::fetchXrd($url);
+
+ if ($xrd) {
+ if ($xrd->host != $domain) {
+ return false;
+ }
+
+ return $xrd->links;
+ }
+ }
+}
+
+class Discovery_LRDD_Link_Header implements Discovery_LRDD
+{
+ public function discover($uri)
+ {
+ try {
+ $client = new HTTPClient();
+ $response = $client->get($url);
+ } catch (HTTP_Request2_Exception $e) {
+ return false;
+ }
+
+ if ($response->getStatus() != 200) {
+ return false;
+ }
+
+ $link_header = $response->getHeader('Link');
+ if (!$link_header) {
+ return false;
+ }
+
+ return Discovery_LRDD_Link_Header::parseHeader($header);
+ }
+
+ protected static function parseHeader($header)
+ {
+ preg_match('/^<[^>]+>/', $header, $uri_reference);
+ if (empty($uri_reference)) return;
+
+ $link_uri = trim($uri_reference[0], '<>');
+ $link_rel = array();
+ $link_type = null;
+
+ // remove uri-reference from header
+ $header = substr($header, strlen($uri_reference[0]));
+
+ // parse link-params
+ $params = explode($header, ';');
+
+ foreach ($params as $param) {
+ if (empty($param)) continue;
+ list($param_name, $param_value) = explode('=', $param, 2);
+ $param_name = trim($param_name);
+ $param_value = preg_replace('(^"|"$)', '', trim($param_value));
+
+ // for now we only care about 'rel' and 'type' link params
+ // TODO do something with the other links-params
+ switch ($param_name) {
+ case 'rel':
+ $link_rel = trim($param_value);
+ break;
+
+ case 'type':
+ $link_type = trim($param_value);
+ }
+ }
+
+ return array(
+ 'href' => $link_uri,
+ 'rel' => $link_rel,
+ 'type' => $link_type);
+ }
+}
+
+class Discovery_LRDD_Link_HTML implements Discovery_LRDD
+{
+ public function discover($uri)
+ {
+ try {
+ $client = new HTTPClient();
+ $response = $client->get($url);
+ } catch (HTTP_Request2_Exception $e) {
+ return false;
+ }
+
+ if ($response->getStatus() != 200) {
+ return false;
+ }
+
+ return Discovery_LRDD_Link_HTML::parse($response->getBody());
+ }
+
+
+ public function parse($html)
+ {
+ $links = array();
+
+ preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
+ $head_html = $head_matches[2];
+
+ preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
+
+ foreach ($link_matches[0] as $link_html) {
+ $link_url = null;
+ $link_rel = null;
+ $link_type = null;
+
+ preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
+ if ( isset($rel_matches[3]) ) {
+ $link_rel = $rel_matches[3];
+ } else if ( isset($rel_matches[1]) ) {
+ $link_rel = $rel_matches[1];
+ }
+
+ preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
+ if ( isset($href_matches[3]) ) {
+ $link_uri = $href_matches[3];
+ } else if ( isset($href_matches[1]) ) {
+ $link_uri = $href_matches[1];
+ }
+
+ preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
+ if ( isset($type_matches[3]) ) {
+ $link_type = $type_matches[3];
+ } else if ( isset($type_matches[1]) ) {
+ $link_type = $type_matches[1];
+ }
+
+ $links[] = array(
+ 'href' => $link_url,
+ 'rel' => $link_rel,
+ 'type' => $link_type,
+ );
+ }
+
+ return $links;
+ }
+}
diff --git a/plugins/OStatus/lib/magicenvelope.php b/plugins/OStatus/lib/magicenvelope.php
index 81f4609c5..4f8f88155 100644
--- a/plugins/OStatus/lib/magicenvelope.php
+++ b/plugins/OStatus/lib/magicenvelope.php
@@ -50,7 +50,16 @@ class MagicEnvelope
public function getKeyPair($signer_uri)
{
- return 'RSA.79_L2gq-TD72Nsb5yGS0r9stLLpJZF5AHXyxzWmQmlqKl276LEJEs8CppcerLcR90MbYQUwt-SX9slx40Yq3vA==.AQAB.AR-jo5KMfSISmDAT2iMs2_vNFgWRjl5rbJVvA0SpGIEWyPdCGxlPtCbTexp8-0ZEIe8a4SyjatBECH5hxgMTpw==';
+ $disco = new Discovery();
+
+ $links = $disco->lookup($signer_uri);
+ if ($link = Discovery::getService($links, 'magic-public-key')) {
+ list($type, $keypair) = explode(';', $link['href']);
+ return $keypair;
+ }
+
+ throw new Exception('Unable to locate signer public key');
+ //return 'RSA.79_L2gq-TD72Nsb5yGS0r9stLLpJZF5AHXyxzWmQmlqKl276LEJEs8CppcerLcR90MbYQUwt-SX9slx40Yq3vA==.AQAB.AR-jo5KMfSISmDAT2iMs2_vNFgWRjl5rbJVvA0SpGIEWyPdCGxlPtCbTexp8-0ZEIe8a4SyjatBECH5hxgMTpw==';
}
@@ -59,7 +68,7 @@ class MagicEnvelope
$signer_uri = $this->normalizeUser($signer_uri);
if (!$this->checkAuthor($text, $signer_uri)) {
- return false;
+ throw new Exception("Unable to determine entry author.");
}
$signature_alg = Magicsig::fromString($this->getKeyPair($signer_uri));
diff --git a/plugins/OStatus/lib/salmon.php b/plugins/OStatus/lib/salmon.php
index b5f178cc6..9d4359f74 100644
--- a/plugins/OStatus/lib/salmon.php
+++ b/plugins/OStatus/lib/salmon.php
@@ -72,8 +72,12 @@ class Salmon
// TODO: Should probably be getting the signer uri as an argument?
$signer_uri = $magic_env->getAuthor($text);
- $env = $magic_env->signMessage($text, 'application/atom+xml', $signer_uri);
-
+ try {
+ $env = $magic_env->signMessage($text, 'application/atom+xml', $signer_uri);
+ } catch (Exception $e) {
+ common_log(LOG_ERR, "Salmon signing failed: ". $e->getMessage());
+ return $text;
+ }
return $magic_env->unfold($env);
}