OpenIDPlugin.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * PHP version 5
  6. *
  7. * LICENCE: This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Plugin
  21. * @package StatusNet
  22. * @author Evan Prodromou <evan@status.net>
  23. * @author Craig Andrews <candrews@integralblue.com>
  24. * @copyright 2009-2010 StatusNet, Inc.
  25. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Plugin for OpenID authentication and identity
  34. *
  35. * This class enables consumer support for OpenID, the distributed authentication
  36. * and identity system.
  37. *
  38. * Depends on: WebFinger plugin for HostMeta-lookup (user@host format)
  39. *
  40. * @category Plugin
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Craig Andrews <candrews@integralblue.com>
  44. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. * @link http://openid.net/
  48. */
  49. class OpenIDPlugin extends Plugin
  50. {
  51. // Plugin parameter: set true to disallow non-OpenID logins
  52. // If set, overrides the setting in database or $config['site']['openidonly']
  53. public $openidOnly = null;
  54. function initialize()
  55. {
  56. parent::initialize();
  57. if ($this->openidOnly !== null) {
  58. global $config;
  59. $config['site']['openidonly'] = (bool)$this->openidOnly;
  60. }
  61. }
  62. /**
  63. * Add OpenID-related paths to the router table
  64. *
  65. * Hook for RouterInitialized event.
  66. *
  67. * @param URLMapper $m URL mapper
  68. *
  69. * @return boolean hook return
  70. */
  71. public function onStartInitializeRouter(URLMapper $m)
  72. {
  73. $m->connect('main/openid', array('action' => 'openidlogin'));
  74. $m->connect('main/openidtrust', array('action' => 'openidtrust'));
  75. $m->connect('settings/openid', array('action' => 'openidsettings'));
  76. $m->connect('index.php?action=finishopenidlogin',
  77. array('action' => 'finishopenidlogin'));
  78. $m->connect('index.php?action=finishaddopenid',
  79. array('action' => 'finishaddopenid'));
  80. $m->connect('main/openidserver', array('action' => 'openidserver'));
  81. $m->connect('panel/openid', array('action' => 'openidadminpanel'));
  82. return true;
  83. }
  84. /**
  85. * In OpenID-only mode, disable paths for password stuff
  86. *
  87. * @param string $path path to connect
  88. * @param array $defaults path defaults
  89. * @param array $rules path rules
  90. * @param array $result unused
  91. *
  92. * @return boolean hook return
  93. */
  94. function onStartConnectPath(&$path, &$defaults, &$rules, &$result)
  95. {
  96. if (common_config('site', 'openidonly')) {
  97. // Note that we should not remove the login and register
  98. // actions. Lots of auth-related things link to them,
  99. // such as when visiting a private site without a session
  100. // or revalidating a remembered login for admin work.
  101. //
  102. // We take those two over with redirects to ourselves
  103. // over in onArgsInitialize().
  104. static $block = array('main/recoverpassword',
  105. 'settings/password');
  106. if (in_array($path, $block)) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. /**
  113. * If we've been hit with password-login args, redirect
  114. *
  115. * @param array $args args (URL, Get, post)
  116. *
  117. * @return boolean hook return
  118. */
  119. function onArgsInitialize($args)
  120. {
  121. if (common_config('site', 'openidonly')) {
  122. if (array_key_exists('action', $args)) {
  123. $action = trim($args['action']);
  124. if (in_array($action, array('login', 'register'))) {
  125. common_redirect(common_local_url('openidlogin'));
  126. } else if ($action == 'passwordsettings') {
  127. common_redirect(common_local_url('openidsettings'));
  128. } else if ($action == 'recoverpassword') {
  129. // TRANS: Client exception thrown when an action is not available.
  130. throw new ClientException(_m('Unavailable action.'));
  131. }
  132. }
  133. }
  134. return true;
  135. }
  136. /**
  137. * Public XRDS output hook
  138. *
  139. * Puts the bits of code needed by some OpenID providers to show
  140. * we're good citizens.
  141. *
  142. * @param Action $action Action being executed
  143. * @param XMLOutputter &$xrdsOutputter Output channel
  144. *
  145. * @return boolean hook return
  146. */
  147. function onEndPublicXRDS(Action $action, &$xrdsOutputter)
  148. {
  149. $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
  150. 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
  151. 'version' => '2.0'));
  152. $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
  153. //consumer
  154. foreach (array('finishopenidlogin', 'finishaddopenid') as $finish) {
  155. $xrdsOutputter->showXrdsService(Auth_OpenID_RP_RETURN_TO_URL_TYPE,
  156. common_local_url($finish));
  157. }
  158. //provider
  159. $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/server',
  160. common_local_url('openidserver'),
  161. null,
  162. null,
  163. 'http://specs.openid.net/auth/2.0/identifier_select');
  164. $xrdsOutputter->elementEnd('XRD');
  165. }
  166. /**
  167. * If we're in OpenID-only mode, hide all the main menu except OpenID login.
  168. *
  169. * @param Action $action Action being run
  170. *
  171. * @return boolean hook return
  172. */
  173. function onStartPrimaryNav($action)
  174. {
  175. if (common_config('site', 'openidonly') && !common_logged_in()) {
  176. // TRANS: Tooltip for main menu option "Login"
  177. $tooltip = _m('TOOLTIP', 'Login to the site.');
  178. $action->menuItem(common_local_url('openidlogin'),
  179. // TRANS: Main menu option when not logged in to log in
  180. _m('MENU', 'Login'),
  181. $tooltip,
  182. false,
  183. 'nav_login');
  184. // TRANS: Tooltip for main menu option "Help"
  185. $tooltip = _m('TOOLTIP', 'Help me!');
  186. $action->menuItem(common_local_url('doc', array('title' => 'help')),
  187. // TRANS: Main menu option for help on the StatusNet site
  188. _m('MENU', 'Help'),
  189. $tooltip,
  190. false,
  191. 'nav_help');
  192. if (!common_config('site', 'private')) {
  193. // TRANS: Tooltip for main menu option "Search"
  194. $tooltip = _m('TOOLTIP', 'Search for people or text.');
  195. $action->menuItem(common_local_url('peoplesearch'),
  196. // TRANS: Main menu option when logged in or when the StatusNet instance is not private
  197. _m('MENU', 'Search'), $tooltip, false, 'nav_search');
  198. }
  199. Event::handle('EndPrimaryNav', array($action));
  200. return false;
  201. }
  202. return true;
  203. }
  204. /**
  205. * Menu for login
  206. *
  207. * If we're in openidOnly mode, we disable the menu for all other login.
  208. *
  209. * @param Action $action Action being executed
  210. *
  211. * @return boolean hook return
  212. */
  213. function onStartLoginGroupNav($action)
  214. {
  215. if (common_config('site', 'openidonly')) {
  216. $this->showOpenIDLoginTab($action);
  217. // Even though we replace this code, we
  218. // DON'T run the End* hook, to keep others from
  219. // adding tabs. Not nice, but.
  220. return false;
  221. }
  222. return true;
  223. }
  224. /**
  225. * Menu item for login
  226. *
  227. * @param Action $action Action being executed
  228. *
  229. * @return boolean hook return
  230. */
  231. function onEndLoginGroupNav($action)
  232. {
  233. $this->showOpenIDLoginTab($action);
  234. return true;
  235. }
  236. /**
  237. * Show menu item for login
  238. *
  239. * @param Action $action Action being executed
  240. *
  241. * @return void
  242. */
  243. function showOpenIDLoginTab($action)
  244. {
  245. $action_name = $action->trimmed('action');
  246. $action->menuItem(common_local_url('openidlogin'),
  247. // TRANS: OpenID plugin menu item on site logon page.
  248. _m('MENU', 'OpenID'),
  249. // TRANS: OpenID plugin tooltip for logon menu item.
  250. _m('Login or register with OpenID.'),
  251. $action_name === 'openidlogin');
  252. }
  253. /**
  254. * Show menu item for password
  255. *
  256. * We hide it in openID-only mode
  257. *
  258. * @param Action $menu Widget for menu
  259. * @param void &$unused Unused value
  260. *
  261. * @return void
  262. */
  263. function onStartAccountSettingsPasswordMenuItem($menu, &$unused) {
  264. if (common_config('site', 'openidonly')) {
  265. return false;
  266. }
  267. return true;
  268. }
  269. /**
  270. * Menu item for OpenID settings
  271. *
  272. * @param Action $action Action being executed
  273. *
  274. * @return boolean hook return
  275. */
  276. function onEndAccountSettingsNav($action)
  277. {
  278. $action_name = $action->trimmed('action');
  279. $action->menuItem(common_local_url('openidsettings'),
  280. // TRANS: OpenID plugin menu item on user settings page.
  281. _m('MENU', 'OpenID'),
  282. // TRANS: OpenID plugin tooltip for user settings menu item.
  283. _m('Add or remove OpenIDs.'),
  284. $action_name === 'openidsettings');
  285. return true;
  286. }
  287. /**
  288. * Autoloader
  289. *
  290. * Loads our classes if they're requested.
  291. *
  292. * @param string $cls Class requested
  293. *
  294. * @return boolean hook return
  295. */
  296. function onAutoload($cls)
  297. {
  298. switch ($cls)
  299. {
  300. case 'Auth_OpenID_TeamsExtension':
  301. case 'Auth_OpenID_TeamsRequest':
  302. case 'Auth_OpenID_TeamsResponse':
  303. require_once dirname(__FILE__) . '/extlib/teams-extension.php';
  304. return false;
  305. }
  306. return parent::onAutoload($cls);
  307. }
  308. /**
  309. * Login actions
  310. *
  311. * These actions should be visible even when the site is marked private
  312. *
  313. * @param Action $action Action to show
  314. * @param boolean &$login Whether it's a login action
  315. *
  316. * @return boolean hook return
  317. */
  318. function onLoginAction($action, &$login)
  319. {
  320. switch ($action)
  321. {
  322. case 'openidlogin':
  323. case 'finishopenidlogin':
  324. case 'openidserver':
  325. $login = true;
  326. return false;
  327. default:
  328. return true;
  329. }
  330. }
  331. /**
  332. * We include a <meta> element linking to the webfinger resource page,
  333. * for OpenID client-side authentication.
  334. *
  335. * @param Action $action Action being shown
  336. *
  337. * @return void
  338. */
  339. function onEndShowHeadElements(Action $action)
  340. {
  341. if ($action instanceof ShowstreamAction) {
  342. $action->element('link', array('rel' => 'openid2.provider',
  343. 'href' => common_local_url('openidserver')));
  344. $action->element('link', array('rel' => 'openid2.local_id',
  345. 'href' => $action->getTarget()->getUrl()));
  346. $action->element('link', array('rel' => 'openid.server',
  347. 'href' => common_local_url('openidserver')));
  348. $action->element('link', array('rel' => 'openid.delegate',
  349. 'href' => $action->getTarget()->getUrl()));
  350. }
  351. if ($action instanceof SitestreamAction) {
  352. $action->element('meta', array('http-equiv' => 'X-XRDS-Location',
  353. 'content' => common_local_url('publicxrds')));
  354. }
  355. return true;
  356. }
  357. /**
  358. * Redirect to OpenID login if they have an OpenID
  359. *
  360. * @param Action $action Action being executed
  361. * @param User $user User doing the action
  362. *
  363. * @return boolean whether to continue
  364. */
  365. function onRedirectToLogin($action, $user)
  366. {
  367. if (common_config('site', 'openidonly') || (!empty($user) && User_openid::hasOpenID($user->id))) {
  368. common_redirect(common_local_url('openidlogin'), 303);
  369. }
  370. return true;
  371. }
  372. /**
  373. * Show some extra instructions for using OpenID
  374. *
  375. * @param Action $action Action being executed
  376. *
  377. * @return boolean hook value
  378. */
  379. function onEndShowPageNotice($action)
  380. {
  381. $name = $action->trimmed('action');
  382. switch ($name)
  383. {
  384. case 'register':
  385. if (common_logged_in()) {
  386. // TRANS: Page notice for logged in users to try and get them to add an OpenID account to their StatusNet account.
  387. // TRANS: This message contains Markdown links in the form (description)[link].
  388. $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
  389. '[Add an OpenID to your account](%%action.openidsettings%%)!');
  390. } else {
  391. // TRANS: Page notice for anonymous users to try and get them to register with an OpenID account.
  392. // TRANS: This message contains Markdown links in the form (description)[link].
  393. $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
  394. 'Try our [OpenID registration]'.
  395. '(%%action.openidlogin%%)!)');
  396. }
  397. break;
  398. case 'login':
  399. // TRANS: Page notice on the login page to try and get them to log on with an OpenID account.
  400. // TRANS: This message contains Markdown links in the form (description)[link].
  401. $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
  402. 'Try our [OpenID login]'.
  403. '(%%action.openidlogin%%)!)');
  404. break;
  405. default:
  406. return true;
  407. }
  408. $output = common_markup_to_html($instr);
  409. $action->raw($output);
  410. return true;
  411. }
  412. /**
  413. * Load our document if requested
  414. *
  415. * @param string &$title Title to fetch
  416. * @param string &$output HTML to output
  417. *
  418. * @return boolean hook value
  419. */
  420. function onStartLoadDoc(&$title, &$output)
  421. {
  422. if ($title == 'openid') {
  423. $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
  424. $c = file_get_contents($filename);
  425. $output = common_markup_to_html($c);
  426. return false; // success!
  427. }
  428. return true;
  429. }
  430. /**
  431. * Add our document to the global menu
  432. *
  433. * @param string $title Title being fetched
  434. * @param string &$output HTML being output
  435. *
  436. * @return boolean hook value
  437. */
  438. function onEndDocsMenu(&$items) {
  439. $items[] = array('doc',
  440. array('title' => 'openid'),
  441. _m('MENU', 'OpenID'),
  442. _('Logging in with OpenID'),
  443. 'nav_doc_openid');
  444. return true;
  445. }
  446. /**
  447. * Data definitions
  448. *
  449. * Assure that our data objects are available in the DB
  450. *
  451. * @return boolean hook value
  452. */
  453. function onCheckSchema()
  454. {
  455. $schema = Schema::get();
  456. $schema->ensureTable('user_openid', User_openid::schemaDef());
  457. $schema->ensureTable('user_openid_trustroot', User_openid_trustroot::schemaDef());
  458. $schema->ensureTable('user_openid_prefs', User_openid_prefs::schemaDef());
  459. /* These are used by JanRain OpenID library */
  460. $schema->ensureTable('oid_associations',
  461. array(
  462. 'fields' => array(
  463. 'server_url' => array('type' => 'blob', 'not null' => true),
  464. 'handle' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'default' => ''), // character set latin1,
  465. 'secret' => array('type' => 'blob'),
  466. 'issued' => array('type' => 'int'),
  467. 'lifetime' => array('type' => 'int'),
  468. 'assoc_type' => array('type' => 'varchar', 'length' => 64),
  469. ),
  470. 'primary key' => array(array('server_url', 191), 'handle'),
  471. ));
  472. $schema->ensureTable('oid_nonces',
  473. array(
  474. 'fields' => array(
  475. 'server_url' => array('type' => 'varchar', 'length' => 2047),
  476. 'timestamp' => array('type' => 'int'),
  477. 'salt' => array('type' => 'char', 'length' => 40),
  478. ),
  479. 'unique keys' => array(
  480. 'oid_nonces_server_url_timestamp_salt_key' => array(array('server_url', 191), 'timestamp', 'salt'),
  481. ),
  482. ));
  483. return true;
  484. }
  485. /**
  486. * Add our tables to be deleted when a user is deleted
  487. *
  488. * @param User $user User being deleted
  489. * @param array &$tables Array of table names
  490. *
  491. * @return boolean hook value
  492. */
  493. function onUserDeleteRelated($user, &$tables)
  494. {
  495. $tables[] = 'User_openid';
  496. $tables[] = 'User_openid_trustroot';
  497. return true;
  498. }
  499. /**
  500. * Add an OpenID tab to the admin panel
  501. *
  502. * @param Widget $nav Admin panel nav
  503. *
  504. * @return boolean hook value
  505. */
  506. function onEndAdminPanelNav($nav)
  507. {
  508. if (AdminPanelAction::canAdmin('openid')) {
  509. $action_name = $nav->action->trimmed('action');
  510. $nav->out->menuItem(
  511. common_local_url('openidadminpanel'),
  512. // TRANS: OpenID configuration menu item.
  513. _m('MENU','OpenID'),
  514. // TRANS: Tooltip for OpenID configuration menu item.
  515. _m('OpenID configuration.'),
  516. $action_name == 'openidadminpanel',
  517. 'nav_openid_admin_panel'
  518. );
  519. }
  520. return true;
  521. }
  522. /**
  523. * Add OpenID information to the Account Management Control Document
  524. * Event supplied by the Account Manager plugin
  525. *
  526. * @param array &$amcd Array that expresses the AMCD
  527. *
  528. * @return boolean hook value
  529. */
  530. function onEndAccountManagementControlDocument(&$amcd)
  531. {
  532. $amcd['auth-methods']['openid'] = array(
  533. 'connect' => array(
  534. 'method' => 'POST',
  535. 'path' => common_local_url('openidlogin'),
  536. 'params' => array(
  537. 'identity' => 'openid_url'
  538. )
  539. )
  540. );
  541. }
  542. /**
  543. * Add our version information to output
  544. *
  545. * @param array &$versions Array of version-data arrays
  546. *
  547. * @return boolean hook value
  548. */
  549. function onPluginVersion(array &$versions)
  550. {
  551. $versions[] = array('name' => 'OpenID',
  552. 'version' => GNUSOCIAL_VERSION,
  553. 'author' => 'Evan Prodromou, Craig Andrews',
  554. 'homepage' => 'http://status.net/wiki/Plugin:OpenID',
  555. 'rawdescription' =>
  556. // TRANS: Plugin description.
  557. _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));
  558. return true;
  559. }
  560. function onStartOAuthLoginForm($action, &$button)
  561. {
  562. if (common_config('site', 'openidonly')) {
  563. // Cancel the regular password login form, we won't need it.
  564. $this->showOAuthLoginForm($action);
  565. // TRANS: button label for OAuth authorization page when needing OpenID authentication first.
  566. $button = _m('BUTTON', 'Continue');
  567. return false;
  568. } else {
  569. // Leave the regular password login form in place.
  570. // We'll add an OpenID link at bottom...?
  571. return true;
  572. }
  573. }
  574. /**
  575. * @fixme merge with common code for main OpenID login form
  576. * @param HTMLOutputter $action
  577. */
  578. protected function showOAuthLoginForm($action)
  579. {
  580. $action->elementStart('fieldset');
  581. // TRANS: OpenID plugin logon form legend.
  582. $action->element('legend', null, _m('LEGEND','OpenID login'));
  583. $action->elementStart('ul', 'form_data');
  584. $action->elementStart('li');
  585. $provider = common_config('openid', 'trusted_provider');
  586. $appendUsername = common_config('openid', 'append_username');
  587. if ($provider) {
  588. // TRANS: Field label.
  589. $action->element('label', array(), _m('OpenID provider'));
  590. $action->element('span', array(), $provider);
  591. if ($appendUsername) {
  592. $action->element('input', array('id' => 'openid_username',
  593. 'name' => 'openid_username',
  594. 'style' => 'float: none'));
  595. }
  596. $action->element('p', 'form_guide',
  597. // TRANS: Form guide.
  598. ($appendUsername ? _m('Enter your username.') . ' ' : '') .
  599. // TRANS: Form guide.
  600. _m('You will be sent to the provider\'s site for authentication.'));
  601. $action->hidden('openid_url', $provider);
  602. } else {
  603. // TRANS: OpenID plugin logon form field label.
  604. $action->input('openid_url', _m('OpenID URL'),
  605. '',
  606. // TRANS: OpenID plugin logon form field instructions.
  607. _m('Your OpenID URL.'));
  608. }
  609. $action->elementEnd('li');
  610. $action->elementEnd('ul');
  611. $action->elementEnd('fieldset');
  612. }
  613. /**
  614. * Handle a POST user credential check in apioauthauthorization.
  615. * If given an OpenID URL, we'll pass us over to the regular things
  616. * and then redirect back here on completion.
  617. *
  618. * @fixme merge with common code for main OpenID login form
  619. * @param HTMLOutputter $action
  620. */
  621. function onStartOAuthLoginCheck($action, &$user)
  622. {
  623. $provider = common_config('openid', 'trusted_provider');
  624. if ($provider) {
  625. $openid_url = $provider;
  626. if (common_config('openid', 'append_username')) {
  627. $openid_url .= $action->trimmed('openid_username');
  628. }
  629. } else {
  630. $openid_url = $action->trimmed('openid_url');
  631. }
  632. if ($openid_url) {
  633. require_once dirname(__FILE__) . '/openid.php';
  634. oid_assert_allowed($openid_url);
  635. $returnto = common_local_url(
  636. 'ApiOAuthAuthorize',
  637. array(),
  638. array(
  639. 'oauth_token' => $action->arg('oauth_token'),
  640. 'mode' => $action->arg('mode')
  641. )
  642. );
  643. common_set_returnto($returnto);
  644. // This will redirect if functional...
  645. $result = oid_authenticate($openid_url,
  646. 'finishopenidlogin');
  647. if (is_string($result)) { # error message
  648. throw new ServerException($result);
  649. } else {
  650. exit(0);
  651. }
  652. }
  653. return true;
  654. }
  655. /**
  656. * Add link in user's XRD file to allow OpenID login.
  657. *
  658. * This link in the XRD should let users log in with their
  659. * Webfinger identity to services that support it. See
  660. * http://webfinger.org/login for an example.
  661. *
  662. * @param XML_XRD $xrd Currently-displaying resource descriptor
  663. * @param Profile $target The profile that it's for
  664. *
  665. * @return boolean hook value (always true)
  666. */
  667. function onEndWebFingerProfileLinks(XML_XRD $xrd, Profile $target)
  668. {
  669. $xrd->links[] = new XML_XRD_Element_Link(
  670. 'http://specs.openid.net/auth/2.0/provider',
  671. $target->profileurl);
  672. return true;
  673. }
  674. /**
  675. * Add links in the user's profile block to their OpenID URLs.
  676. *
  677. * @param Profile $profile The profile being shown
  678. * @param Array &$links Writeable array of arrays (href, text, image).
  679. *
  680. * @return boolean hook value (true)
  681. */
  682. function onOtherAccountProfiles($profile, &$links)
  683. {
  684. $prefs = User_openid_prefs::getKV('user_id', $profile->id);
  685. if (empty($prefs) || !$prefs->hide_profile_link) {
  686. $oid = new User_openid();
  687. $oid->user_id = $profile->id;
  688. if ($oid->find()) {
  689. while ($oid->fetch()) {
  690. $links[] = array('href' => $oid->display,
  691. 'text' => _('OpenID'),
  692. 'image' => $this->path("icons/openid-16x16.gif"));
  693. }
  694. }
  695. }
  696. return true;
  697. }
  698. }