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 $http_alias = false;
40 public function initialize()
42 common_config_set('webfinger', 'http_alias', $this->http_alias);
45 public function onRouterInitialized(URLMapper $m)
47 $m->connect('.well-known/host-meta', array('action' => 'hostmeta'));
48 $m->connect('.well-known/host-meta.:format',
49 array('action' => 'hostmeta',
50 'format' => '(xml|json)'));
51 // the resource GET parameter can be anywhere, so don't mention it here
52 $m->connect('.well-known/webfinger', array('action' => 'webfinger'));
53 $m->connect('.well-known/webfinger.:format',
54 array('action' => 'webfinger',
55 'format' => '(xml|json)'));
56 $m->connect('main/ownerxrd', array('action' => 'ownerxrd'));
60 public function onLoginAction($action, &$login)
72 public function onStartGetProfileAcctUri(Profile $profile, &$acct)
74 $wfr = new WebFingerResource_Profile($profile);
76 $acct = $wfr->reconstructAcct();
77 } catch (Exception $e) {
84 public function onEndGetWebFingerResource($resource, WebFingerResource &$target=null, array $args=array())
87 if (Discovery::isAcct($resource)) {
88 $parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
89 if (count($parts) == 2) {
90 list($nick, $domain) = $parts;
91 if ($domain === common_config('site', 'server')) {
92 $nick = common_canonical_nickname($nick);
93 $user = User::getKV('nickname', $nick);
94 if (!($user instanceof User)) {
95 throw new NoSuchUserException(array('nickname'=>$nick));
97 $profile = $user->getProfile();
99 throw new Exception(_('Remote profiles not supported via WebFinger yet.'));
103 $user = User::getKV('uri', $resource);
104 if ($user instanceof User) {
105 $profile = $user->getProfile();
107 // try and get it by profile url
108 $profile = Profile::getKV('profileurl', $resource);
112 if ($profile instanceof Profile) {
113 $target = new WebFingerResource_Profile($profile);
114 return false; // We got our target, stop handler execution
117 $notice = Notice::getKV('uri', $resource);
118 if ($notice instanceof Notice) {
119 $target = new WebFingerResource_Notice($notice);
126 public function onStartHostMetaLinks(array &$links)
128 foreach (Discovery::supportedMimeTypes() as $type) {
129 $links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
130 common_local_url('webfinger') . '?resource={uri}',
136 $links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL, common_local_url('ApiOAuthAccessToken'));
137 $links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
138 $links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL, common_local_url('ApiOAuthAuthorize'));
142 * Add a link header for LRDD Discovery
144 public function onStartShowHTML(Action $action)
146 if ($action instanceof ShowstreamAction) {
147 $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
148 $url = common_local_url('webfinger') . '?resource='.$acct;
150 foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {
151 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"');
156 public function onPluginVersion(array &$versions)
158 $versions[] = array('name' => 'WebFinger',
159 'version' => GNUSOCIAL_VERSION,
160 'author' => 'Mikael Nordfeldth',
161 'homepage' => 'http://www.gnu.org/software/social/',
162 // TRANS: Plugin description.
163 'rawdescription' => _m('Adds WebFinger lookup to GNU Social'));