WebFingerPlugin.php 9.7 KB

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