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 function onRouterInitialized($m)
40 $m->connect('.well-known/host-meta', array('action' => 'hostmeta'));
41 $m->connect('.well-known/host-meta.:format',
42 array('action' => 'hostmeta',
43 'format' => '(xml|json)'));
44 // the resource GET parameter can be anywhere, so don't mention it here
45 $m->connect('.well-known/webfinger', array('action' => 'webfinger'));
46 $m->connect('.well-known/webfinger.:format',
47 array('action' => 'webfinger',
48 'format' => '(xml|json)'));
49 $m->connect('main/ownerxrd', array('action' => 'ownerxrd'));
53 public function onLoginAction($action, &$login)
65 public function onStartGetProfileAcctUri(Profile $profile, &$acct)
67 $wfr = new WebFingerResource_Profile($profile);
69 $acct = $wfr->reconstructAcct();
70 } catch (Exception $e) {
77 public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
80 if (Discovery::isAcct($resource)) {
81 $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
82 if (count($parts) == 2) {
83 list($nick, $domain) = $parts;
84 if ($domain !== common_config('site', 'server')) {
85 throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
88 $nick = common_canonical_nickname($nick);
89 $user = User::getKV('nickname', $nick);
90 if (!($user instanceof User)) {
91 throw new NoSuchUserException(array('nickname'=>$nick));
93 $profile = $user->getProfile();
95 } elseif (!common_valid_http_url($resource)) {
96 // If it's not a URL, we can't do our http<->https legacy fix thingie anyway,
97 // so just try the User URI lookup!
99 $user = User::getByUri($resource);
100 $profile = $user->getProfile();
101 } catch (NoResultException $e) {
102 // not a User, maybe a Notice? we'll try that further down...
105 // this means $resource is a common_valid_http_url (or https)
106 // First build up a set of alternative resource URLs that we can use.
107 $alt_urls = [$resource => true];
108 if (strtolower(parse_url($resource, PHP_URL_SCHEME)) === 'https'
109 && common_config('fix', 'legacy_http')) {
110 $alt_urls[preg_replace('/^https:/i', 'http:', $resource, 1)] = true;
112 if (common_config('fix', 'fancyurls')) {
113 foreach (array_keys($alt_urls) as $url) {
114 try { // if it's a /index.php/ url
115 // common_fake_local_fancy_url can throw an exception
116 $alt_url = common_fake_local_fancy_url($url);
117 } catch (Exception $e) { // let's try to create a fake local /index.php/ url
118 // this too if it can't do anything about the URL
119 $alt_url = common_fake_local_nonfancy_url($url);
122 $alt_urls[$alt_url] = true;
125 common_debug(__METHOD__.': Generated these alternative URLs for various federation fixes: '._ve(array_keys($alt_urls)));
128 common_debug(__METHOD__.': Finding User URI for WebFinger lookup on resource=='._ve($resource));
130 $user->whereAddIn('uri', array_keys($alt_urls), $user->columnType('uri'));
132 if ($user->find(true)) {
133 $profile = $user->getProfile();
136 } catch (Exception $e) {
137 // Most likely a UserNoProfileException, if it ever happens
138 // and then we need to do some debugging and perhaps fixes.
139 common_log(LOG_ERR, get_class($e).': '._ve($e->getMessage()));
144 common_debug(__METHOD__.': Finding User_group URI for WebFinger lookup on resource=='._ve($resource));
145 $group = new User_group();
146 $group->whereAddIn('uri', array_keys($alt_urls), $group->columnType('uri'));
148 if ($group->find(true)) {
149 $profile = $group->getProfile();
152 } catch (Exception $e) {
153 common_log(LOG_ERR, get_class($e).': '._ve($e->getMessage()));
157 // User URI did not match, so let's try our alt_urls as Profile URL values
158 if (!$profile instanceof Profile) {
159 common_debug(__METHOD__.': Finding Profile URLs for WebFinger lookup on resource=='._ve($resource));
160 // if our rewrite hack didn't work, try to get something by profile URL
161 $profile = new Profile();
162 $profile->whereAddIn('profileurl', array_keys($alt_urls), $profile->columnType('profileurl'));
164 if (!$profile->find(true) || !$profile->isLocal()) {
165 // * Either we didn't find the profile, then we want to make
166 // the $profile variable null for clarity.
167 // * Or we did find it but for a possibly malicious remote
168 // user who might've set their profile URL to a Notice URL
169 // which would've caused a sort of DoS unless we continue
170 // our search here by discarding the remote profile.
176 if ($profile instanceof Profile) {
177 common_debug(__METHOD__.': Found Profile with ID=='._ve($profile->getID()).' for resource=='._ve($resource));
178 $target = new WebFingerResource_Profile($profile);
179 return false; // We got our target, stop handler execution
182 $notice = Notice::getKV('uri', $resource);
183 if ($notice instanceof Notice) {
184 $target = new WebFingerResource_Notice($notice);
191 public function onStartHostMetaLinks(array &$links)
193 foreach (Discovery::supportedMimeTypes() as $type) {
194 $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
195 common_local_url('webfinger') . '?resource={uri}',
201 $links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL, common_local_url('ApiOAuthAccessToken'));
202 $links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
203 $links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL, common_local_url('ApiOAuthAuthorize'));
207 * Add a link header for LRDD Discovery
209 public function onStartShowHTML($action)
211 if ($action instanceof ShowstreamAction) {
212 $resource = $action->getTarget()->getUri();
213 $url = common_local_url('webfinger') . '?resource='.urlencode($resource);
215 foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
216 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false);
221 public function onPluginVersion(array &$versions)
223 $versions[] = array('name' => 'WebFinger',
224 'version' => GNUSOCIAL_VERSION,
225 'author' => 'Mikael Nordfeldth',
226 'homepage' => 'http://www.gnu.org/software/social/',
227 // TRANS: Plugin description.
228 'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));