openidserver.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * Settings for OpenID
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Craig Andrews <candrews@integralblue.com>
  22. * @copyright 2008-2009 StatusNet, Inc.
  23. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. require_once INSTALLDIR . '/plugins/OpenID/openid.php';
  28. /**
  29. * Settings for OpenID
  30. *
  31. * Lets users add, edit and delete OpenIDs from their account
  32. *
  33. * @category Settings
  34. * @package GNUsocial
  35. * @author Craig Andrews <candrews@integralblue.com>
  36. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. class OpenidserverAction extends Action
  40. {
  41. public $oserver;
  42. public function prepare(array $args = [])
  43. {
  44. parent::prepare($args);
  45. $this->oserver = oid_server();
  46. return true;
  47. }
  48. public function handle()
  49. {
  50. parent::handle();
  51. $request = $this->oserver->decodeRequest();
  52. if (in_array($request->mode, array('checkid_immediate',
  53. 'checkid_setup'))) {
  54. if (!$this->scoped instanceof Profile) {
  55. if ($request->immediate) {
  56. //cannot prompt the user to login in immediate mode, so answer false
  57. $response = $this->generateDenyResponse($request);
  58. } else {
  59. // Go log in, and then come back.
  60. //
  61. // Note: 303 redirect rather than 307 to avoid
  62. // prompting user for form resubmission if we
  63. // were POSTed here.
  64. common_set_returnto($_SERVER['REQUEST_URI']);
  65. common_redirect(common_local_url('login'), 303);
  66. }
  67. } elseif (
  68. in_array($request->identity, $this->scoped->getAliases())
  69. || $request->idSelect()
  70. ) {
  71. $user_openid_trustroot = User_openid_trustroot::pkeyGet([
  72. 'user_id' => $this->scoped->getID(),
  73. 'trustroot' => $request->trust_root,
  74. ]);
  75. if (empty($user_openid_trustroot)) {
  76. if ($request->immediate) {
  77. //cannot prompt the user to trust this trust root in immediate mode, so answer false
  78. $response = $this->generateDenyResponse($request);
  79. } else {
  80. common_ensure_session();
  81. $_SESSION['openid_trust_root'] = $request->trust_root;
  82. $allowResponse = $this->generateAllowResponse($request, $this->scoped);
  83. $this->oserver->encodeResponse($allowResponse); //sign the response
  84. $denyResponse = $this->generateDenyResponse($request);
  85. $this->oserver->encodeResponse($denyResponse); //sign the response
  86. $_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
  87. $_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
  88. // Ask the user to trust this trust root...
  89. //
  90. // Note: 303 redirect rather than 307 to avoid
  91. // prompting user for form resubmission if we
  92. // were POSTed here.
  93. common_redirect(common_local_url('openidtrust'), 303);
  94. }
  95. } else {
  96. //user has previously authorized this trust root
  97. $response = $this->generateAllowResponse($request, $this->scoped);
  98. }
  99. } elseif ($request->immediate) {
  100. $response = $this->generateDenyResponse($request);
  101. } else {
  102. //invalid
  103. $this->clientError(sprintf(
  104. // TRANS: OpenID plugin client error given trying to add an unauthorised OpenID to a user (403).
  105. // TRANS: %s is a request identity.
  106. _m('You are not authorized to use the identity %s.'),
  107. $request->identity
  108. ), 403);
  109. }
  110. } else {
  111. $response = $this->oserver->handleRequest($request);
  112. }
  113. if ($response) {
  114. $response = $this->oserver->encodeResponse($response);
  115. if ($response->code != AUTH_OPENID_HTTP_OK) {
  116. http_response_code($response->code);
  117. }
  118. if ($response->headers) {
  119. foreach ($response->headers as $k => $v) {
  120. header("$k: $v");
  121. }
  122. }
  123. $this->raw($response->body);
  124. } else {
  125. $this->clientError(
  126. // TRANS: OpenID plugin client error given when not getting a response for a given OpenID provider (500).
  127. _m('Just an OpenID provider. Nothing to see here, move along...'),
  128. 500
  129. );
  130. }
  131. }
  132. public function generateAllowResponse($request, Profile $profile)
  133. {
  134. $response = $request->answer(true, null, $profile->getUrl());
  135. $user = $profile->getUser();
  136. $sreg_data = [
  137. 'fullname' => $profile->getFullname(),
  138. 'nickname' => $profile->getNickname(),
  139. 'email' => $user->email, // FIXME: Should we make the email optional?
  140. 'language' => $user->language,
  141. 'timezone' => $user->timezone,
  142. ];
  143. $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
  144. $sreg_response = Auth_OpenID_SRegResponse::extractResponse(
  145. $sreg_request,
  146. $sreg_data
  147. );
  148. $sreg_response->toMessage($response->fields);
  149. return $response;
  150. }
  151. public function generateDenyResponse($request)
  152. {
  153. $response = $request->answer(false);
  154. return $response;
  155. }
  156. }