r1435: Fixed a bug in Authentication that could potentially cause the Bugdar user...
[bugdar.git] / includes / auth / auth.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 require_once('./includes/api_user.php');
23
24 /**
25 * Abstract Authentication
26 *
27 * This is an abstract class that is used to provide authentication for
28 * Bugdar.
29 *
30 * @author Blue Static
31 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
32 * @version $Revision$
33 * @package Bugdar
34 *
35 */
36 class Authentication
37 {
38 /**
39 * The database connection to AUTHENTICATE against; can be to a separate database
40 * @var object
41 */
42 var $authDb;
43
44 /**
45 * The database connection to the BUGDAR database
46 * @var object
47 */
48 var $db;
49
50 /**
51 * The Bugdar registry
52 * @var object
53 */
54 var $registry;
55
56 /**
57 * Array of user data from the AUTHENTICATION database
58 * @var array
59 */
60 var $authUser;
61
62 /**
63 * Array of user data from the BUGDAR database
64 * @var array
65 */
66 var $bugdarUser;
67
68 /**
69 * Mapping of Bugdar fields to authentication database fields; these will be synced between databases upon login.
70 * AT THE VERY MINIMUM, YOU MUST MAP THESE FIELDS:
71 * @var array
72 */
73 var $fieldMap = array(
74 'authid' => null,
75 'displayname' => null,
76 'email' => null,
77 );
78
79 // ###################################################################
80 /**
81 * Constructor
82 */
83 function __construct()
84 {
85 global $bugsys;
86
87 $this->registry =& $bugsys;
88 $this->db =& $bugsys->db;
89
90 $this->registry->debug('authentication system: ' . get_class($this));
91
92 $this->_setupDatabase();
93 }
94
95 // ###################################################################
96 /**
97 * Returns the information array for the Bugdar user. This must be
98 * called after an authentication method.
99 */
100 function fetchBugdarUser()
101 {
102 return $this->bugdarUser;
103 }
104
105 // ###################################################################
106 /**
107 * Sets up the database to authenticate against. You can create a new
108 * database object here. Whatever you choose, you need to reference
109 * Authentication->authDb to the object
110 */
111 function _setupDatabase() {}
112
113 // ###################################################################
114 /**
115 * Returns the sanitized value of the user ID or unique identifier
116 * found in the cookie of an already-authenticated user.
117 */
118 function _fetchCookieUniqueId() {}
119
120 // ###################################################################
121 /**
122 * Returns the sanitized value of the authentication key or cookie-safe
123 * password found in the cookies of an already-authenticated user.
124 */
125 function _fetchCookiePassword() {}
126
127 // ###################################################################
128 /**
129 * Returns an array of user data fetched using the user information
130 * values found in cookies. It should NOT be responsible for verifying
131 * the authentication information, but only fetching it.
132 */
133 function _fetchUserUsingCookies() {}
134
135 // ###################################################################
136 /**
137 * Returns TRUE if the cookie data values are valid in the data array
138 * returned from _fetchUserUsingCookies(), and FALSE if they are not.
139 */
140 function _verifyCookieData() {}
141
142 // ###################################################################
143 /**
144 * Authenticates the user using cookie data. You shouldn't need to
145 * customize this method if you implement all the helpers correctly.
146 * Returns TRUE if the cookies are valid and the user is logged in.
147 */
148 function authenticateCookies()
149 {
150 if (!$this->_fetchCookieUniqueId() OR !$this->_fetchCookiePassword())
151 {
152 $this->clearCookies();
153 return false;
154 }
155
156 $this->authUser = $this->_fetchUserUsingCookies();
157
158 if (!$this->authUser)
159 {
160 $this->authUser = null;
161 $this->clearCookies();
162 return false;
163 }
164
165 if ($this->_verifyCookieData())
166 {
167 $this->_setCookies(true);
168 $this->bugdarUser = $this->_fetchBugdarUserFromAuthUser();
169 if ($this->_syncBugdarUser())
170 {
171 $this->bugdarUser = $this->_fetchBugdarUserFromAuthUser();
172 }
173 return true;
174 }
175 else
176 {
177 $this->authUser = null;
178 $this->clearCookies();
179 return false;
180 }
181 }
182
183 // ###################################################################
184 /**
185 * Returns an array with the authentication user information, found
186 * by the unique login identifier passed to the function.
187 */
188 function _fetchUserWithIdentifier($string) {}
189
190 // ###################################################################
191 /**
192 * Verifies that the authUser's password matches the plain-text password
193 * passed to this function. This is basically the transformation of
194 * the plaintext to the hashed password and the result of the comparison.
195 */
196 function _verifyLoginUser($password) {}
197
198 // ###################################################################
199 /**
200 * Authenticates a user at login from two keys: an identifier and
201 * a password. In Bugdar, the identifier is an email, but it can be
202 * any unique string found in the authentication database. Returns
203 * TRUE if the authentication is successful, and FALSE if not. Also
204 * determines if the cookies are sticky ("rememember me" login)
205 */
206 function authenticateLogin($string, $password, $sticky = false)
207 {
208 $this->authUser = $this->_fetchUserWithIdentifier($string);
209
210 if (!$this->authUser)
211 {
212 $this->authUser = null;
213 return false;
214 }
215
216 if ($this->_verifyLoginUser($password))
217 {
218 $this->_setCookies($sticky);
219 $this->bugdarUser = $this->_fetchBugdarUserFromAuthUser();
220 $this->_syncBugdarUser();
221 return true;
222 }
223 else
224 {
225 $this->authUser = null;
226 return false;
227 }
228 }
229
230 // ###################################################################
231 /**
232 * Returns the BUGDAR user array from the data in the AUTHENTICATION user
233 * array. If the Bugdar user does not exist, call _createBugdarUser()
234 * to add the user into the Bugdar database. This is necessary so Bugdar options
235 * can be saved in the Bugdar database (and not in the auth one), however
236 * authentication details will NOT be stored in the Bugdar database.
237 */
238 function _fetchBugdarUserFromAuthUser()
239 {
240 $user = $this->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE authid = '" . $this->authUser[ $this->fieldMap['authid'] ] . "'");
241 if (!$user)
242 {
243 return $this->_createBugdarUser();
244 }
245 return $user;
246 }
247
248 // ###################################################################
249 /**
250 * Creates a Bugdar user with the authentication details specified in
251 * the auth array and returns it. You need to call this in
252 * _fetchBugdarUserFromAuthUser() and use the UserAPI to create the user.
253 * This will create a new user in Bugdar with the data from the authentication DB
254 * with the fields specified in fieldMap.
255 */
256 function _createBugdarUser()
257 {
258 $user = new UserAPI($this->registry);
259
260 // if the email already exists in the DB, it must be the same person so just hook up the authid
261 if ($check = $this->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email = '" . $this->db->escape_string($this->authUser[ $this->fieldMap['email'] ]) . "'"))
262 {
263 $user->set('userid', $check['userid']);
264 $user->set_condition();
265 $user->set('authid', $this->authUser[ $this->fieldMap['authid'] ]);
266 $user->update();
267 $user->fetch();
268
269 return $user->objdata;
270 }
271 else
272 {
273 $user = new UserAPI($this->registry);
274 foreach ($this->fieldMap AS $bugdar => $authdb)
275 {
276 $user->set($bugdar, $this->authUser["$authDb"]);
277 }
278 $user->set('usergroupid', 2);
279 $user->insert();
280
281 return $user->values;
282 }
283 }
284
285 // ###################################################################
286 /**
287 * Syncs a Bugdar user's fieldMap'ed values to the authentication DB's
288 * values. This allows the users to stay mostly-in-sync for the most
289 * basic of information (like email, timezone, etc.). Passwords are
290 * NOT synced. Returns TRUE if the user data was changed.
291 */
292 function _syncBugdarUser()
293 {
294 $fields = $this->fieldMap;
295 unset($fields['authid']);
296 unset($fields['password']);
297
298 $change = false;
299
300 $user = new UserAPI($this->registry);
301 $user->set('userid', $this->authUser[ $this->fieldMap['authid'] ]);
302 $user->set_condition();
303 foreach ($fields AS $bugdar => $auth)
304 {
305 if ($this->bugdarUser["$bugdar"] != $this->authUser["$auth"])
306 {
307 $user->set($bugdar, $this->authUser["$auth"]);
308 $change = true;
309 }
310 }
311 if ($change)
312 {
313 $user->update();
314 }
315
316 return $change;
317 }
318
319 // ###################################################################
320 /**
321 * Responsible for unsetting all authentication cookies because they
322 * are invalid
323 */
324 function clearCookies() {}
325
326 // ###################################################################
327 /**
328 * Sets the authentication cookies; this is done both at login and
329 * for renewing the cookies upon successful cookie validation. The
330 * option it takes determines whether the cookies are sticky or not.
331 */
332 function _setCookies($permanent = false) {}
333 }
334
335 /*=====================================================================*\
336 || ###################################################################
337 || # $HeadURL$
338 || # $Id$
339 || ###################################################################
340 \*=====================================================================*/
341 ?>