Compare commits

...

4 Commits

Author SHA1 Message Date
ff6cd38c8d update README 2025-03-27 21:03:55 +01:00
9e21bdc135 add doc 2025-03-27 21:00:25 +01:00
513b7563b6 refactor 2025-03-27 20:38:02 +01:00
1521d8dec2 add README 2025-03-27 20:36:51 +01:00
10 changed files with 112 additions and 48 deletions

View File

@@ -0,0 +1,31 @@
# PHP LDAP
## Configuration
### AD
Par défaut, l'application se connecte au serveur AD à l'adresse `woodywood.local`
### Base de données
Par défaut, l'application se connecte à la base de données locale
avec l'utilisateur `root` et le mot de passe `4321`
## Captures d'écran
Page d'authentification :
[[screenshots/auth.png]]
Page d'erreur :
[[screenshots/error.png]]
Page utilisateur :
[[screenshots/success.png]]
Page admin (en se connectant avec l'utilisateur Administrateur):
[[screenshots/admin.png]]

View File

@@ -1,3 +1,27 @@
<?php <?php
phpinfo();
?> if (!isset($_POST["domain"]) || !isset($_POST["user"]) || !isset($_POST["password"])) {
require_once "templates/login_form.html";
exit;
}
$domain = rtrim($_POST["domain"]);
$user = rtrim($_POST["user"]);
$password = rtrim($_POST["password"]);
require_once "src/ldap.php";
$result = LdapIsConnected($domain, $user, $password, []);
if (!$result) {
require_once "templates/login_failed.html";
exit;
}
require_once "view/View.php";
$info = LdapGetUserInfo($user);
$body = PrintLoginInfo($info);
require_once "templates/login_success.html.php";

View File

@@ -1,27 +0,0 @@
<?php
if (!isset($_POST["domain"]) || !isset($_POST["user"]) || !isset($_POST["password"])) {
require_once "templates/login_form.html";
exit;
}
$domain = rtrim($_POST["domain"]);
$user = rtrim($_POST["user"]);
$password = rtrim($_POST["password"]);
require_once "ldap.php";
$result = LdapIsConnected($domain, $user, $password, []);
if (!$result) {
require_once "templates/login_failed.html";
exit;
}
require_once "view/View.php";
$info = LdapGetUserInfo($user);
$body = PrintLoginInfo($info);
require_once "templates/login_success.html.php";

BIN
screenshots/admin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

BIN
screenshots/auth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
screenshots/error.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
screenshots/success.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

View File

@@ -1,14 +1,18 @@
<?php <?php
$table_name = "authentification_attempts";
/**
* Connexion à la base de données
*/
function GetDbConnection(): ?PDO function GetDbConnection(): ?PDO
{ {
$host = 'localhost'; // ou l'adresse IP du serveur MariaDB $host = 'localhost';
$dbname = 'ldap'; // nom de votre base de donn<6E>es $dbname = 'ldap';
$username = 'root'; // nom d'utilisateur MariaDB $username = 'root';
$password = '4321'; // mot de passe pour l'utilisateur $password = '4321';
try { try {
// Cr<EFBFBD>ation d'une instance PDO pour la connexion <EFBFBD> la base de donn<EFBFBD>es // Création d'une instance PDO pour la connexion à la base de données
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// Configuration du mode d'erreur de PDO pour les exceptions // Configuration du mode d'erreur de PDO pour les exceptions
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@@ -39,9 +43,12 @@ class AuthAttempt
} }
} }
/**
* Ajoute une entrée dans la table des tentatives de connexion
*/
function InsertLine(AuthAttempt $attempt) function InsertLine(AuthAttempt $attempt)
{ {
$table_name = "authentification_attempts"; global $table_name;
$pdo = GetDbConnection(); $pdo = GetDbConnection();
$query = $pdo->prepare("INSERT INTO $table_name(`username`, `status`, `ip_address`) VALUES (:user, :status, :ip);"); $query = $pdo->prepare("INSERT INTO $table_name(`username`, `status`, `ip_address`) VALUES (:user, :status, :ip);");
@@ -53,8 +60,11 @@ function InsertLine(AuthAttempt $attempt)
$query->execute(); $query->execute();
} }
/**
* Récupère toutes les lignes de tentative de connexion
*/
function GetLines() : array { function GetLines() : array {
$table_name = "authentification_attempts"; global $table_name;
$pdo = GetDbConnection(); $pdo = GetDbConnection();
$query = $pdo->prepare("SELECT * FROM $table_name;"); $query = $pdo->prepare("SELECT * FROM $table_name;");

View File

@@ -23,6 +23,9 @@ class UserInfo
} }
} }
/**
* Connexion avec des identifiants
*/
function LdapConnect(string $domain, string $username, string $password): LDAP\Result|false function LdapConnect(string $domain, string $username, string $password): LDAP\Result|false
{ {
global $handle; global $handle;
@@ -30,7 +33,10 @@ function LdapConnect(string $domain, string $username, string $password): LDAP\R
return $bind; return $bind;
} }
function LdapIsConnected(string $domain, string $username, string $password) { /**
* Se connecte à l'AD et vérifie la validité des identifiants
*/
function LdapIsConnected(string $domain, string $username, string $password) : bool {
global $handle; global $handle;
$result = LdapConnect($domain, $username, $password); $result = LdapConnect($domain, $username, $password);
ldap_parse_result($handle, $result, $error_code, $matched_dn, $error_message, $referrals, $controls); ldap_parse_result($handle, $result, $error_code, $matched_dn, $error_message, $referrals, $controls);
@@ -39,20 +45,16 @@ function LdapIsConnected(string $domain, string $username, string $password) {
return $success; return $success;
} }
/**
* Enregistre la tentative de connexion dans la base de données
*/
function LogConnection(string $username, bool $success) { function LogConnection(string $username, bool $success) {
InsertLine(new AuthAttempt($username, $success ? "success" : "failure", $_SERVER['REMOTE_ADDR'])); InsertLine(new AuthAttempt($username, $success ? "success" : "failure", $_SERVER['REMOTE_ADDR']));
} }
function LdapConnectAndBind() /**
{ * Retourne les informations d'un utilisateur
global $ldap_domain_name; */
$ldap_instance = ldap_connect("ldap://$ldap_domain_name.local");
ldap_set_option($ldap_instance, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_instance, LDAP_OPT_REFERRALS, 0);
ldap_bind($ldap_instance, "Administrateur@woodywood", "3AFISE+25");
return $ldap_instance;
}
function LdapGetUserInfo(string $user): ?UserInfo function LdapGetUserInfo(string $user): ?UserInfo
{ {
global $handle; global $handle;
@@ -71,12 +73,18 @@ function LdapGetUserInfo(string $user): ?UserInfo
return null; return null;
} }
/**
* Permet de retourner les valeur dans une chaîne de caractère de type dn
*/
function GetValue(string $dnStr, string $key): array function GetValue(string $dnStr, string $key): array
{ {
preg_match_all("/$key=([^,]+)/", $dnStr, $matches); preg_match_all("/$key=([^,]+)/", $dnStr, $matches);
return isset($matches[1]) ? $matches[1] : []; return isset($matches[1]) ? $matches[1] : [];
} }
/**
* Retourne tous les objets du type spécifié dans l'ou souhaitée et filtre le résultat
*/
function LdapGetObjectsInOU(string $ou, string $objectType, string $field): array function LdapGetObjectsInOU(string $ou, string $objectType, string $field): array
{ {
global $ldap_domain_name; global $ldap_domain_name;
@@ -110,11 +118,17 @@ function LdapGetObjectsInOU(string $ou, string $objectType, string $field): arra
return $object_values; return $object_values;
} }
/**
* Retourne la liste des utilisateurs d'une OU
*/
function LdapGetUsersInOU(string $ou): array function LdapGetUsersInOU(string $ou): array
{ {
return LdapGetObjectsInOU($ou, "user", "CN"); return LdapGetObjectsInOU($ou, "user", "CN");
} }
/**
* Retourne la liste des groupes d'une OU
*/
function LdapGetGroupsInOU(string $ou): array function LdapGetGroupsInOU(string $ou): array
{ {
return LdapGetObjectsInOU($ou, "group", "CN"); return LdapGetObjectsInOU($ou, "group", "CN");

View File

@@ -2,6 +2,9 @@
$admin_account = "Administrateur"; $admin_account = "Administrateur";
/**
* Affiche les éléments d'une liste
*/
function PrintListFirsts(string $title, array $liste): string function PrintListFirsts(string $title, array $liste): string
{ {
$result = '<div class="list-group-item"><h5>' . $title . '</h5>'; $result = '<div class="list-group-item"><h5>' . $title . '</h5>';
@@ -13,6 +16,9 @@ function PrintListFirsts(string $title, array $liste): string
return $result; return $result;
} }
/**
* Affiche les informations de l'utilisateur
*/
function PrintLoginInfo($info) function PrintLoginInfo($info)
{ {
global $admin_account; global $admin_account;
@@ -33,10 +39,16 @@ function PrintLoginInfo($info)
return $body; return $body;
} }
/**
* Retourne la chaîne traduite de l'état d'une tentative de connexion
*/
function translateSuccess(string $success) { function translateSuccess(string $success) {
return $success == "success" ? "Succès" : "Échec"; return $success == "success" ? "Succès" : "Échec";
} }
/**
* Affiche l'interface administrateur
*/
function PrintAdminInterface(): string function PrintAdminInterface(): string
{ {
$auth_attempts = GetLines(); $auth_attempts = GetLines();