Authenticating against Multiple Servers
| From: | Feroze Md. Arif | Date: | Sun, 30 Nov 2003 13:23:19 +0000 |
| Subject: | Authenticating against Multiple Servers | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-9221@lists.php.net to get a copy of this message | ||
Hi,
I am building an application that needs to be authenticated against Multiple
servers (two MySQL databases & two LDAP Servers). I have written a script
that uses the PEAR AUTH package. I am not very satisfied with the code
though it works. I am pasting the code below for your comments. I would
appreciate it if anyone could suggest a more elegant approach.
Best Regards
Feroze
==================
Jar Jar Binks will be Jedi!
<?php
require_once('Auth/Auth.php');
$authFail = 0; // A global Variable used in the loginFunction. Sigh!
if ($_GET['action'] == "logout") {
// Logout the User
checkLogin("logout");
}
//Do the login check only if the Session variable is missing.
//Since this code has to be put into every protected page,
//it makes sense to do this, especially if there are a lot of servers to
//authenticate against. But then again, who would want to authenticate
against
//a lot of servers?
if($_SESSION['auth']['registered'] || checkLogin()){
//If the User is logged in Display the Page
echo "Welcome ". ucwords($_SESSION['username']). "<br />";
echo "<a
href=\"".$_SERVER['PHP_SELF']."?action=logout\">Logout</a><br
/>";
echo "<a href=\"samp.htm\">Another Page</a>";
}
function loginFunction()
{
//The login Function as prescribed by the doctors at PEARAUTH
global $authFail;
if($authFail && isset($_POST['submitted'])) {
// If the authentication has failed after submission
// return to try and authenticate off the next server
return 0;
}
// Display the Login Form
echo "<form method=\"post\" action=\"" .
$_SERVER['PHP_SELF'] . "\">";
echo "<input type=\"text\" name=\"username\">";
echo "<input type=\"password\" name=\"password\">";
echo "<input type=\"hidden\" name=\"submitted\"
value=\"1\">";
echo "<input type=\"submit\">";
echo "</form>";
exit();
}
function checkLogin($action=""){
global $authFail;
//Create an array of DSNs
$dsn[0] = array('type'=>'DB',
'dsnstuff'=>"mysql://dbuser:dbpassword@localhost/dbname");
$dsn[1] = array('type'=>'LDAP','dsnstuff'=>array(
'host' => 'ldapserver',
'port' =>389,
'basedn' => 'basedn',
'userattr' => 'uid'));
//Loop through the available servers & authentication methods
foreach($dsn as $key=>$dsnvalue){
$authenticated = 0;
if(isset($a)) unset($a);
$authFail = 1;
$a = new Auth($dsnvalue['type'], $dsnvalue['dsnstuff'],
"loginFunction");
$a->start(); //If authentication fails the loginFunction is called and
since
//the global variable $authFail is true the loginFunction will
//return control to this point again
if($a->getAuth()) {
if($action == "logout"){
$authFail = 0;
$a->logout();
$a->start();
}
return 1;
}
}
//The user couldn't be authenticated on any server so set global variable
$authFail to false
//and let the loginFunction be called to display the login form
$authFail = 0;
$a->start();
return 1;
}
?>