Skip to content

Commit 4ccf748

Browse files
committed
add /rest/v1/validatepassword.php api endpoint :
- takes a login and a password as x-www-form-urlencoded format parameters - returns an "isValid" boolean - uses the project root cache folder to store recent LDAP queries using symfony cache system The "isValid" boolean returned is : - true if the login doesn't exist in the LDAP - true if the login exists and the password doesn't contain any forbidden ldap entry - false if the login exists and the password contains a forbidden ldap entry
1 parent 5815f2a commit 4ccf748

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

rest/v1/validatepassword.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
4+
5+
global $ldapInstance, $ldaphost, $ldap_base, $pwd_forbidden_ldap_fields;
6+
7+
require_once("./include.php");
8+
9+
#==============================================================================
10+
# Action
11+
#==============================================================================
12+
$return = Array();
13+
$login = "";
14+
$password = "";
15+
16+
if (
17+
isset($_POST["login"]) && $_POST["login"] &&
18+
isset($_POST["password"]) && $_POST["password"]
19+
) {
20+
$login = $_POST["login"];
21+
$password = $_POST["password"];
22+
} else {
23+
$return['message'] = "Login and password required.";
24+
http_response_code(400);
25+
echo json_encode($return);
26+
return;
27+
}
28+
29+
$attributes = $pwd_forbidden_ldap_fields;
30+
31+
#==============================================================================
32+
# Load data from cache or LDAP
33+
#==============================================================================
34+
$userData = [];
35+
36+
$cache = new FilesystemAdapter(
37+
namespace: 'ldap_forbiddenvalidation_cache',
38+
defaultLifetime: 300,
39+
directory: dirname(__DIR__, 2) . '/cache'
40+
);
41+
42+
$cacheKey = 'ldap_user_' . md5($login);
43+
$cachedItem = $cache->getItem($cacheKey);
44+
45+
if ($cachedItem->isHit()) {
46+
$userData = $cachedItem->get();
47+
}
48+
else {
49+
$filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" . $login . "))";
50+
51+
// Connect to LDAP
52+
$ldap_connection = $ldapInstance->connect();
53+
$ldap = $ldap_connection[0];
54+
55+
// Assert successful LDAP connection
56+
if (!$ldap_connection) {
57+
$return['message'] = "Could not connect to {$ldaphost}.";
58+
http_response_code(500);
59+
echo json_encode($return);
60+
return;
61+
}
62+
63+
// Search LDAP using filter, get the entries, and set count.
64+
$search = ldap_search($ldap, $ldap_base, $filter, $attributes, 0, 0);
65+
66+
$userData = ldap_get_entries($ldap, $search);
67+
$cachedItem->set($userData);
68+
if (!$cache->save($cachedItem)) {
69+
error_log("Error while trying to write cache");
70+
}
71+
}
72+
73+
#==============================================================================
74+
# Process data
75+
#==============================================================================
76+
if ($userData["count"] === 0) {
77+
http_response_code(404);
78+
$return['message'] = "User not found.";
79+
echo json_encode($return);
80+
return;
81+
}
82+
83+
$upperPwd = strtoupper($password);
84+
85+
foreach ($attributes as $attribute) {
86+
$lowerAttribute = strtolower($attribute);
87+
if(!isset($userData[0][$lowerAttribute][0])) {
88+
continue;
89+
}
90+
91+
$upperValue = strtoupper($userData[0][$lowerAttribute][0]);
92+
93+
if(str_contains($upperPwd, $upperValue)) {
94+
$return['message'] = "User found.";
95+
$return['isValid'] = false;
96+
97+
http_response_code(200);
98+
echo json_encode($return);
99+
100+
return;
101+
}
102+
}
103+
104+
$return['message'] = "User found.";
105+
$return['isValid'] = true;
106+
http_response_code(200);
107+
echo json_encode($return);

0 commit comments

Comments
 (0)