OpenIDPlugin.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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, &$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. * User XRDS output hook
  168. *
  169. * Puts the bits of code needed to discover OpenID endpoints.
  170. *
  171. * @param Action $action Action being executed
  172. * @param XMLOutputter &$xrdsOutputter Output channel
  173. *
  174. * @return boolean hook return
  175. */
  176. function onEndUserXRDS($action, &$xrdsOutputter)
  177. {
  178. $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
  179. 'xml:id' => 'openid',
  180. 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
  181. 'version' => '2.0'));
  182. $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
  183. //consumer
  184. $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/return_to',
  185. common_local_url('finishopenidlogin'));
  186. //provider
  187. $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/signon',
  188. common_local_url('openidserver'),
  189. null,
  190. null,
  191. common_profile_url($action->user->nickname));
  192. $xrdsOutputter->elementEnd('XRD');
  193. }
  194. /**
  195. * If we're in OpenID-only mode, hide all the main menu except OpenID login.
  196. *
  197. * @param Action $action Action being run
  198. *
  199. * @return boolean hook return
  200. */
  201. function onStartPrimaryNav($action)
  202. {
  203. if (common_config('site', 'openidonly') && !common_logged_in()) {
  204. // TRANS: Tooltip for main menu option "Login"
  205. $tooltip = _m('TOOLTIP', 'Login to the site.');
  206. $action->menuItem(common_local_url('openidlogin'),
  207. // TRANS: Main menu option when not logged in to log in
  208. _m('MENU', 'Login'),
  209. $tooltip,
  210. false,
  211. 'nav_login');
  212. // TRANS: Tooltip for main menu option "Help"
  213. $tooltip = _m('TOOLTIP', 'Help me!');
  214. $action->menuItem(common_local_url('doc', array('title' => 'help')),
  215. // TRANS: Main menu option for help on the StatusNet site
  216. _m('MENU', 'Help'),
  217. $tooltip,
  218. false,
  219. 'nav_help');
  220. if (!common_config('site', 'private')) {
  221. // TRANS: Tooltip for main menu option "Search"
  222. $tooltip = _m('TOOLTIP', 'Search for people or text.');
  223. $action->menuItem(common_local_url('peoplesearch'),
  224. // TRANS: Main menu option when logged in or when the StatusNet instance is not private
  225. _m('MENU', 'Search'), $tooltip, false, 'nav_search');
  226. }
  227. Event::handle('EndPrimaryNav', array($action));
  228. return false;
  229. }
  230. return true;
  231. }
  232. /**
  233. * Menu for login
  234. *
  235. * If we're in openidOnly mode, we disable the menu for all other login.
  236. *
  237. * @param Action $action Action being executed
  238. *
  239. * @return boolean hook return
  240. */
  241. function onStartLoginGroupNav($action)
  242. {
  243. if (common_config('site', 'openidonly')) {
  244. $this->showOpenIDLoginTab($action);
  245. // Even though we replace this code, we
  246. // DON'T run the End* hook, to keep others from
  247. // adding tabs. Not nice, but.
  248. return false;
  249. }
  250. return true;
  251. }
  252. /**
  253. * Menu item for login
  254. *
  255. * @param Action $action Action being executed
  256. *
  257. * @return boolean hook return
  258. */
  259. function onEndLoginGroupNav($action)
  260. {
  261. $this->showOpenIDLoginTab($action);
  262. return true;
  263. }
  264. /**
  265. * Show menu item for login
  266. *
  267. * @param Action $action Action being executed
  268. *
  269. * @return void
  270. */
  271. function showOpenIDLoginTab($action)
  272. {
  273. $action_name = $action->trimmed('action');
  274. $action->menuItem(common_local_url('openidlogin'),
  275. // TRANS: OpenID plugin menu item on site logon page.
  276. _m('MENU', 'OpenID'),
  277. // TRANS: OpenID plugin tooltip for logon menu item.
  278. _m('Login or register with OpenID.'),
  279. $action_name === 'openidlogin');
  280. }
  281. /**
  282. * Show menu item for password
  283. *
  284. * We hide it in openID-only mode
  285. *
  286. * @param Action $menu Widget for menu
  287. * @param void &$unused Unused value
  288. *
  289. * @return void
  290. */
  291. function onStartAccountSettingsPasswordMenuItem($menu, &$unused) {
  292. if (common_config('site', 'openidonly')) {
  293. return false;
  294. }
  295. return true;
  296. }
  297. /**
  298. * Menu item for OpenID settings
  299. *
  300. * @param Action $action Action being executed
  301. *
  302. * @return boolean hook return
  303. */
  304. function onEndAccountSettingsNav($action)
  305. {
  306. $action_name = $action->trimmed('action');
  307. $action->menuItem(common_local_url('openidsettings'),
  308. // TRANS: OpenID plugin menu item on user settings page.
  309. _m('MENU', 'OpenID'),
  310. // TRANS: OpenID plugin tooltip for user settings menu item.
  311. _m('Add or remove OpenIDs.'),
  312. $action_name === 'openidsettings');
  313. return true;
  314. }
  315. /**
  316. * Autoloader
  317. *
  318. * Loads our classes if they're requested.
  319. *
  320. * @param string $cls Class requested
  321. *
  322. * @return boolean hook return
  323. */
  324. function onAutoload($cls)
  325. {
  326. switch ($cls)
  327. {
  328. case 'Auth_OpenID_TeamsExtension':
  329. case 'Auth_OpenID_TeamsRequest':
  330. case 'Auth_OpenID_TeamsResponse':
  331. require_once dirname(__FILE__) . '/extlib/teams-extension.php';
  332. return false;
  333. }
  334. return parent::onAutoload($cls);
  335. }
  336. /**
  337. * Sensitive actions
  338. *
  339. * These actions should use https when SSL support is 'sometimes'
  340. *
  341. * @param Action $action Action to form an URL for
  342. * @param boolean &$ssl Whether to mark it for SSL
  343. *
  344. * @return boolean hook return
  345. */
  346. function onSensitiveAction($action, &$ssl)
  347. {
  348. switch ($action)
  349. {
  350. case 'finishopenidlogin':
  351. case 'finishaddopenid':
  352. $ssl = true;
  353. return false;
  354. default:
  355. return true;
  356. }
  357. }
  358. /**
  359. * Login actions
  360. *
  361. * These actions should be visible even when the site is marked private
  362. *
  363. * @param Action $action Action to show
  364. * @param boolean &$login Whether it's a login action
  365. *
  366. * @return boolean hook return
  367. */
  368. function onLoginAction($action, &$login)
  369. {
  370. switch ($action)
  371. {
  372. case 'openidlogin':
  373. case 'finishopenidlogin':
  374. case 'openidserver':
  375. $login = true;
  376. return false;
  377. default:
  378. return true;
  379. }
  380. }
  381. /**
  382. * We include a <meta> element linking to the webfinger resource page,
  383. * for OpenID client-side authentication.
  384. *
  385. * @param Action $action Action being shown
  386. *
  387. * @return void
  388. */
  389. function onEndShowHeadElements($action)
  390. {
  391. if ($action instanceof ShowstreamAction) {
  392. $action->element('link', array('rel' => 'openid2.provider',
  393. 'href' => common_local_url('openidserver')));
  394. $action->element('link', array('rel' => 'openid2.local_id',
  395. 'href' => $action->profile->profileurl));
  396. $action->element('link', array('rel' => 'openid.server',
  397. 'href' => common_local_url('openidserver')));
  398. $action->element('link', array('rel' => 'openid.delegate',
  399. 'href' => $action->profile->profileurl));
  400. }
  401. return true;
  402. }
  403. /**
  404. * Redirect to OpenID login if they have an OpenID
  405. *
  406. * @param Action $action Action being executed
  407. * @param User $user User doing the action
  408. *
  409. * @return boolean whether to continue
  410. */
  411. function onRedirectToLogin($action, $user)
  412. {
  413. if (common_config('site', 'openid_only') || (!empty($user) && User_openid::hasOpenID($user->id))) {
  414. common_redirect(common_local_url('openidlogin'), 303);
  415. }
  416. return true;
  417. }
  418. /**
  419. * Show some extra instructions for using OpenID
  420. *
  421. * @param Action $action Action being executed
  422. *
  423. * @return boolean hook value
  424. */
  425. function onEndShowPageNotice($action)
  426. {
  427. $name = $action->trimmed('action');
  428. switch ($name)
  429. {
  430. case 'register':
  431. if (common_logged_in()) {
  432. // TRANS: Page notice for logged in users to try and get them to add an OpenID account to their StatusNet account.
  433. // TRANS: This message contains Markdown links in the form (description)[link].
  434. $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
  435. '[Add an OpenID to your account](%%action.openidsettings%%)!');
  436. } else {
  437. // TRANS: Page notice for anonymous users to try and get them to register with an OpenID account.
  438. // TRANS: This message contains Markdown links in the form (description)[link].
  439. $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
  440. 'Try our [OpenID registration]'.
  441. '(%%action.openidlogin%%)!)');
  442. }
  443. break;
  444. case 'login':
  445. // TRANS: Page notice on the login page to try and get them to log on with an OpenID account.
  446. // TRANS: This message contains Markdown links in the form (description)[link].
  447. $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
  448. 'Try our [OpenID login]'.
  449. '(%%action.openidlogin%%)!)');
  450. break;
  451. default:
  452. return true;
  453. }
  454. $output = common_markup_to_html($instr);
  455. $action->raw($output);
  456. return true;
  457. }
  458. /**
  459. * Load our document if requested
  460. *
  461. * @param string &$title Title to fetch
  462. * @param string &$output HTML to output
  463. *
  464. * @return boolean hook value
  465. */
  466. function onStartLoadDoc(&$title, &$output)
  467. {
  468. if ($title == 'openid') {
  469. $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
  470. $c = file_get_contents($filename);
  471. $output = common_markup_to_html($c);
  472. return false; // success!
  473. }
  474. return true;
  475. }
  476. /**
  477. * Add our document to the global menu
  478. *
  479. * @param string $title Title being fetched
  480. * @param string &$output HTML being output
  481. *
  482. * @return boolean hook value
  483. */
  484. function onEndDocsMenu(&$items) {
  485. $items[] = array('doc',
  486. array('title' => 'openid'),
  487. _m('MENU', 'OpenID'),
  488. _('Logging in with OpenID'),
  489. 'nav_doc_openid');
  490. return true;
  491. }
  492. /**
  493. * Data definitions
  494. *
  495. * Assure that our data objects are available in the DB
  496. *
  497. * @return boolean hook value
  498. */
  499. function onCheckSchema()
  500. {
  501. $schema = Schema::get();
  502. $schema->ensureTable('user_openid', User_openid::schemaDef());
  503. $schema->ensureTable('user_openid_trustroot', User_openid_trustroot::schemaDef());
  504. $schema->ensureTable('user_openid_prefs', User_openid_prefs::schemaDef());
  505. /* These are used by JanRain OpenID library */
  506. $schema->ensureTable('oid_associations',
  507. array(
  508. 'fields' => array(
  509. 'server_url' => array('type' => 'blob', 'not null' => true),
  510. 'handle' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'default' => ''), // character set latin1,
  511. 'secret' => array('type' => 'blob'),
  512. 'issued' => array('type' => 'int'),
  513. 'lifetime' => array('type' => 'int'),
  514. 'assoc_type' => array('type' => 'varchar', 'length' => 64),
  515. ),
  516. 'primary key' => array(array('server_url', 255), 'handle'),
  517. ));
  518. $schema->ensureTable('oid_nonces',
  519. array(
  520. 'fields' => array(
  521. 'server_url' => array('type' => 'varchar', 'length' => 2047),
  522. 'timestamp' => array('type' => 'int'),
  523. 'salt' => array('type' => 'char', 'length' => 40),
  524. ),
  525. 'unique keys' => array(
  526. 'oid_nonces_server_url_timestamp_salt_key' => array(array('server_url', 255), 'timestamp', 'salt'),
  527. ),
  528. ));
  529. return true;
  530. }
  531. /**
  532. * Add our tables to be deleted when a user is deleted
  533. *
  534. * @param User $user User being deleted
  535. * @param array &$tables Array of table names
  536. *
  537. * @return boolean hook value
  538. */
  539. function onUserDeleteRelated($user, &$tables)
  540. {
  541. $tables[] = 'User_openid';
  542. $tables[] = 'User_openid_trustroot';
  543. return true;
  544. }
  545. /**
  546. * Add an OpenID tab to the admin panel
  547. *
  548. * @param Widget $nav Admin panel nav
  549. *
  550. * @return boolean hook value
  551. */
  552. function onEndAdminPanelNav($nav)
  553. {
  554. if (AdminPanelAction::canAdmin('openid')) {
  555. $action_name = $nav->action->trimmed('action');
  556. $nav->out->menuItem(
  557. common_local_url('openidadminpanel'),
  558. // TRANS: OpenID configuration menu item.
  559. _m('MENU','OpenID'),
  560. // TRANS: Tooltip for OpenID configuration menu item.
  561. _m('OpenID configuration.'),
  562. $action_name == 'openidadminpanel',
  563. 'nav_openid_admin_panel'
  564. );
  565. }
  566. return true;
  567. }
  568. /**
  569. * Add OpenID information to the Account Management Control Document
  570. * Event supplied by the Account Manager plugin
  571. *
  572. * @param array &$amcd Array that expresses the AMCD
  573. *
  574. * @return boolean hook value
  575. */
  576. function onEndAccountManagementControlDocument(&$amcd)
  577. {
  578. $amcd['auth-methods']['openid'] = array(
  579. 'connect' => array(
  580. 'method' => 'POST',
  581. 'path' => common_local_url('openidlogin'),
  582. 'params' => array(
  583. 'identity' => 'openid_url'
  584. )
  585. )
  586. );
  587. }
  588. /**
  589. * Add our version information to output
  590. *
  591. * @param array &$versions Array of version-data arrays
  592. *
  593. * @return boolean hook value
  594. */
  595. function onPluginVersion(&$versions)
  596. {
  597. $versions[] = array('name' => 'OpenID',
  598. 'version' => GNUSOCIAL_VERSION,
  599. 'author' => 'Evan Prodromou, Craig Andrews',
  600. 'homepage' => 'http://status.net/wiki/Plugin:OpenID',
  601. 'rawdescription' =>
  602. // TRANS: Plugin description.
  603. _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));
  604. return true;
  605. }
  606. function onStartOAuthLoginForm($action, &$button)
  607. {
  608. if (common_config('site', 'openidonly')) {
  609. // Cancel the regular password login form, we won't need it.
  610. $this->showOAuthLoginForm($action);
  611. // TRANS: button label for OAuth authorization page when needing OpenID authentication first.
  612. $button = _m('BUTTON', 'Continue');
  613. return false;
  614. } else {
  615. // Leave the regular password login form in place.
  616. // We'll add an OpenID link at bottom...?
  617. return true;
  618. }
  619. }
  620. /**
  621. * @fixme merge with common code for main OpenID login form
  622. * @param HTMLOutputter $action
  623. */
  624. protected function showOAuthLoginForm($action)
  625. {
  626. $action->elementStart('fieldset');
  627. // TRANS: OpenID plugin logon form legend.
  628. $action->element('legend', null, _m('LEGEND','OpenID login'));
  629. $action->elementStart('ul', 'form_data');
  630. $action->elementStart('li');
  631. $provider = common_config('openid', 'trusted_provider');
  632. $appendUsername = common_config('openid', 'append_username');
  633. if ($provider) {
  634. // TRANS: Field label.
  635. $action->element('label', array(), _m('OpenID provider'));
  636. $action->element('span', array(), $provider);
  637. if ($appendUsername) {
  638. $action->element('input', array('id' => 'openid_username',
  639. 'name' => 'openid_username',
  640. 'style' => 'float: none'));
  641. }
  642. $action->element('p', 'form_guide',
  643. // TRANS: Form guide.
  644. ($appendUsername ? _m('Enter your username.') . ' ' : '') .
  645. // TRANS: Form guide.
  646. _m('You will be sent to the provider\'s site for authentication.'));
  647. $action->hidden('openid_url', $provider);
  648. } else {
  649. // TRANS: OpenID plugin logon form field label.
  650. $action->input('openid_url', _m('OpenID URL'),
  651. '',
  652. // TRANS: OpenID plugin logon form field instructions.
  653. _m('Your OpenID URL.'));
  654. }
  655. $action->elementEnd('li');
  656. $action->elementEnd('ul');
  657. $action->elementEnd('fieldset');
  658. }
  659. /**
  660. * Handle a POST user credential check in apioauthauthorization.
  661. * If given an OpenID URL, we'll pass us over to the regular things
  662. * and then redirect back here on completion.
  663. *
  664. * @fixme merge with common code for main OpenID login form
  665. * @param HTMLOutputter $action
  666. */
  667. function onStartOAuthLoginCheck($action, &$user)
  668. {
  669. $provider = common_config('openid', 'trusted_provider');
  670. if ($provider) {
  671. $openid_url = $provider;
  672. if (common_config('openid', 'append_username')) {
  673. $openid_url .= $action->trimmed('openid_username');
  674. }
  675. } else {
  676. $openid_url = $action->trimmed('openid_url');
  677. }
  678. if ($openid_url) {
  679. require_once dirname(__FILE__) . '/openid.php';
  680. oid_assert_allowed($openid_url);
  681. $returnto = common_local_url(
  682. 'ApiOAuthAuthorize',
  683. array(),
  684. array(
  685. 'oauth_token' => $action->arg('oauth_token'),
  686. 'mode' => $action->arg('mode')
  687. )
  688. );
  689. common_set_returnto($returnto);
  690. // This will redirect if functional...
  691. $result = oid_authenticate($openid_url,
  692. 'finishopenidlogin');
  693. if (is_string($result)) { # error message
  694. throw new ServerException($result);
  695. } else {
  696. exit(0);
  697. }
  698. }
  699. return true;
  700. }
  701. /**
  702. * Add link in user's XRD file to allow OpenID login.
  703. *
  704. * This link in the XRD should let users log in with their
  705. * Webfinger identity to services that support it. See
  706. * http://webfinger.org/login for an example.
  707. *
  708. * @param XML_XRD $xrd Currently-displaying resource descriptor
  709. * @param Profile $target The profile that it's for
  710. *
  711. * @return boolean hook value (always true)
  712. */
  713. function onEndWebFingerProfileLinks(XML_XRD $xrd, Profile $target)
  714. {
  715. $xrd->links[] = new XML_XRD_Element_Link(
  716. 'http://specs.openid.net/auth/2.0/provider',
  717. $target->profileurl);
  718. return true;
  719. }
  720. /**
  721. * Add links in the user's profile block to their OpenID URLs.
  722. *
  723. * @param Profile $profile The profile being shown
  724. * @param Array &$links Writeable array of arrays (href, text, image).
  725. *
  726. * @return boolean hook value (true)
  727. */
  728. function onOtherAccountProfiles($profile, &$links)
  729. {
  730. $prefs = User_openid_prefs::getKV('user_id', $profile->id);
  731. if (empty($prefs) || !$prefs->hide_profile_link) {
  732. $oid = new User_openid();
  733. $oid->user_id = $profile->id;
  734. if ($oid->find()) {
  735. while ($oid->fetch()) {
  736. $links[] = array('href' => $oid->display,
  737. 'text' => _('OpenID'),
  738. 'image' => $this->path("icons/openid-16x16.gif"));
  739. }
  740. }
  741. }
  742. return true;
  743. }
  744. }