]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/WebFingerPlugin.php
Merge branch 'master' into nightly
[quix0rs-gnu-social.git] / plugins / WebFinger / WebFingerPlugin.php
1 <?php
2 /*
3  * GNU Social - a federating social network
4  * Copyright (C) 2013, Free Software Foundation, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * Implements WebFinger for GNU Social, as well as support for the
22  * '.well-known/host-meta' resource.
23  *
24  * Depends on: LRDD plugin
25  *
26  * @package GNUsocial
27  * @author  Mikael Nordfeldth <mmn@hethane.se>
28  */
29
30 if (!defined('GNUSOCIAL')) { exit(1); }
31
32 class WebFingerPlugin extends Plugin
33 {
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';
37
38     public function onRouterInitialized(URLMapper $m)
39     {
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'));
50         return true;
51     }
52
53     public function onLoginAction($action, &$login)
54     {
55         switch ($action) {
56         case 'hostmeta':
57         case 'webfinger':
58             $login = true;
59             return false;
60         }
61         
62         return true;
63     }
64
65     public function onStartGetProfileAcctUri(Profile $profile, &$acct)
66     {
67         $wfr = new WebFingerResource_Profile($profile);
68         try {
69             $acct = $wfr->reconstructAcct();
70         } catch (Exception $e) {
71             return true;
72         }
73
74         return false;
75     }
76
77     public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
78     {
79         $profile = null;
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.'));
86                 }
87
88                 $nick = common_canonical_nickname($nick);
89                 $user = User::getKV('nickname', $nick);
90                 if (!($user instanceof User)) {
91                     throw new NoSuchUserException(array('nickname'=>$nick));
92                 }
93                 $profile = $user->getProfile();
94             }
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!
98             try {
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...
103             }
104         } else {
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;
111             }
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);
120                     }
121
122                     $alt_urls[$alt_url] = true;
123                 }
124             }
125             common_debug(__METHOD__.': Generated these alternative URLs for various federation fixes: '._ve(array_keys($alt_urls)));
126
127             try {
128                 common_debug(__METHOD__.': Finding User URI for WebFinger lookup on resource=='._ve($resource));
129                 $user = new User();
130                 $user->whereAddIn('uri', array_keys($alt_urls), $user->columnType('uri'));
131                 $user->limit(1);
132                 if ($user->find(true)) {
133                     $profile = $user->getProfile();
134                 }
135                 unset($user);
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()));
140                 throw $e;
141             }
142
143             try {
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'));
147                 $group->limit(1);
148                 if ($group->find(true)) {
149                     $profile = $group->getProfile();
150                 }
151                 unset($group);
152             } catch (Exception $e) {
153                 common_log(LOG_ERR, get_class($e).': '._ve($e->getMessage()));
154                 throw $e;
155             }
156
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'));
163                 $profile->limit(1);
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.
171                     $profile = null;
172                 }
173             }
174         }
175
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
180         }
181
182         $notice = Notice::getKV('uri', $resource);
183         if ($notice instanceof Notice) {
184             $target = new WebFingerResource_Notice($notice);
185             return false;
186         }
187
188         return true;
189     }
190
191     public function onStartHostMetaLinks(array &$links)
192     {
193         foreach (Discovery::supportedMimeTypes() as $type) {
194             $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
195                             common_local_url('webfinger') . '?resource={uri}',
196                             $type,
197                             true);    // isTemplate
198         }
199
200         // OAuth connections
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'));
204     }
205
206     /**
207      * Add a link header for LRDD Discovery
208      */
209     public function onStartShowHTML($action)
210     {
211         if ($action instanceof ShowstreamAction) {
212             $resource = $action->getTarget()->getUri();
213             $url = common_local_url('webfinger') . '?resource='.urlencode($resource);
214
215             foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
216                 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false);
217             }
218         }
219     }
220
221     public function onPluginVersion(array &$versions)
222     {
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'));
229
230         return true;
231     }
232 }