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;
40 public function initialize()
42 common_config_set('webfinger', 'http_alias', $this->http_alias);
45 public function onRouterInitialized($m)
47 $m->connect('.well-known/host-meta', array('action' => 'hostmeta'));
48 $m->connect('.well-known/host-meta.:format',
49 array('action' => 'hostmeta',
50 'format' => '(xml|json)'));
51 // the resource GET parameter can be anywhere, so don't mention it here
52 $m->connect('.well-known/webfinger', array('action' => 'webfinger'));
53 $m->connect('.well-known/webfinger.:format',
54 array('action' => 'webfinger',
55 'format' => '(xml|json)'));
56 $m->connect('main/ownerxrd', array('action' => 'ownerxrd'));
60 public function onLoginAction($action, &$login)
72 public function onStartGetProfileAcctUri(Profile $profile, &$acct)
74 $wfr = new WebFingerResource_Profile($profile);
76 $acct = $wfr->reconstructAcct();
77 } catch (Exception $e) {
84 public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
87 if (Discovery::isAcct($resource)) {
88 $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
89 if (count($parts) == 2) {
90 list($nick, $domain) = $parts;
91 if ($domain === common_config('site', 'server')) {
92 $nick = common_canonical_nickname($nick);
93 $user = User::getKV('nickname', $nick);
94 if (!($user instanceof User)) {
95 throw new NoSuchUserException(array('nickname'=>$nick));
97 $profile = $user->getProfile();
99 throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
104 $user = User::getByUri($resource);
105 $profile = $user->getProfile();
106 } catch (NoResultException $e) {
107 if (common_config('fix', 'fancyurls')) {
109 try { // if it's a /index.php/ url
110 // common_fake_local_fancy_url can throw an exception
111 $alt_url = common_fake_local_fancy_url($resource);
112 } catch (Exception $e) { // let's try to create a fake local /index.php/ url
113 // this too if it can't do anything about the URL
114 $alt_url = common_fake_local_nonfancy_url($resource);
117 // and this will throw a NoResultException if not found
118 $user = User::getByUri($alt_url);
119 $profile = $user->getProfile();
120 } catch (Exception $e) {
121 // apparently we didn't get any matches with that, so continue...
127 // if we still haven't found a match...
128 if (!$profile instanceof Profile) {
129 // if our rewrite hack didn't work, try to get something by profile URL
130 $profile = Profile::getKV('profileurl', $resource);
133 if ($profile instanceof Profile) {
134 $target = new WebFingerResource_Profile($profile);
135 return false; // We got our target, stop handler execution
138 $notice = Notice::getKV('uri', $resource);
139 if ($notice instanceof Notice) {
140 $target = new WebFingerResource_Notice($notice);
147 public function onStartHostMetaLinks(array &$links)
149 foreach (Discovery::supportedMimeTypes() as $type) {
150 $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
151 common_local_url('webfinger') . '?resource={uri}',
157 $links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL, common_local_url('ApiOAuthAccessToken'));
158 $links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
159 $links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL, common_local_url('ApiOAuthAuthorize'));
163 * Add a link header for LRDD Discovery
165 public function onStartShowHTML($action)
167 if ($action instanceof ShowstreamAction) {
168 $resource = $action->getTarget()->getUri();
169 $url = common_local_url('webfinger') . '?resource='.urlencode($resource);
171 foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
172 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false);
177 public function onPluginVersion(array &$versions)
179 $versions[] = array('name' => 'WebFinger',
180 'version' => GNUSOCIAL_VERSION,
181 'author' => 'Mikael Nordfeldth',
182 'homepage' => 'http://www.gnu.org/software/social/',
183 // TRANS: Plugin description.
184 'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));