3 * GNU Social - a federating social network
4 * Copyright (C) 2013, Free Software Foundation, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Implements WebFinger for GNU Social, as well as support for the
22 * '.well-known/host-meta' resource.
24 * Depends on: LRDD plugin
27 * @author Mikael Nordfeldth <mmn@hethane.se>
30 if (!defined('GNUSOCIAL')) { exit(1); }
32 class WebFingerPlugin extends Plugin
34 const OAUTH_ACCESS_TOKEN_REL = 'http://apinamespace.org/oauth/access_token';
35 const OAUTH_REQUEST_TOKEN_REL = 'http://apinamespace.org/oauth/request_token';
36 const OAUTH_AUTHORIZE_REL = 'http://apinamespace.org/oauth/authorize';
38 public $http_alias = false;
39 public $fancyurlfix = true; // adds + interprets some extra aliases related to 'index.php/' URLs
41 public function initialize()
43 common_config_set('webfinger', 'http_alias', $this->http_alias);
44 common_config_set('webfinger', 'fancyurlfix', $this->fancyurlfix);
47 public function onRouterInitialized($m)
49 $m->connect('.well-known/host-meta', array('action' => 'hostmeta'));
50 $m->connect('.well-known/host-meta.:format',
51 array('action' => 'hostmeta',
52 'format' => '(xml|json)'));
53 // the resource GET parameter can be anywhere, so don't mention it here
54 $m->connect('.well-known/webfinger', array('action' => 'webfinger'));
55 $m->connect('.well-known/webfinger.:format',
56 array('action' => 'webfinger',
57 'format' => '(xml|json)'));
58 $m->connect('main/ownerxrd', array('action' => 'ownerxrd'));
62 public function onLoginAction($action, &$login)
74 public function onStartGetProfileAcctUri(Profile $profile, &$acct)
76 $wfr = new WebFingerResource_Profile($profile);
78 $acct = $wfr->reconstructAcct();
79 } catch (Exception $e) {
86 public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
89 if (Discovery::isAcct($resource)) {
90 $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
91 if (count($parts) == 2) {
92 list($nick, $domain) = $parts;
93 if ($domain === common_config('site', 'server')) {
94 $nick = common_canonical_nickname($nick);
95 $user = User::getKV('nickname', $nick);
96 if (!($user instanceof User)) {
97 throw new NoSuchUserException(array('nickname'=>$nick));
99 $profile = $user->getProfile();
101 throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
106 $user = User::getByUri($resource);
107 $profile = $user->getProfile();
108 } catch (NoResultException $e) {
109 if (common_config('webfinger', 'fancyurlfix')) {
111 try { // if it's a /index.php/ url
112 // common_fake_local_fancy_url can throw an exception
113 $alt_url = common_fake_local_fancy_url($resource);
114 } catch (Exception $e) { // let's try to create a fake local /index.php/ url
115 // this too if it can't do anything about the URL
116 $alt_url = common_fake_local_nonfancy_url($resource);
119 // and this will throw a NoResultException if not found
120 $user = User::getByUri($alt_url);
121 $profile = $user->getProfile();
122 } catch (Exception $e) {
123 // apparently we didn't get any matches with that, so continue...
129 // if we still haven't found a match...
130 if (!$profile instanceof Profile) {
131 // if our rewrite hack didn't work, try to get something by profile URL
132 $profile = Profile::getKV('profileurl', $resource);
135 if ($profile instanceof Profile) {
136 $target = new WebFingerResource_Profile($profile);
137 return false; // We got our target, stop handler execution
140 $notice = Notice::getKV('uri', $resource);
141 if ($notice instanceof Notice) {
142 $target = new WebFingerResource_Notice($notice);
149 public function onStartHostMetaLinks(array &$links)
151 foreach (Discovery::supportedMimeTypes() as $type) {
152 $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
153 common_local_url('webfinger') . '?resource={uri}',
159 $links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL, common_local_url('ApiOAuthAccessToken'));
160 $links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
161 $links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL, common_local_url('ApiOAuthAuthorize'));
165 * Add a link header for LRDD Discovery
167 public function onStartShowHTML($action)
169 if ($action instanceof ShowstreamAction) {
170 $resource = $action->getTarget()->getUri();
171 $url = common_local_url('webfinger') . '?resource='.urlencode($resource);
173 foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
174 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false);
179 public function onPluginVersion(array &$versions)
181 $versions[] = array('name' => 'WebFinger',
182 'version' => GNUSOCIAL_VERSION,
183 'author' => 'Mikael Nordfeldth',
184 'homepage' => 'http://www.gnu.org/software/social/',
185 // TRANS: Plugin description.
186 'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));