-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathLoginController.php
More file actions
158 lines (133 loc) · 5.3 KB
/
LoginController.php
File metadata and controls
158 lines (133 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use LdapRecord\Auth\BindException;
use LdapRecord\Container;
use LdapRecord\Models\Entry as LdapEntry;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected string $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function username(): string
{
return 'login';
}
/**
* LDAP bind (LDAPRecord v2)
*/
protected function ldapBindAndGetUser(string $appUsername, string $password): ?LdapEntry
{
if ($appUsername === '' || $password === '') {
Log::debug('LDAP skipped: empty identifier or password.');
return null;
}
try {
$query = LdapEntry::query();
// Optionnel : restreindre à une OU si configuré
$base = trim((string) config('app.ldap_users_base_dn'));
if ($base !== '') {
$query->in($base);
}
// Attributs de login à tester côté LDAP (uid, sAMAccountName, etc.)
$attrs = array_values(array_filter(array_map('trim', explode(',', (string) config('app.ldap_login_attributes')))));
if (empty($attrs)) {
Log::warning('LDAP login aborted: app.ldap_login_attributes is empty.');
return null;
}
// Filtre OR sur les attributs configurés
$first = true;
foreach ($attrs as $attr) {
if ($first) {
$query->whereEquals($attr, $appUsername);
$first = false;
} else {
$query->orWhereEquals($attr, $appUsername);
}
}
// Collision guard
$results = $query->limit(2)->get();
if ($results->count() === 0) {
Log::debug('LDAP user not found for identifier.', ['identifier' => $appUsername]);
return null;
}
if ($results->count() > 1) {
Log::warning('LDAP identifier collision: multiple entries match.', [
'identifier' => $appUsername,
'attributes' => $attrs,
]);
return null;
}
/** @var LdapEntry $ldapUser */
$ldapUser = $results->first();
$connection = Container::getConnection();
$dn = $ldapUser->getDn();
if ($dn && $connection->auth()->attempt($dn, $password, true)) {
return $ldapUser;
}
return null;
} catch (BindException $e) {
Log::warning('LDAP bind failed', [
'error' => $e->getMessage(),
'diagnostic' => $e->getDetailedError()?->getDiagnosticMessage(),
]);
return null;
} catch (\Throwable $e) {
Log::error('LDAP error: '.$e->getMessage());
return null;
}
}
/**
* Login avec LDAP optionnel + fallback local (UNIQUEMENT via 'login').
*/
protected function attemptLogin(Request $request): bool
{
$useLdap = (bool) config('app.ldap_enabled');
$fallbackLocal = (bool) config('app.ldap_fallback_local');
$autoProvision = (bool) config('app.ldap_auto_provision');
$credentials = $request->only($this->username(), 'password'); // ['login' => ..., 'password' => ...]
$identifier = (string) ($credentials[$this->username()] ?? '');
$password = (string) ($credentials['password'] ?? '');
$remember = $request->boolean('remember');
if ($useLdap) {
$ldapUser = $this->ldapBindAndGetUser($identifier, $password);
if ($ldapUser) {
// Mapping local UNIQUEMENT par 'login'
$local = User::where('login', $identifier)->first();
if (!$local && $autoProvision) {
$local = User::create([
'name' => $ldapUser->getFirstAttribute('cn') ?: $identifier,
'email' => $ldapUser->getFirstAttribute('mail') ?: 'user@localhost.local',
'login' => $identifier,
'role' => 5,
'password' => Hash::make(Str::random(32)), // inutilisable en local par défaut
]);
}
if ($local) {
$this->guard()->login($local, $remember);
return true;
}
// LDAP OK mais pas d'utilisateur local et pas d’auto-provision
return false;
}
// LDAP KO → éventuel fallback local (toujours via 'login')
if (!$fallbackLocal) {
return false;
}
}
// Auth locale (Laravel) — utilisera ['login' => ..., 'password' => ...]
return $this->guard()->attempt(
$this->credentials($request), // credentials() retournera login + password car username() = 'login'
$remember
);
}
}