]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/WebFingerPlugin.php
Verify that authenticated API calls are made from our domain name.
[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 $http_alias = false;
39     public $fancyurlfix = true; // adds + interprets some extra aliases related to 'index.php/' URLs
40
41     public function initialize()
42     {
43         common_config_set('webfinger', 'http_alias', $this->http_alias);
44         common_config_set('webfinger', 'fancyurlfix', $this->fancyurlfix);
45     }
46
47     public function onRouterInitialized($m)
48     {
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'));
59         return true;
60     }
61
62     public function onLoginAction($action, &$login)
63     {
64         switch ($action) {
65         case 'hostmeta':
66         case 'webfinger':
67             $login = true;
68             return false;
69         }
70         
71         return true;
72     }
73
74     public function onStartGetProfileAcctUri(Profile $profile, &$acct)
75     {
76         $wfr = new WebFingerResource_Profile($profile);
77         try {
78             $acct = $wfr->reconstructAcct();
79         } catch (Exception $e) {
80             return true;
81         }
82
83         return false;
84     }
85
86     public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
87     {
88         $profile = null;
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));
98                     }
99                     $profile = $user->getProfile();
100                 } else {
101                     throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
102                 }
103             }
104         } else {
105             try {
106                 $user = User::getByUri($resource);
107                 $profile = $user->getProfile();
108             } catch (NoResultException $e) {
109                 if (common_config('webfinger', 'fancyurlfix')) {
110                     try {
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);
117                         }
118
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...
124                     }
125                 }
126             }
127         }
128
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);
133         }
134
135         if ($profile instanceof Profile) {
136             $target = new WebFingerResource_Profile($profile);
137             return false;   // We got our target, stop handler execution
138         }
139
140         $notice = Notice::getKV('uri', $resource);
141         if ($notice instanceof Notice) {
142             $target = new WebFingerResource_Notice($notice);
143             return false;
144         }
145
146         return true;
147     }
148
149     public function onStartHostMetaLinks(array &$links)
150     {
151         foreach (Discovery::supportedMimeTypes() as $type) {
152             $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
153                             common_local_url('webfinger') . '?resource={uri}',
154                             $type,
155                             true);    // isTemplate
156         }
157
158         // OAuth connections
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'));
162     }
163
164     /**
165      * Add a link header for LRDD Discovery
166      */
167     public function onStartShowHTML($action)
168     {
169         if ($action instanceof ShowstreamAction) {
170             $resource = $action->getTarget()->getUri();
171             $url = common_local_url('webfinger') . '?resource='.urlencode($resource);
172
173             foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
174                 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false);
175             }
176         }
177     }
178
179     public function onPluginVersion(array &$versions)
180     {
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'));
187
188         return true;
189     }
190 }