]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Merge remote branch 'statusnet/testing' into testing
authorJames Walker <walkah@walkah.net>
Fri, 26 Feb 2010 04:49:45 +0000 (23:49 -0500)
committerJames Walker <walkah@walkah.net>
Fri, 26 Feb 2010 04:49:45 +0000 (23:49 -0500)
Conflicts:
plugins/OStatus/lib/webfinger.php

plugins/OStatus/OStatusPlugin.php
plugins/OStatus/actions/hostmeta.php
plugins/OStatus/actions/ostatusinit.php
plugins/OStatus/actions/webfinger.php [deleted file]
plugins/OStatus/actions/xrd.php [new file with mode: 0644]
plugins/OStatus/classes/Magicsig.php
plugins/OStatus/classes/Ostatus_profile.php
plugins/OStatus/lib/discovery.php [new file with mode: 0644]
plugins/OStatus/lib/magicenvelope.php
plugins/OStatus/lib/salmon.php

index 7f75b7b2b4aff75995e0cf2adc764827bc05f02d..91d0554982f2ea0463c24acd393adafeca5f842e 100644 (file)
@@ -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)
index 850b8a0fe89d47bab373327abab797db13dbc5b9..3d00b98ae0d359687380fbdbe5cf4c27e1466244 100644 (file)
@@ -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();
     }
 }
