]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidserver.php
2a414c48732ba54ea054c5fe688824417998ab77
[quix0rs-gnu-social.git] / plugins / OpenID / openidserver.php
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  * @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
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/action.php';
35 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
36 require_once(INSTALLDIR.'/plugins/OpenID/User_openid_trustroot.php');
37
38 /**
39  * Settings for OpenID
40  *
41  * Lets users add, edit and delete OpenIDs from their account
42  *
43  * @category Settings
44  * @package  StatusNet
45  * @author   Craig Andrews <candrews@integralblue.com>
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/
48  */
49 class OpenidserverAction extends Action
50 {
51     var $oserver;
52
53     function prepare($args)
54     {
55         parent::prepare($args);
56         $this->oserver = oid_server();
57         return true;
58     }
59
60     function handle($args)
61     {
62         parent::handle($args);
63         $request = $this->oserver->decodeRequest();
64         if (in_array($request->mode, array('checkid_immediate',
65             'checkid_setup'))) {
66             $user = common_current_user();
67             if(!$user){
68                 if($request->immediate){
69                     //cannot prompt the user to login in immediate mode, so answer false
70                     $response = $this->generateDenyResponse($request);
71                 }else{
72                     // Go log in, and then come back.
73                     //
74                     // Note: 303 redirect rather than 307 to avoid
75                     // prompting user for form resubmission if we
76                     // were POSTed here.
77                     common_set_returnto($_SERVER['REQUEST_URI']);
78                     common_redirect(common_local_url('login'), 303);
79                     return;
80                 }
81             }else if(common_profile_url($user->nickname) == $request->identity || $request->idSelect()){
82                 $user_openid_trustroot = User_openid_trustroot::pkeyGet(
83                                                 array('user_id'=>$user->id, 'trustroot'=>$request->trust_root));
84                 if(empty($user_openid_trustroot)){
85                     if($request->immediate){
86                         //cannot prompt the user to trust this trust root in immediate mode, so answer false
87                         $response = $this->generateDenyResponse($request);
88                     }else{
89                         common_ensure_session();
90                         $_SESSION['openid_trust_root'] = $request->trust_root;
91                         $allowResponse = $this->generateAllowResponse($request, $user);
92                         $this->oserver->encodeResponse($allowResponse); //sign the response
93                         $denyResponse = $this->generateDenyResponse($request);
94                         $this->oserver->encodeResponse($denyResponse); //sign the response
95                         $_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
96                         $_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
97
98                         // Ask the user to trust this trust root...
99                         //
100                         // Note: 303 redirect rather than 307 to avoid
101                         // prompting user for form resubmission if we
102                         // were POSTed here.
103                         common_redirect(common_local_url('openidtrust'), 303);
104                         return;
105                     }
106                 }else{
107                     //user has previously authorized this trust root
108                     $response = $this->generateAllowResponse($request, $user);
109                     //$response = $request->answer(true, null, common_profile_url($user->nickname));
110                 }
111             } else if ($request->immediate) {
112                 $response = $this->generateDenyResponse($request);
113             } else {
114                 //invalid
115                 $this->clientError(sprintf(_m('You are not authorized to use the identity %s.'),$request->identity),$code=403);
116             }
117         } else {
118             $response = $this->oserver->handleRequest($request);
119         }
120
121         if($response){
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);
126             }
127
128             if($response->headers){
129                 foreach ($response->headers as $k => $v) {
130                     header("$k: $v");
131                 }
132             }
133             $this->raw($response->body);
134         }else{
135             $this->clientError(_m('Just an OpenID provider. Nothing to see here, move along...'),$code=500);
136         }
137     }
138
139     function generateAllowResponse($request, $user){
140         $response = $request->answer(true, null, common_profile_url($user->nickname));
141         
142         $profile = $user->getProfile();
143         $sreg_data = array(
144             'fullname' => $profile->fullname,
145             'nickname' => $user->nickname,
146             'email' => $user->email,
147             'language' => $user->language,
148             'timezone' => $user->timezone);
149         $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
150         $sreg_response = Auth_OpenID_SRegResponse::extractResponse(
151                               $sreg_request, $sreg_data);
152         $sreg_response->toMessage($response->fields);
153         return $response;
154     }
155
156     function generateDenyResponse($request){
157         $response = $request->answer(false);
158         return $response;
159     }
160 }