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

Commit d37b67c

Browse files
committed
Finishing up logging of authentication requests
1 parent 144dd5c commit d37b67c

21 files changed

+314
-43
lines changed

src/AdldapAuthServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,19 @@ protected function registerListeners()
9797
Event::listen(Authenticated::class, Listeners\BindsLdapUserModel::class);
9898

9999
if ($this->isLogging()) {
100+
// If logging is enabled, we will set up our event listeners that
101+
// log each event fired throughout the authentication process.
100102
Event::listen(Events\Importing::class, Listeners\LogImport::class);
101103
Event::listen(Events\Synchronized::class, Listeners\LogSynchronized::class);
102104
Event::listen(Events\Synchronizing::class, Listeners\LogSynchronizing::class);
105+
Event::listen(Events\Authenticated::class, Listeners\LogAuthenticated::class);
106+
Event::listen(Events\Authenticating::class, Listeners\LogAuthentication::class);
107+
Event::listen(Events\AuthenticationFailed::class, Listeners\LogAuthenticationFailure::class);
108+
Event::listen(Events\AuthenticationRejected::class, Listeners\LogAuthenticationRejection::class);
109+
Event::listen(Events\AuthenticationSuccessful::class, Listeners\LogAuthenticationSuccess::class);
103110
Event::listen(Events\DiscoveredWithCredentials::class, Listeners\LogDiscovery::class);
104111
Event::listen(Events\AuthenticatedWithWindows::class, Listeners\LogWindowsAuth::class);
105112
Event::listen(Events\AuthenticatedModelTrashed::class, Listeners\LogTrashedModel::class);
106-
Event::listen(Events\AuthenticatedWithCredentials::class, Listeners\LogCredentialAuth::class);
107113
}
108114
}
109115

src/Auth/DatabaseUserProvider.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Adldap\Laravel\Facades\Resolver;
77
use Adldap\Laravel\Commands\Import;
88
use Adldap\Laravel\Commands\SyncPassword;
9+
use Adldap\Laravel\Events\AuthenticationRejected;
10+
use Adldap\Laravel\Events\AuthenticationSuccessful;
911
use Adldap\Laravel\Events\DiscoveredWithCredentials;
1012
use Adldap\Laravel\Events\AuthenticatedWithCredentials;
1113
use Illuminate\Support\Facades\Bus;
@@ -131,15 +133,19 @@ public function validateCredentials(Authenticatable $model, array $credentials)
131133
// validation rules pass, we will allow the authentication
132134
// attempt. Otherwise, it is automatically rejected.
133135
if ($this->passesValidation($this->user, $model)) {
134-
// Sync / set the users password since it has been verified.
136+
// Here we can now synchronize / set the users password since
137+
// they have successfully passed authentication
138+
// and our validation rules.
135139
Bus::dispatch(new SyncPassword($model, $credentials));
136140

137-
// All of our validation rules have passed and we can
138-
// finally save the model in case of changes.
139141
$model->save();
140142

143+
Event::fire(new AuthenticationSuccessful($this->user));
144+
141145
return true;
142146
}
147+
148+
Event::fire(new AuthenticationRejected($this->user));
143149
}
144150