index 3f2f6368f6038b94092e8fc0090fd8cfa4c5e18f..5c85755959ae7475ffde8fad5e063f4924437fe9 100644 (file)
@@ -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/webfinger.php
deleted file mode 100644 (file)
index e292cce..0000000
+++ /dev/null
@@ -1,109 +0,0 @@
-<?php
-/*
- * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2010, StatusNet, Inc.
- *
- * 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 OStatusPlugin
- * @maintainer James Walker <james@status.net>
- */
-
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
-
-class WebfingerAction extends Action
-{
-
-    public $uri;
-
-    function prepare($args)
-    {
-        parent::prepare($args);
-
-        $this->uri = $this->trimmed('uri');
-
-        return true;
-    }
-
-    function handle()
-    {
-        $acct = Webfinger::normalize($this->uri);
-
-        $xrd = new XRD();
-
-        list($nick, $domain) = explode('@', urldecode($acct));
-        $nick = common_canonical_nickname($nick);
-
-        $this->user = User::staticGet('nickname', $nick);
-        if (!$this->user) {
-            $this->clientError(_('No such user.'), 404);
-            return false;
-        }
-
-        $xrd->subject = $this->uri;
-        $xrd->alias[] = common_profile_url($nick);
-        $xrd->links[] = array('rel' => Webfinger::PROFILEPAGE,
-                              'type' => 'text/html',
-                              'href' => common_profile_url($nick));
-
-        $xrd->links[] = array('rel' => Webfinger::UPDATESFROM,
-                              'href' => common_local_url('ApiTimelineUser',
-                                                         array('id' => $this->user->id,
-                                                               'format' => 'atom')),
-                              'type' => 'application/atom+xml');
-
-        // hCard
-        $xrd->links[] = array('rel' => Webfinger::HCARD,
-                              'type' => 'text/html',
-                              'href' => common_local_url('hcard', array('nickname' => $nick)));
-
-        // XFN
-        $xrd->links[] = array('rel' => 'http://gmpg.org/xfn/11',
-                              'type' => 'text/html',
-                              'href' => common_profile_url($nick));
-        // FOAF
-        $xrd->links[] = array('rel' => 'describedby',
-                              'type' => 'application/rdf+xml',
-                              'href' => common_local_url('foaf',
-                                                         array('nickname' => $nick)));
-
-        $salmon_url = common_local_url('salmon',
-                                       array('id' => $this->user->id));
-
-        $xrd->links[] = array('rel' => 'salmon',
-                              'href' => $salmon_url);
-
-        // Get this user's keypair
-        $magickey = Magicsig::staticGet('user_id', $this->user->id);
-        if (!$magickey) {
-            // No keypair yet, let's generate one.
-            $magickey = new Magicsig();
-            $magickey->generate();
-        }
-
-        $xrd->links[] = array('rel' => Magicsig::PUBLICKEYREL,
-                              'href' => 'data:application/magic-public-key;'. $magickey->keypair);
-
-        // TODO - finalize where the redirect should go on the publisher
-        $url = common_local_url('ostatussub') . '?profile={uri}';
-        $xrd->links[] = array('rel' => 'http://ostatus.org/schema/1.0/subscribe',
-                              'template' => $url );
-
-        header('Content-type: text/xml');
-        print $xrd->toXML();
-    }
-
-}
diff --git a/plugins/OStatus/actions/xrd.php b/plugins/OStatus/actions/xrd.php
new file mode 100644 (file)
index 0000000..2a754dc
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * 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 OStatusPlugin
+ * @maintainer James Walker <james@status.net>
+ */
+
+if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
+
+class XrdAction extends Action
+{
+
+    public $uri;
+
+    function prepare($args)
+    {
+        parent::prepare($args);
+
+        $this->uri = $this->trimmed('uri');
+
+        return true;
+    }
+
+    function handle()
+    {
+        $acct = Discovery::normalize($this->uri);
+
+        $xrd = new XRD();
+
+        list($nick, $domain) = explode('@', substr(urldecode($acct), 5));
+        $nick = common_canonical_nickname($nick);
+
+        $this->user = User::staticGet('nickname', $nick);
+        if (!$this->user) {
+            $this->clientError(_('No such user.'), 404);
+            return false;
+        }
+
+        $xrd->subject = $this->uri;
+        $xrd->alias[] = common_profile_url($nick);
+        $xrd->links[] = array('rel' => Discovery::PROFILEPAGE,
+                              'type' => 'text/html',
+                              'href' => common_profile_url($nick));
+
+        $xrd->links[] = array('rel' => Discovery::UPDATESFROM,
+                              'href' => common_local_url('ApiTimelineUser',
+                                                         array('id' => $this->user->id,
+                                                               'format' => 'atom')),
+                              'type' => 'application/atom+xml');
+
+        // hCard
+        $xrd->links[] = array('rel' => Webfinger::HCARD,
+                              'type' => 'text/html',
+                              'href' => common_local_url('hcard', array('nickname' => $nick)));
+
+        // XFN
+        $xrd->links[] = array('rel' => 'http://gmpg.org/xfn/11',
+                              'type' => 'text/html',
+                              'href' => common_profile_url($nick));
+        // FOAF
+        $xrd->links[] = array('rel' => 'describedby',
+                              'type' => 'application/rdf+xml',
+                              'href' => common_local_url('foaf',
+                                                         array('nickname' => $nick)));
+
+        $salmon_url = common_local_url('salmon',
+                                       array('id' => $this->user->id));
+
+        $xrd->links[] = array('rel' => 'salmon',
+                              'href' => $salmon_url);
+
+        // Get this user's keypair
+        $magickey = Magicsig::staticGet('user_id', $this->user->id);
+        if (!$magickey) {
+            // No keypair yet, let's generate one.
+            $magickey = new Magicsig();
+            $magickey->generate($this->user->id);
+        }
+
+        $xrd->links[] = array('rel' => Magicsig::PUBLICKEYREL,
+                              'href' => 'data:application/magic-public-key;'. $magickey->keypair);
+
+        // TODO - finalize where the redirect should go on the publisher
+        $url = common_local_url('ostatussub') . '?profile={uri}';
+        $xrd->links[] = array('rel' => 'http://ostatus.org/schema/1.0/subscribe',
+                              'template' => $url );
+
+        header('Content-type: text/xml');
+        print $xrd->toXML();
+    }
+
+}
index 681aec184d0ec8672cf01342826a52abb4b54ac5..02882d19b13e1750e43e1b31c7fe7c3a55263d62 100644 (file)
@@ -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();
     }
 
index 9c344feb77be5e15605b3b3e03878630d7bcceab..4a9aafce1e31d55c93d444b33b5d0d528905c407 100644 (file)
@@ -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 (file)
index 0000000..8aba313
--- /dev/null
@@ -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;
+    }
+}
index 81f4609c5cd94a138ab795381486c967f0e00913..4f8f8815533faaeeac0afabed535d0be8c3afb09 100644 (file)
@@ -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));
index b5f178cc6a2e4cec2f191dad33d96ed39aa9a4ad..9d4359f74f5e5c875b7faabd6d7a823bdeb7f013 100644 (file)
@@ -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);
     }