openidserver.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Settings for OpenID
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Settings
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @copyright 2008-2009 StatusNet, Inc.
  26. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. require_once INSTALLDIR.'/plugins/OpenID/openid.php';
  34. /**
  35. * Settings for OpenID
  36. *
  37. * Lets users add, edit and delete OpenIDs from their account
  38. *
  39. * @category Settings
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  42. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. */
  46. class OpenidserverAction extends Action
  47. {
  48. var $oserver;
  49. function prepare($args)
  50. {
  51. parent::prepare($args);
  52. $this->oserver = oid_server();
  53. return true;
  54. }
  55. function handle($args)
  56. {
  57. parent::handle($args);
  58. $request = $this->oserver->decodeRequest();
  59. if (in_array($request->mode, array('checkid_immediate',
  60. 'checkid_setup'))) {
  61. $user = common_current_user();
  62. if(!$user){
  63. if($request->immediate){
  64. //cannot prompt the user to login in immediate mode, so answer false
  65. $response = $this->generateDenyResponse($request);
  66. }else{
  67. // Go log in, and then come back.
  68. //
  69. // Note: 303 redirect rather than 307 to avoid
  70. // prompting user for form resubmission if we
  71. // were POSTed here.
  72. common_set_returnto($_SERVER['REQUEST_URI']);
  73. common_redirect(common_local_url('login'), 303);
  74. }
  75. }else if(common_profile_url($user->nickname) == $request->identity || $request->idSelect()){
  76. $user_openid_trustroot = User_openid_trustroot::pkeyGet(
  77. array('user_id'=>$user->id, 'trustroot'=>$request->trust_root));
  78. if(empty($user_openid_trustroot)){
  79. if($request->immediate){
  80. //cannot prompt the user to trust this trust root in immediate mode, so answer false
  81. $response = $this->generateDenyResponse($request);
  82. }else{
  83. common_ensure_session();
  84. $_SESSION['openid_trust_root'] = $request->trust_root;
  85. $allowResponse = $this->generateAllowResponse($request, $user);
  86. $this->oserver->encodeResponse($allowResponse); //sign the response
  87. $denyResponse = $this->generateDenyResponse($request);
  88. $this->oserver->encodeResponse($denyResponse); //sign the response
  89. $_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
  90. $_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
  91. // Ask the user to trust this trust root...
  92. //
  93. // Note: 303 redirect rather than 307 to avoid
  94. // prompting user for form resubmission if we
  95. // were POSTed here.
  96. common_redirect(common_local_url('openidtrust'), 303);
  97. }
  98. }else{
  99. //user has previously authorized this trust root
  100. $response = $this->generateAllowResponse($request, $user);
  101. //$response = $request->answer(true, null, common_profile_url($user->nickname));
  102. }
  103. } else if ($request->immediate) {
  104. $response = $this->generateDenyResponse($request);
  105. } else {
  106. //invalid
  107. // TRANS: OpenID plugin client error given trying to add an unauthorised OpenID to a user (403).
  108. // TRANS: %s is a request identity.
  109. $this->clientError(sprintf(_m('You are not authorized to use the identity %s.'),$request->identity),$code=403);
  110. }
  111. } else {
  112. $response = $this->oserver->handleRequest($request);
  113. }
  114. if($response){
  115. $response = $this->oserver->encodeResponse($response);
  116. if ($response->code != AUTH_OPENID_HTTP_OK) {
  117. header(sprintf("HTTP/1.1 %d ", $response->code),
  118. true, $response->code);
  119. }
  120. if($response->headers){
  121. foreach ($response->headers as $k => $v) {
  122. header("$k: $v");
  123. }
  124. }
  125. $this->raw($response->body);
  126. }else{
  127. // TRANS: OpenID plugin client error given when not getting a response for a given OpenID provider (500).
  128. $this->clientError(_m('Just an OpenID provider. Nothing to see here, move along...'),$code=500);
  129. }
  130. }
  131. function generateAllowResponse($request, $user){
  132. $response = $request->answer(true, null, common_profile_url($user->nickname));
  133. $profile = $user->getProfile();
  134. $sreg_data = array(
  135. 'fullname' => $profile->fullname,
  136. 'nickname' => $user->nickname,
  137. 'email' => $user->email,
  138. 'language' => $user->language,
  139. 'timezone' => $user->timezone);
  140. $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
  141. $sreg_response = Auth_OpenID_SRegResponse::extractResponse(
  142. $sreg_request, $sreg_data);
  143. $sreg_response->toMessage($response->fields);
  144. return $response;
  145. }
  146. function generateDenyResponse($request){
  147. $response = $request->answer(false);
  148. return $response;
  149. }
  150. }