ldap-matter/oauth/connexion.php

92 lines
2.8 KiB
PHP
Raw Normal View History

2017-08-08 03:01:11 +08:00
<?php
session_start();
/**
* @author Denis CLAVIER <clavierd at gmail dot com>
*/
// include our LDAP object
require_once __DIR__.'/LDAP/LDAP.php';
require_once __DIR__.'/LDAP/config_ldap.php';
2017-08-08 03:01:11 +08:00
// Verify all fields have been filled
if (empty($_POST['user']) || empty($_POST['password']))
{
echo 'Please fill in your Username and Password<br /><br />';
2017-08-08 03:01:11 +08:00
echo 'Click <a href="./index.php">here</a> to come back to login page';
}
else
{
// Check received data length (to prevent code injection)
if (strlen($_POST['user']) > 15)
{
2020-04-30 05:55:04 +08:00
echo 'Username is longer than 15 characters ... Please try again<br /><br />';
2017-08-08 03:01:11 +08:00
echo 'Click <a href="./index.php">here</a> to come back to login page';
}
2020-04-30 05:50:57 +08:00
elseif (strlen($_POST['password']) > 50)
2017-08-08 03:01:11 +08:00
{
2020-04-30 05:50:57 +08:00
echo 'Password is longer than 50 characters ... Please try again<br /><br />';
echo 'Click <a href="./index.php">here</a> to come back to login page';
} elseif (strlen($_POST['password']) <= 7)
{
echo 'Password is shorter than 7 characters ... Please try again<br /><br />';
echo 'Click <a href="./index.php">here</a> to come back to login page';
2017-08-08 03:01:11 +08:00
}
else
{
// Remove every html tag and useless space on username (to prevent XSS)
$user=strip_tags(trim($_POST['user']));
$user=$_POST['user'];
$password=$_POST['password'];
// Open a LDAP connection
$ldap = new LDAP($ldap_host,$ldap_port,$ldap_version);
2017-08-08 03:01:11 +08:00
// Check user credential on LDAP
try{
$authenticated = $ldap->checkLogin($user,$password,$ldap_search_attribute,$ldap_filter,$ldap_base_dn,$ldap_bind_dn,$ldap_bind_pass);
}
catch (Exception $e)
{
2020-05-01 01:21:56 +08:00
if ($e->getCode() == 404) {
$resp = json_encode(
[
"error" => "User not found",
"message" => "$user is not in the group of authorized users."
]
);
} else {
$resp = json_encode(array("error" => "Impossible to get data", "message" => $e->getMessage()));
2020-05-01 01:21:56 +08:00
}
$authenticated = false;
}
// If user is authenticated
if ($authenticated)
2017-08-08 03:01:11 +08:00
{
$_SESSION['uid']=$user;
// If user came here with an autorize request, redirect him to the authorize page. Else prompt a simple message.
if (isset($_SESSION['auth_page']))
{
$auth_page=$_SESSION['auth_page'];
header('Location: ' . $auth_page);
exit();
}
else
{
echo "Congratulation you are authenticated ! <br /><br /> However there is nothing to do here ...";
}
}
// check login on LDAP has failed. Login and password were invalid or LDAP is unreachable
else
{
echo "Authentication failed ... Check your username and password.<br />If error persist contact your administrator.<br /><br />";
2017-08-08 03:01:11 +08:00
echo 'Click <a href="./index.php">here</a> to come back to login page';
echo '<br /><br /><br />' . $resp;
2017-08-08 03:01:11 +08:00
}
}
}