]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/OpenID.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / lib / WikiUser / OpenID.php
1 <?php
2
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     {
53         $version = 1.1;
54         $this->_setError("");
55         if (isset($params['openid_ns']) &&
56             $params['openid_ns'] == $NS_2_0
57         ) { // global session var
58             $version = 2.0;
59         }
60         if (isset($params["openid_claimed_id"])) {
61             $identity = $params["openid_claimed_id"];
62         } elseif (isset($params["openid_identity"])) {
63             $identity = $params["openid_identity"];
64         } else {
65             $identity = "";
66         }
67
68         if ($version < 2.0 && !isset($params["openid_claimed_id"])) {
69             global $request;
70             $session = $request->getSessionVar('openid');
71             if (!$session) {
72                 $request->setSessionVar('openid', array());
73             }
74             if ($session['identity'] == $identity) {
75                 $identity = $session['claimed_id'];
76             }
77         }
78         if (empty($params['openid_return_to'])) {
79             $this->_setError("Missing openid.return_to");
80             return false;
81         }
82         if (empty($params['openid_signed'])) {
83             $this->_setError("Missing openid.signed");
84             return false;
85         }
86         if (empty($params['openid_sig'])) {
87             $this->_setError("Missing openid.sig");
88             return false;
89         }
90         if (empty($params['openid_mode'])) {
91             $this->_setError("Missing openid.mode");
92             return false;
93         }
94         if ($params['openid_mode'] != 'id_res') {
95             $this->_setError("Wrong openid.mode '" . $params['openid_mode'] . "' != 'id_res'");
96             return false;
97         }
98         if (empty($params['openid_assoc_handle'])) {
99             $this->_setError("Missing openid.assoc_handle");
100             return false;
101         }
102     }
103
104     /**
105      * Performs check of OpenID identity.
106      *
107      * This is the first step of OpenID authentication process.
108      * On success the function does not return (it does HTTP redirection to
109      * server and exits). On failure it returns false.
110      *
111      * @param bool                              $immediate  enables or disables interaction with user
112      * @param string                            $id         OpenID identity
113      * @param string                            $returnTo   HTTP URL to redirect response from server to
114      * @param string                            $root       HTTP URL to identify consumer on server
115      * @param mixed                             $extensions extension object or array of extensions objects
116      * @param Zend_Controller_Response_Abstract $response   an optional response
117      *  object to perform HTTP or HTML form redirection
118      * @return bool
119      */
120     function _checkId($immediate, $id, $returnTo = null, $root = null,
121                       $extensions = null, $response = null)
122     {
123         $this->_setError('');
124
125         /*if (!Zend_OpenId::normalize($id)) {
126             $this->_setError("Normalisation failed");
127             return false;
128         }*/
129         $claimedId = $id;
130
131         if (!$this->_discovery($id, $server, $version)) {
132             $this->_setError("Discovery failed");
133             return false;
134         }
135         if (!$this->_associate($server, $version)) {
136             $this->_setError("Association failed");
137             return false;
138         }
139         if (!$this->_getAssociation(
140             $server,
141             $handle,
142             $macFunc,
143             $secret,
144             $expires)
145         ) {
146             /* Use dumb mode */
147             unset($handle);
148             unset($macFunc);
149             unset($secret);
150             unset($expires);
151         }
152
153         $params = array();
154         if ($version >= 2.0) {
155             //$params['openid.ns'] = Zend_OpenId::NS_2_0;
156         }
157
158         $params['openid.mode'] = $immediate ?
159             'checkid_immediate' : 'checkid_setup';
160
161         $params['openid.identity'] = $id;
162
163         $params['openid.claimed_id'] = $claimedId;
164
165         if ($version <= 2.0) {
166             global $request;
167             $session = $request->getSessionVar('openid');
168             $request->setSessionVar('identity', $id);
169             $request->setSessionVar('claimed_id', $claimedId);
170         }
171
172         if (isset($handle)) {
173             $params['openid.assoc_handle'] = $handle;
174         }
175
176         //$params['openid.return_to'] = Zend_OpenId::absoluteUrl($returnTo);
177
178         // See lib/WikiUser/FaceBook.php how to handle http requests
179         $web = new HttpClient("$server", 80);
180         if (DEBUG & _DEBUG_LOGIN) $web->setDebug(true);
181
182         if (empty($root)) {
183             //$root = Zend_OpenId::selfUrl();
184             if ($root[strlen($root) - 1] != '/') {
185                 $root = dirname($root);
186             }
187         }
188         if ($version >= 2.0) {
189             $params['openid.realm'] = $root;
190         } else {
191             $params['openid.trust_root'] = $root;
192         }
193
194         /*if (!Zend_OpenId_Extension::forAll($extensions, 'prepareRequest', $params)) {
195             $this->_setError("Extension::prepareRequest failure");
196             return false;
197         }
198         */
199
200         //Zend_OpenId::redirect($server, $params, $response);
201         return true;
202     }
203
204     function _setError($message)
205     {
206         $this->_error = $message;
207     }
208
209     function checkPass($password)
210     {
211         if (!loadPhpExtension('openssl')) {
212             trigger_error(
213                 sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
214                     . sprintf(_(" %s AUTH ignored."), 'OpenID'),
215                 E_USER_WARNING);
216             return $this->_tryNextUser();
217         }
218
219         $retval = $this->_checkId(false, $id, $returnTo, $root, $extensions, $response);
220         $this->_authmethod = 'OpenID';
221         if (DEBUG & _DEBUG_LOGIN) trigger_error(get_class($this) . "::checkPass => $retval",
222             E_USER_WARNING);
223         if ($retval) {
224             $this->_level = WIKIAUTH_USER;
225         } else {
226             $this->_level = WIKIAUTH_ANON;
227         }
228         return $this->_level;
229     }
230
231     /* do nothing. the login/redirect is done in checkPass */
232     function userExists()
233     {
234         if (!$this->isValidName($this->_userid)) {
235             return $this->_tryNextUser();
236         }
237         if (!loadPhpExtension('openssl')) {
238             trigger_error
239             (sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
240                     . sprintf(_(" %s AUTH ignored."), 'OpenID'),
241                 E_USER_WARNING);
242             return $this->_tryNextUser();
243         }
244         if (DEBUG & _DEBUG_LOGIN)
245             trigger_error(get_class($this) . "::userExists => true (dummy)", E_USER_WARNING);
246         return true;
247     }
248
249     // no quotes and shorter than 128
250     function isValidName()
251     {
252         if (!$this->_userid) return false;
253         return !preg_match('/[\"\']/', $this->_userid) and strlen($this->_userid) < 128;
254     }
255 }
256
257 // Local Variables:
258 // mode: php
259 // tab-width: 8
260 // c-basic-offset: 4
261 // c-hanging-comment-ender-p: nil
262 // indent-tabs-mode: nil
263 // End: