OpenIDPlugin.php 26 KB

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