3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
35 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
40 * Lets users add, edit and delete OpenIDs from their account
44 * @author Craig Andrews <candrews@integralblue.com>
45 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
49 class OpenidserverAction extends Action
53 function prepare(array $args=array())
55 parent::prepare($args);
56 $this->oserver = oid_server();
60 function handle(array $args=array())
62 parent::handle($args);
63 $request = $this->oserver->decodeRequest();
64 if (in_array($request->mode, array('checkid_immediate',
66 $user = common_current_user();
68 if($request->immediate){
69 //cannot prompt the user to login in immediate mode, so answer false
70 $response = $this->generateDenyResponse($request);
72 // Go log in, and then come back.
74 // Note: 303 redirect rather than 307 to avoid
75 // prompting user for form resubmission if we
77 common_set_returnto($_SERVER['REQUEST_URI']);
78 common_redirect(common_local_url('login'), 303);
80 }else if(common_profile_url($user->nickname) == $request->identity || $request->idSelect()){
81 $user_openid_trustroot = User_openid_trustroot::pkeyGet(
82 array('user_id'=>$user->id, 'trustroot'=>$request->trust_root));
83 if(empty($user_openid_trustroot)){
84 if($request->immediate){
85 //cannot prompt the user to trust this trust root in immediate mode, so answer false
86 $response = $this->generateDenyResponse($request);
88 common_ensure_session();
89 $_SESSION['openid_trust_root'] = $request->trust_root;
90 $allowResponse = $this->generateAllowResponse($request, $user);
91 $this->oserver->encodeResponse($allowResponse); //sign the response
92 $denyResponse = $this->generateDenyResponse($request);
93 $this->oserver->encodeResponse($denyResponse); //sign the response
94 $_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
95 $_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
97 // Ask the user to trust this trust root...
99 // Note: 303 redirect rather than 307 to avoid
100 // prompting user for form resubmission if we
102 common_redirect(common_local_url('openidtrust'), 303);
105 //user has previously authorized this trust root
106 $response = $this->generateAllowResponse($request, $user);
107 //$response = $request->answer(true, null, common_profile_url($user->nickname));
109 } else if ($request->immediate) {
110 $response = $this->generateDenyResponse($request);
113 // TRANS: OpenID plugin client error given trying to add an unauthorised OpenID to a user (403).
114 // TRANS: %s is a request identity.
115 $this->clientError(sprintf(_m('You are not authorized to use the identity %s.'),$request->identity),$code=403);
118 $response = $this->oserver->handleRequest($request);
122 $response = $this->oserver->encodeResponse($response);
123 if ($response->code != AUTH_OPENID_HTTP_OK) {
124 header(sprintf("HTTP/1.1 %d ", $response->code),
125 true, $response->code);
128 if($response->headers){
129 foreach ($response->headers as $k => $v) {
133 $this->raw($response->body);
135 // TRANS: OpenID plugin client error given when not getting a response for a given OpenID provider (500).
136 $this->clientError(_m('Just an OpenID provider. Nothing to see here, move along...'),$code=500);
140 function generateAllowResponse($request, $user){
141 $response = $request->answer(true, null, common_profile_url($user->nickname));
143 $profile = $user->getProfile();
145 'fullname' => $profile->fullname,
146 'nickname' => $user->nickname,
147 'email' => $user->email,
148 'language' => $user->language,
149 'timezone' => $user->timezone);
150 $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
151 $sreg_response = Auth_OpenID_SRegResponse::extractResponse(
152 $sreg_request, $sreg_data);
153 $sreg_response->toMessage($response->fields);
157 function generateDenyResponse($request){
158 $response = $request->answer(false);