finishsynchopenid.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * OpenID Synch completion
  18. *
  19. * @package GNUsocial
  20. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  21. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. require_once(INSTALLDIR . '/plugins/OpenID/openid.php');
  26. /**
  27. * Action that handles OpenID Synch completion.
  28. *
  29. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class FinishsynchopenidAction extends Action
  33. {
  34. public $msg = null;
  35. /**
  36. * Handle the redirect back from OpenID confirmation
  37. *
  38. * Check to see if the user's logged in, and then try
  39. * to use the OpenID login system.
  40. *
  41. * @param array $args $_REQUEST arguments
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. parent::handle();
  48. if (!common_logged_in()) {
  49. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  50. $this->clientError(_m('Not logged in.'));
  51. } else {
  52. $this->tryLogin();
  53. }
  54. }
  55. /**
  56. * Try to log in using OpenID
  57. *
  58. * Check the OpenID for validity; potentially store it.
  59. *
  60. * @return void
  61. */
  62. public function tryLogin()
  63. {
  64. $consumer = oid_consumer();
  65. $response = $consumer->complete(common_local_url('finishsynchopenid'));
  66. if ($response->status == Auth_OpenID_CANCEL) {
  67. // TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled.
  68. $this->message(_m('OpenID authentication cancelled.'));
  69. return;
  70. } elseif ($response->status == Auth_OpenID_FAILURE) {
  71. // TRANS: OpenID authentication failed; display the error message.
  72. // TRANS: %s is the error message.
  73. $this->message(sprintf(
  74. _m('OpenID authentication failed: %s.'),
  75. $response->message
  76. ));
  77. } elseif ($response->status == Auth_OpenID_SUCCESS) {
  78. $display = $response->getDisplayIdentifier();
  79. $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
  80. $response->endpoint->canonicalID : $display;
  81. $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
  82. if ($sreg_resp) {
  83. $sreg = $sreg_resp->contents();
  84. }
  85. // Launchpad teams extension
  86. if (!oid_check_teams($response)) {
  87. // TRANS: OpenID authentication error.
  88. $this->message(_m('OpenID authentication aborted: You are not allowed to login to this site.'));
  89. return;
  90. }
  91. $cur = common_current_user();
  92. // start a transaction
  93. $cur->query('START TRANSACTION');
  94. if (Event::handle('StartOpenIDUpdateUser', [$cur, $canonical, &$sreg])) {
  95. if (!oid_update_user($cur, $sreg)) {
  96. // TRANS: Message in case the user or the user profile cannot be saved in StatusNet.
  97. $this->message(_m('Error updating profile.'));
  98. return;
  99. }
  100. }
  101. Event::handle('EndOpenIDUpdateUser', [$cur, $canonical, $sreg]);
  102. // success!
  103. $cur->query('COMMIT');
  104. oid_set_last($display);
  105. common_redirect(common_local_url('openidsettings'), 303);
  106. }
  107. }
  108. /**
  109. * Show a failure message
  110. *
  111. * Something went wrong. Save the message, and show the page.
  112. *
  113. * @param string $msg Error message to show
  114. *
  115. * @return void
  116. */
  117. public function message($msg)
  118. {
  119. $this->message = $msg;
  120. $this->showPage();
  121. }
  122. /**
  123. * Title of the page
  124. *
  125. * @return string title
  126. */
  127. public function title()
  128. {
  129. // TRANS: Title after getting the status of the OpenID authorisation request.
  130. // TODO update after understanding the function ^
  131. return _m('OpenID Synchronization');
  132. }
  133. /**
  134. * Show error message
  135. *
  136. * @return void
  137. */
  138. public function showPageNotice()
  139. {
  140. if ($this->message) {
  141. $this->element('p', 'error', $this->message);
  142. }
  143. }
  144. }