Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 465bbc5

Browse files
committed
Rework middleware
1 parent 3d43cb2 commit 465bbc5

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

src/Middleware/WindowsAuthenticate.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
namespace Adldap\Laravel\Middleware;
44

55
use Closure;
6-
use Adldap\Models\ModelNotFoundException;
7-
use Adldap\Laravel\Traits\UsesAdldap;
8-
use Adldap\Laravel\Traits\DispatchesAuthEvents;
6+
use Adldap\Models\User;
7+
use Adldap\Laravel\Facades\Resolver;
8+
use Adldap\Laravel\Commands\Import;
9+
use Adldap\Laravel\Commands\SyncPassword;
910
use Adldap\Laravel\Auth\DatabaseUserProvider;
1011
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
12+
use Adldap\Laravel\Events\AuthenticatedWithWindows;
1113
use Illuminate\Http\Request;
1214
use Illuminate\Contracts\Auth\Guard;
15+
use Illuminate\Support\Facades\Bus;
16+
use Illuminate\Support\Facades\Event;
1317

1418
class WindowsAuthenticate
1519
{
16-
use UsesAdldap, DispatchesAuthEvents;
17-
1820
/**
1921
* The authenticator implementation.
2022
*
@@ -44,7 +46,7 @@ public function handle(Request $request, Closure $next)
4446
{
4547
if (!$this->auth->check()) {
4648
// Retrieve the SSO login attribute.
47-
$auth = $this->getWindowsAuthAttribute();
49+
$auth = $this->attribute();
4850

4951
// Retrieve the SSO input key.
5052
$key = key($auth);
@@ -95,61 +97,66 @@ protected function retrieveAuthenticatedUser($key, $username)
9597
{
9698
$provider = $this->auth->getProvider();
9799

98-
try {
99-
$resolver = $this->getResolver();
100-
101-
// Find the user in AD.
102-
$user = $resolver->query()->where([$key => $username])->firstOrFail();
103-
100+
// Find the user in AD.
101+
if ($user = Resolver::query()->where([$key => $username])->first()) {
104102
if ($provider instanceof NoDatabaseUserProvider) {
105-
$this->handleAuthenticatedWithWindows($user);
103+
Event::fire(new AuthenticatedWithWindows($user));
106104

107105
return $user;
108106
} elseif ($provider instanceof DatabaseUserProvider) {
109-
$credentials = [
110-
$resolver->getEloquentUsername() => $user->getFirstAttribute($resolver->getLdapUsername()),
111-
];
107+
$credentials = $this->makeCredentials($user);
112108

113109
// Here we'll import the AD user. If the user already exists in
114110
// our local database, it will be returned from the importer.
115-
$model = $this->getImporter()->run($user, $this->getModel(), $credentials);
111+
$model = Bus::dispatch(
112+
new Import($user, $this->model(), $credentials)
113+
);
116114

117-
// We'll assign a random password for the authenticating user.
118-
$password = str_random();
119-
120-
// Set the models password.
121-
$model->password = $model->hasSetMutator('password') ?
122-
$password : bcrypt($password);
115+
// We'll sync / set the users password after
116+
// our model has been synchronized.
117+
Bus::dispatch(new SyncPassword($model));
123118

124119
// We also want to save the returned model in case it doesn't
125120
// exist yet, or there are changes to be synced.
126121
$model->save();
127122

128-
$this->handleAuthenticatedWithWindows($user, $model);
123+
Event::fire(new AuthenticatedWithWindows($user, $model));
129124

130125
return $model;
131126
}
132-
} catch (ModelNotFoundException $e) {
133-
// User could not be located.
134127
}
135128
}
136129

130+
/**
131+
* Returns a credentials array to be used in the import command.
132+
*
133+
* @param User $user
134+
*
135+
* @return array
136+
*/
137+
protected function makeCredentials(User $user)
138+
{
139+
return [
140+
Resolver::getEloquentUsername() => $user->getFirstAttribute(Resolver::getLdapUsername()),
141+
];
142+
}
143+
137144
/**
138145
* Returns the configured authentication model.
139146
*
140147
* @return \Illuminate\Database\Eloquent\Model
141148
*/
142-
protected function getModel()
149+
protected function model()
143150
{
144-
return auth()->getProvider()->createModel();
151+
return $this->auth->getProvider()->createModel();
145152
}
146153

147154
/**
148155
* Returns the windows authentication attribute.
149156
*
150157
* @return string
151158
*/
152-
protected function getWindowsAuthAttribute()
159+
protected function attribute()
153160
{
154161
return config('adldap_auth.windows_auth_attribute', ['samaccountname' => 'AUTH_USER']);
155162
}

0 commit comments

Comments
 (0)