]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/actions/openidserver.php
Let OpenID match against aliases (fix fancyurl stuff etc.)
[quix0rs-gnu-social.git] / plugins / OpenID / actions / 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  * @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
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
36
37 /**
38  * Settings for OpenID
39  *
40  * Lets users add, edit and delete OpenIDs from their account
41  *
42  * @category Settings
43  * @package  StatusNet
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/
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             if (!$this->scoped instanceof Profile) {
67                 if($request->immediate){
68                     //cannot prompt the user to login in immediate mode, so answer false
69                     $response = $this->generateDenyResponse($request);
70                 }else{
71                     // Go log in, and then come back.
72                     //
73                     // Note: 303 redirect rather than 307 to avoid
74                     // prompting user for form resubmission if we
75                     // were POSTed here.
76                     common_set_returnto($_SERVER['REQUEST_URI']);
77                     common_redirect(common_local_url('login'), 303);
78                 }
79             } elseif (in_array($request->identity, $this->scoped->getAliases()) || $request->idSelect()) {
80                 $user_openid_trustroot = User_openid_trustroot::pkeyGet(
81                                                 array('user_id'=>$this->scoped->getID(), 'trustroot'=>$request->trust_root));
82                 if(empty($user_openid_trustroot)){
83                     if($request->immediate){
84                         //cannot prompt the user to trust this trust root in immediate mode, so answer false
85                         $response = $this->generateDenyResponse($request);
86                     }else{
87                         common_ensure_session();
88                         $_SESSION['openid_trust_root'] = $request->trust_root;
89                         $allowResponse = $this->generateAllowResponse($request, $this->scoped);
90                         $this->oserver->encodeResponse($allowResponse); //sign the response
91                         $denyResponse = $this->generateDenyResponse($request);
92                         $this->oserver->encodeResponse($denyResponse); //sign the response
93                         $_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
94                         $_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
95
96                         // Ask the user to trust this trust root...
97                         //
98                         // Note: 303 redirect rather than 307 to avoid
99                         // prompting user for form resubmission if we
100                         // were POSTed here.
101                         common_redirect(common_local_url('openidtrust'), 303);
102                     }
103                 } else {
104                     //user has previously authorized this trust root
105                     $response = $this->generateAllowResponse($request, $this->scoped);
106                 }
107             } elseif ($request->immediate) {
108                 $response = $this->generateDenyResponse($request);
109             } else {
110                 //invalid
111                 // TRANS: OpenID plugin client error given trying to add an unauthorised OpenID to a user (403).
112                 // TRANS: %s is a request identity.
113                 $this->clientError(sprintf(_m('You are not authorized to use the identity %s.'),$request->identity),$code=403);
114             }
115         } else {
116             $response = $this->oserver->handleRequest($request);
117         }
118
119         if($response){
120             $response = $this->oserver->encodeResponse($response);
121             if ($response->code != AUTH_OPENID_HTTP_OK) {
122                 header(sprintf("HTTP/1.1 %d ", $response->code),
123                        true, $response->code);
124             }
125
126             if($response->headers){
127                 foreach ($response->headers as $k => $v) {
128                     header("$k: $v");
129                 }
130             }
131             $this->raw($response->body);
132         }else{
133             // TRANS: OpenID plugin client error given when not getting a response for a given OpenID provider (500).
134             $this->clientError(_m('Just an OpenID provider. Nothing to see here, move along...'),$code=500);
135         }
136     }
137
138     function generateAllowResponse($request, Profile $profile){
139         $response = $request->answer(true, null, $profile->getUrl());
140         $user = $profile->getUser();
141
142         $sreg_data = array(
143             'fullname' => $profile->getFullname(),
144             'nickname' => $profile->getNickname(),
145             'email' => $user->email,    // FIXME: Should we make the email optional?
146             'language' => $user->language,
147             'timezone' => $user->timezone);
148         $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
149         $sreg_response = Auth_OpenID_SRegResponse::extractResponse(
150                               $sreg_request, $sreg_data);
151         $sreg_response->toMessage($response->fields);
152         return $response;
153     }
154
155     function generateDenyResponse($request){
156         $response = $request->answer(false);
157         return $response;
158     }
159 }