]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/OpenID.php
elseif
[SourceForge/phpwiki.git] / lib / WikiUser / OpenID.php
1 <?php //-*-php-*-
2 // $Id$
3 /*
4  * Copyright (C) 2010 ReiniUrban
5  * Zend_OpenId_Consumer parts from Zend licensed under
6  * http://framework.zend.com/license/new-bsd
7  *
8  * This file is part of PhpWiki.
9  *
10  * PhpWiki is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * PhpWiki is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  * This is not yet finished. We do not want to use zend extensions.
25  *
26  * See http://openid.net/specs/openid-authentication-1_1.html
27  */
28
29 // requires the openssl extension
30 require_once("lib/HttpClient.php");
31
32 class _OpenIDPassUser
33 extends _PassUser
34 /**
35  * Preferences are handled in _PassUser
36  */
37 {
38     /**
39      * Verifies authentication response from OpenID server.
40      *
41      * This is the second step of OpenID authentication process.
42      * The function returns true on successful authentication and false on
43      * failure.
44      *
45      * @param array $params HTTP query data from OpenID server
46      * @param string &$identity this argument is set to end-user's claimed
47      *  identifier or OpenID provider local identifier.
48      * @param  mixed $extensions extension object or array of extensions objects
49      * @return bool
50      */
51     function verify($params, &$identity = "", $extensions = null) {
52         $version = 1.1;
53         $this->_setError("");
54         if (isset($params['openid_ns']) &&
55             $params['openid_ns'] == $NS_2_0) { // global session var
56             $version = 2.0;
57         }
58         if (isset($params["openid_claimed_id"])) {
59             $identity = $params["openid_claimed_id"];
60         } elseif (isset($params["openid_identity"])){
61             $identity = $params["openid_identity"];
62         } else {
63             $identity = "";
64         }
65
66         if ($version < 2.0 && !isset($params["openid_claimed_id"])) {
67             global $request;
68             $session = $request->getSessionVar('openid');
69             if (!$session) {
70                 $request->setSessionVar('openid', array());
71             }
72             if ($session['identity'] == $identity) {
73                 $identity = $session['claimed_id'];
74             }
75         }
76         if (empty($params['openid_return_to'])) {
77             $this->_setError("Missing openid.return_to");
78             return false;
79         }
80         if (empty($params['openid_signed'])) {
81             $this->_setError("Missing openid.signed");
82             return false;
83         }
84         if (empty($params['openid_sig'])) {
85             $this->_setError("Missing openid.sig");
86             return false;
87         }
88         if (empty($params['openid_mode'])) {
89             $this->_setError("Missing openid.mode");
90             return false;
91         }
92         if ($params['openid_mode'] != 'id_res') {
93             $this->_setError("Wrong openid.mode '".$params['openid_mode']."' != 'id_res'");
94             return false;
95         }
96         if (empty($params['openid_assoc_handle'])) {
97             $this->_setError("Missing openid.assoc_handle");
98             return false;
99         }
100     }
101
102     /**
103      * Performs check of OpenID identity.
104      *
105      * This is the first step of OpenID authentication process.
106      * On success the function does not return (it does HTTP redirection to
107      * server and exits). On failure it returns false.
108      *
109      * @param bool                              $immediate  enables or disables interaction with user
110      * @param string                            $id         OpenID identity
111      * @param string                            $returnTo   HTTP URL to redirect response from server to
112      * @param string                            $root       HTTP URL to identify consumer on server
113      * @param mixed                             $extensions extension object or array of extensions objects
114      * @param Zend_Controller_Response_Abstract $response   an optional response
115      *  object to perform HTTP or HTML form redirection
116      * @return bool
117      */
118     function _checkId($immediate, $id, $returnTo=null, $root=null,
119                       $extensions=null, $response = null) {
120         $this->_setError('');
121
122         /*if (!Zend_OpenId::normalize($id)) {
123             $this->_setError("Normalisation failed");
124             return false;
125         }*/
126         $claimedId = $id;
127
128         if (!$this->_discovery($id, $server, $version)) {
129             $this->_setError("Discovery failed");
130             return false;
131         }
132         if (!$this->_associate($server, $version)) {
133             $this->_setError("Association failed");
134             return false;
135         }
136         if (!$this->_getAssociation(
137                 $server,
138                 $handle,
139                 $macFunc,
140                 $secret,
141                 $expires)) {
142             /* Use dumb mode */
143             unset($handle);
144             unset($macFunc);
145             unset($secret);
146             unset($expires);
147         }
148
149         $params = array();
150         if ($version >= 2.0) {
151             //$params['openid.ns'] = Zend_OpenId::NS_2_0;
152         }
153
154         $params['openid.mode'] = $immediate ?
155             'checkid_immediate' : 'checkid_setup';
156
157         $params['openid.identity'] = $id;
158
159         $params['openid.claimed_id'] = $claimedId;
160
161         if ($version <= 2.0) {
162             global $request;
163             $session = $request->getSessionVar('openid');
164             $request->setSessionVar('identity', $id);
165             $request->setSessionVar('claimed_id', $claimedId);
166         }
167
168         if (isset($handle)) {
169             $params['openid.assoc_handle'] = $handle;
170         }
171
172         //$params['openid.return_to'] = Zend_OpenId::absoluteUrl($returnTo);
173
174         // See lib/WikiUser/FaceBook.php how to handle http requests
175         $web = new HttpClient("$server", 80);
176         if (DEBUG & _DEBUG_LOGIN) $web->setDebug(true);
177
178         if (empty($root)) {
179             //$root = Zend_OpenId::selfUrl();
180             if ($root[strlen($root)-1] != '/') {
181                 $root = dirname($root);
182             }
183         }
184         if ($version >= 2.0) {
185             $params['openid.realm'] = $root;
186         } else {
187             $params['openid.trust_root'] = $root;
188         }
189
190         /*if (!Zend_OpenId_Extension::forAll($extensions, 'prepareRequest', $params)) {
191             $this->_setError("Extension::prepareRequest failure");
192             return false;
193         }
194         */
195
196         //Zend_OpenId::redirect($server, $params, $response);
197         return true;
198     }
199
200     function _setError($message) {
201         $this->_error = $message;
202     }
203
204     function checkPass($password) {
205         $userid = $this->_userid;
206         if (!loadPhpExtension('openssl')) {
207             trigger_error(
208                 sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
209                  . sprintf(_(" %s AUTH ignored."), 'OpenID'),
210                  E_USER_WARNING);
211             return $this->_tryNextUser();
212         }
213
214         $retval = $this->_checkId(false, $id, $returnTo, $root, $extensions, $response);
215         $this->_authmethod = 'OpenID';
216         if (DEBUG & _DEBUG_LOGIN) trigger_error(get_class($this)."::checkPass => $retval",
217                                                 E_USER_WARNING);
218         if ($retval) {
219             $this->_level = WIKIAUTH_USER;
220         } else {
221             $this->_level = WIKIAUTH_ANON;
222         }
223         return $this->_level;
224     }
225
226     /* do nothing. the login/redirect is done in checkPass */
227     function userExists() {
228         if (!$this->isValidName($this->_userid)) {
229             return $this->_tryNextUser();
230         }
231         if (!loadPhpExtension('openssl')) {
232             trigger_error
233                 (sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
234                  . sprintf(_(" %s AUTH ignored."), 'OpenID'),
235                  E_USER_WARNING);
236             return $this->_tryNextUser();
237         }
238         if (DEBUG & _DEBUG_LOGIN)
239             trigger_error(get_class($this)."::userExists => true (dummy)", E_USER_WARNING);
240         return true;
241     }
242
243     // no quotes and shorter than 128
244     function isValidName() {
245         if (!$this->_userid) return false;
246         return !preg_match('/[\"\']/', $this->_userid) and strlen($this->_userid) < 128;
247     }
248 }
249
250 // Local Variables:
251 // mode: php
252 // tab-width: 8
253 // c-basic-offset: 4
254 // c-hanging-comment-ender-p: nil
255 // indent-tabs-mode: nil
256 // End:
257 ?>