WebFingerPlugin.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * Implements WebFinger for GNU Social, as well as support for the
  21. * '.well-known/host-meta' resource.
  22. *
  23. * Depends on: LRDD plugin
  24. *
  25. * @package GNUsocial
  26. * @author Mikael Nordfeldth <mmn@hethane.se>
  27. */
  28. if (!defined('GNUSOCIAL')) { exit(1); }
  29. include("../plugins/WebFinger/lib/webfingerresource.php");
  30. include("../plugins/WebFinger/lib/webfingerresource/notice.php");
  31. include("../plugins/WebFinger/lib/webfingerresource/profile.php");
  32. class WebFingerPlugin extends Plugin
  33. {
  34. const PLUGIN_VERSION = '2.0.0';
  35. const OAUTH_ACCESS_TOKEN_REL = 'http://apinamespace.org/oauth/access_token';
  36. const OAUTH_REQUEST_TOKEN_REL = 'http://apinamespace.org/oauth/request_token';
  37. const OAUTH_AUTHORIZE_REL = 'http://apinamespace.org/oauth/authorize';
  38. public function onRouterInitialized(URLMapper $m)
  39. {
  40. $m->connect('.well-known/host-meta', ['action' => 'hostmeta']);
  41. $m->connect('.well-known/host-meta.:format',
  42. ['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', ['action' => 'webfinger']);
  46. $m->connect('.well-known/webfinger.:format',
  47. ['action' => 'webfinger'],
  48. ['format' => '(xml|json)']);
  49. $m->connect('main/ownerxrd', ['action' => 'ownerxrd']);
  50. return true;
  51. }
  52. public function onLoginAction($action, &$login)
  53. {
  54. switch ($action) {
  55. case 'hostmeta':
  56. case 'webfinger':
  57. $login = true;
  58. return false;
  59. }
  60. return true;
  61. }
  62. public function onStartGetProfileAcctUri(Profile $profile, &$acct)
  63. {
  64. $wfr = new WebFingerResource_Profile($profile);
  65. try {
  66. $acct = $wfr->reconstructAcct();
  67. } catch (Exception $e) {
  68. return true;
  69. }
  70. return false;
  71. }
  72. public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
  73. {
  74. $profile = null;
  75. if (Discovery::isAcct($resource)) {
  76. $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
  77. if (count($parts) == 2) {
  78. list($nick, $domain) = $parts;
  79. if ($domain !== common_config('site', 'server')) {
  80. throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
  81. }
  82. $nick = common_canonical_nickname($nick);
  83. $user = User::getKV('nickname', $nick);
  84. if (!($user instanceof User)) {
  85. throw new NoSuchUserException(array('nickname'=>$nick));
  86. }
  87. $profile = $user->getProfile();
  88. }
  89. } elseif (!common_valid_http_url($resource)) {
  90. // If it's not a URL, we can't do our http<->https legacy fix thingie anyway,
  91. // so just try the User URI lookup!
  92. try {
  93. $user = User::getByUri($resource);
  94. $profile = $user->getProfile();
  95. } catch (NoResultException $e) {
  96. // not a User, maybe a Notice? we'll try that further down...
  97. }
  98. } else {
  99. // this means $resource is a common_valid_http_url (or https)
  100. // First build up a set of alternative resource URLs that we can use.
  101. $alt_urls = [$resource => true];
  102. if (strtolower(parse_url($resource, PHP_URL_SCHEME)) === 'https'
  103. && common_config('fix', 'legacy_http')) {
  104. $alt_urls[preg_replace('/^https:/i', 'http:', $resource, 1)] = true;
  105. }
  106. if (common_config('fix', 'fancyurls')) {
  107. foreach (array_keys($alt_urls) as $url) {
  108. try { // if it's a /index.php/ url
  109. // common_fake_local_fancy_url can throw an exception
  110. $alt_url = common_fake_local_fancy_url($url);
  111. } catch (Exception $e) { // let's try to create a fake local /index.php/ url
  112. // this too if it can't do anything about the URL
  113. $alt_url = common_fake_local_nonfancy_url($url);
  114. }
  115. $alt_urls[$alt_url] = true;
  116. }
  117. }
  118. common_debug(__METHOD__.': Generated these alternative URLs for various federation fixes: '._ve(array_keys($alt_urls)));
  119. try {
  120. common_debug(__METHOD__.': Finding User URI for WebFinger lookup on resource=='._ve($resource));
  121. $user = new User();
  122. $user->whereAddIn('uri', array_keys($alt_urls), $user->columnType('uri'));
  123. $user->limit(1);
  124. if ($user->find(true)) {
  125. $profile = $user->getProfile();
  126. }
  127. unset($user);
  128. } catch (Exception $e) {
  129. // Most likely a UserNoProfileException, if it ever happens
  130. // and then we need to do some debugging and perhaps fixes.
  131. common_log(LOG_ERR, get_class($e).': '._ve($e->getMessage()));
  132. throw $e;
  133. }
  134. try {
  135. common_debug(__METHOD__.': Finding User_group URI for WebFinger lookup on resource=='._ve($resource));
  136. $group = new User_group();
  137. $group->whereAddIn('uri', array_keys($alt_urls), $group->columnType('uri'));
  138. $group->limit(1);
  139. if ($group->find(true)) {
  140. $profile = $group->getProfile();
  141. }
  142. unset($group);
  143. } catch (Exception $e) {
  144. common_log(LOG_ERR, get_class($e).': '._ve($e->getMessage()));
  145. throw $e;
  146. }
  147. // User URI did not match, so let's try our alt_urls as Profile URL values
  148. if (!$profile instanceof Profile) {
  149. common_debug(__METHOD__.': Finding Profile URLs for WebFinger lookup on resource=='._ve($resource));
  150. // if our rewrite hack didn't work, try to get something by profile URL
  151. $profile = new Profile();
  152. $profile->whereAddIn('profileurl', array_keys($alt_urls), $profile->columnType('profileurl'));
  153. $profile->limit(1);
  154. if (!$profile->find(true) || !$profile->isLocal()) {
  155. // * Either we didn't find the profile, then we want to make
  156. // the $profile variable null for clarity.
  157. // * Or we did find it but for a possibly malicious remote
  158. // user who might've set their profile URL to a Notice URL
  159. // which would've caused a sort of DoS unless we continue
  160. // our search here by discarding the remote profile.
  161. $profile = null;
  162. }
  163. }
  164. }
  165. if ($profile instanceof Profile) {
  166. common_debug(__METHOD__.': Found Profile with ID=='._ve($profile->getID()).' for resource=='._ve($resource));
  167. $target = new WebFingerResource_Profile($profile);
  168. return false; // We got our target, stop handler execution
  169. }
  170. $notice = Notice::getKV('uri', $resource);
  171. if ($notice instanceof Notice) {
  172. $target = new WebFingerResource_Notice($notice);
  173. return false;
  174. }
  175. return true;
  176. }
  177. public function onStartHostMetaLinks(array &$links)
  178. {
  179. foreach (Discovery::supportedMimeTypes() as $type) {
  180. $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
  181. common_local_url('webfinger') . '?resource={uri}',
  182. $type,
  183. true); // isTemplate
  184. }
  185. // OAuth connections
  186. $links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL, common_local_url('ApiOAuthAccessToken'));
  187. $links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
  188. $links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL, common_local_url('ApiOAuthAuthorize'));
  189. }
  190. /**
  191. * Add a link header for LRDD Discovery
  192. */
  193. public function onStartShowHTML($action)
  194. {
  195. if ($action instanceof ShowstreamAction) {
  196. $resource = $action->getTarget()->getUri();
  197. $url = common_local_url('webfinger') . '?resource='.urlencode($resource);
  198. foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
  199. header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false);
  200. }
  201. }
  202. }
  203. public function onPluginVersion(array &$versions): bool
  204. {
  205. $versions[] = array('name' => 'WebFinger',
  206. 'version' => self::PLUGIN_VERSION,
  207. 'author' => 'Mikael Nordfeldth',
  208. 'homepage' => GNUSOCIAL_ENGINE_URL,
  209. // TRANS: Plugin description.
  210. 'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));
  211. return true;
  212. }
  213. }