145151
if ($this->isFallingBack() && $model->exists) {

src/Auth/NoDatabaseUserProvider.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Adldap\Laravel\Auth;
44

55
use Adldap\Laravel\Facades\Resolver;
6+
use Adldap\Laravel\Events\AuthenticationRejected;
7+
use Adldap\Laravel\Events\AuthenticationSuccessful;
68
use Adldap\Laravel\Events\DiscoveredWithCredentials;
79
use Adldap\Laravel\Events\AuthenticatedWithCredentials;
810
use Illuminate\Support\Facades\Event;
@@ -57,14 +59,16 @@ public function retrieveByCredentials(array $credentials)
5759
*/
5860
public function validateCredentials(Authenticatable $user, array $credentials)
5961
{
60-
// Perform LDAP authentication and validate the authenticated model.
61-
if (
62-
Resolver::authenticate($user, $credentials) &&
63-
$this->passesValidation($user)
64-
) {
62+
if (Resolver::authenticate($user, $credentials)) {
6563
Event::fire(new AuthenticatedWithCredentials($user));
6664

67-
return true;
65+
if ($this->passesValidation($user)) {
66+
Event::fire(new AuthenticationSuccessful($user));
67+
68+
return true;
69+
}
70+
71+
Event::fire(new AuthenticationRejected($user));
6872
}
6973

7074
return false;

src/Events/Authenticated.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Events;
4+
5+
use Adldap\Models\User;
6+
7+
class Authenticated
8+
{
9+
/**
10+
* The LDAP user that has successfully authenticated.
11+
*
12+
* @var User
13+
*/
14+
public $user;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param User $user
20+
*/
21+
public function __construct(User $user)
22+
{
23+
$this->user = $user;
24+
}
25+
}

src/Events/Authenticating.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Events;
4+
5+
use Adldap\Models\User;
6+
7+
class Authenticating
8+
{
9+
/**
10+
* The LDAP user that is authenticating.
11+
*
12+
* @var User
13+
*/
14+
public $user;
15+
16+
/**
17+
* The username that is being authenticated.
18+
*
19+
* @var string
20+
*/
21+
public $username = '';
22+
23+
/**
24+
* Constructor.
25+
*
26+
* @param User $user
27+
* @param string $username
28+
*/
29+
public function __construct(User $user, $username = '')
30+
{
31+
$this->user = $user;
32+
$this->username = $username;
33+
}
34+
}

src/Events/AuthenticationFailed.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Events;
4+
5+
use Adldap\Models\User;
6+
7+
class AuthenticationFailed
8+
{
9+
/**
10+
* The user that failed authentication.
11+
*
12+
* @var User
13+
*/
14+
public $user;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param User $user
20+
*/
21+
public function __construct(User $user)
22+
{
23+
$this->user = $user;
24+
}
25+
}

src/Events/AuthenticationRejected.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Events;
4+
5+
use Adldap\Models\User;
6+
7+
class AuthenticationRejected
8+
{
9+
/**
10+
* The user that has been denied authentication.
11+
*
12+
* @var User
13+
*/
14+
public $user;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param User $user
20+
*/
21+
public function __construct(User $user)
22+
{
23+
$this->user = $user;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Events;
4+
5+
use Adldap\Models\User;
6+
7+
class AuthenticationSuccessful
8+
{
9+
/**
10+
* The LDAP user that has successfully authenticated.
11+
*
12+
* @var User
13+
*/
14+
public $user;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param User $user
20+
*/
21+
public function __construct(User $user)
22+
{
23+
$this->user = $user;
24+
}
25+
}

src/Listeners/LogAuthenticated.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Listeners;
4+
5+
use Adldap\Laravel\Events\Authenticated;
6+
7+
class LogAuthenticated
8+
{
9+
/**
10+
* Handle the event.
11+
*
12+
* @param Authenticated $event
13+
*
14+
* @return void
15+
*/
16+
public function handle(Authenticated $event)
17+
{
18+
info("User '{$event->user->getCommonName()}' has successfully passed LDAP authentication.");
19+
}
20+
}

src/Listeners/LogAuthentication.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Listeners;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Adldap\Laravel\Events\Authenticating;
7+
8+
class LogAuthentication
9+
{
10+
/**
11+
* Handle the event.
12+
*
13+
* @param Authenticating $event
14+
*
15+
* @return void
16+
*/
17+
public function handle(Authenticating $event)
18+
{
19+
$username = $this->getPrefix().$event->username.$this->getSuffix();
20+
21+
info("User '{$event->user->getCommonName()}' is authenticating with username: '{$username}'");
22+
}
23+
24+
/**
25+
* Returns the account prefix that is applied to username's.
26+
*
27+
* @return string|null
28+
*/
29+
protected function getPrefix()
30+
{
31+
return Config::get("{$this->getConfigSettingsPath()}.account_prefix");
32+
}
33+
34+
/**
35+
* Returns the account suffix that is applied to username's.
36+
*
37+
* @return string|null
38+
*/
39+
protected function getSuffix()
40+
{
41+
return Config::get("{$this->getConfigSettingsPath()}.account_suffix");
42+
}
43+
44+
/**
45+
* Returns the current connections configuration path.
46+
*
47+
* @return string
48+
*/
49+
protected function getConfigSettingsPath()
50+
{
51+
$connection = Config::get('adldap_auth.connection');
52+
53+
return "adldap.$connection.connection_settings";
54+
}
55+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Listeners;
4+
5+
use Adldap\Laravel\Events\AuthenticationFailed;
6+
7+
class LogAuthenticationFailure
8+
{
9+
/**
10+
* Handle the event.
11+
*
12+
* @param AuthenticationFailed $event
13+
*
14+
* @return void
15+
*/
16+
public function handle(AuthenticationFailed $event)
17+
{
18+
info("User '{$event->user->getCommonName()}' has failed LDAP authentication.");
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Listeners;
4+
5+
use Adldap\Laravel\Events\AuthenticationRejected;
6+
7+
class LogAuthenticationRejection
8+
{
9+
/**
10+
* Constructor.
11+
*
12+
* @param AuthenticationRejected $event
13+
*/
14+
public function __construct(AuthenticationRejected $event)
15+
{
16+
info("User '{$event->user->getCommonName()}' has failed validation. They have been denied authentication.");
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Listeners;
4+
5+
use Adldap\Laravel\Events\AuthenticationSuccessful;
6+
7+
class LogAuthenticationSuccess
8+
{
9+
/**
10+
* Handle the event.
11+
*
12+
* @param AuthenticationSuccessful $event
13+
*
14+
* @return void
15+
*/
16+
public function handle(AuthenticationSuccessful $event)
17+
{
18+
info("User '{$event->user->getCommonName()}' has been successfully logged in.");
19+
}
20+
}

src/Listeners/LogCredentialAuth.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Listeners/LogDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ class LogDiscovery
1515
*/
1616
public function handle(DiscoveredWithCredentials $event)
1717
{
18-
info("User {$event->user->getCommonName()} has been successfully found for authentication.");
18+
info("User '{$event->user->getCommonName()}' has been successfully found for authentication.");
1919
}
2020
}

0 commit comments

Comments
 (0)