Skip to content

Commit

Permalink
raise default auth events
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Kaufmann committed Nov 10, 2021
1 parent 76d84d3 commit 85d452b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/JWTGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

use BadMethodCallException;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Auth\Events\Validated;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Authenticatable;
Expand Down Expand Up @@ -167,6 +170,11 @@ public function login(JWTSubject $user)
$token = $this->jwt->fromUser($user);
$this->setToken($token)->setUser($user);

// If we have an event dispatcher instance set we will fire an event so that
// any listeners will hook into the authentication events and run actions
// based on the login and logout events fired from the guard instances.
$this->fireLoginEvent($user);

return $token;
}

Expand All @@ -181,8 +189,15 @@ public function logout($forceForever = false)
{
$this->requireToken()->invalidate($forceForever);

// If we have an event dispatcher instance, we can fire off the logout event
// so any further processing can be done. This allows the developer to be
// listening for anytime a user signs out of this application manually.
$this->events->dispatch(new Logout($this->name, $this->user));

$this->user = null;
$this->jwt->unsetToken();


}

/**
Expand Down Expand Up @@ -233,6 +248,8 @@ public function tokenById($id)
*/
public function once(array $credentials = [])
{
$this->fireAttemptEvent($credentials);

if ($this->validate($credentials)) {
$this->setUser($this->lastAttempted);

Expand Down Expand Up @@ -368,6 +385,21 @@ public function getUser()
return $this->user;
}

/**
* Set the current user.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return $this
*/
public function setUser(Authenticatable $user)
{
$this->user = $user;

$this->fireAuthenticatedEvent($user);

return $this;
}

/**
* Get the current request instance.
*
Expand Down Expand Up @@ -501,6 +533,33 @@ protected function fireFailedEvent($user, array $credentials)
));
}

/**
* Fire the authenticated event if the dispatcher is set.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
protected function fireAuthenticatedEvent($user)
{
$this->events->dispatch(new Authenticated(
$this->name, $user
));
}

/**
* Fire the login event if the dispatcher is set.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param bool $remember
* @return void
*/
protected function fireLoginEvent($user, $remember = false)
{
$this->events->dispatch(new Login(
$this->name, $user, $remember
));
}

/**
* Magically call the JWT instance.
*
Expand Down
53 changes: 52 additions & 1 deletion tests/JWTGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Auth\Events\Validated;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Auth\UserProvider;
Expand Down Expand Up @@ -229,6 +232,14 @@ public function it_should_return_a_token_if_credentials_are_ok_and_user_is_found
->once()
->with(Mockery::type(Validated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->with(Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->with(Mockery::type(Login::class));

$token = $this->guard->claims(['foo' => 'bar'])->attempt($credentials);

$this->assertSame($this->guard->getLastAttempted(), $user);
Expand Down Expand Up @@ -259,6 +270,14 @@ public function it_should_return_true_if_credentials_are_ok_and_user_is_found_wh
->twice()
->with(Mockery::type(Validated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Login::class));

$this->assertTrue($this->guard->attempt($credentials, false)); // once
$this->assertTrue($this->guard->validate($credentials)); // twice
}
Expand Down Expand Up @@ -287,6 +306,14 @@ public function it_should_return_false_if_credentials_are_invalid()
->once()
->with(Mockery::type(Failed::class));

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Login::class));

$this->assertFalse($this->guard->attempt($credentials));
}

Expand All @@ -305,6 +332,14 @@ public function it_should_logout_the_user_by_invalidating_the_token()
$this->jwt->shouldReceive('invalidate')->once()->andReturn(true);
$this->jwt->shouldReceive('unsetToken')->once();

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->with(Mockery::type(Logout::class));

$this->guard->logout();
$this->assertNull($this->guard->getUser());
}
Expand Down Expand Up @@ -357,6 +392,14 @@ public function it_should_generate_a_token_by_id()
->with($user)
->andReturn('foo.bar.baz');

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Login::class));

$this->assertSame('foo.bar.baz', $this->guard->tokenById(1));
}

Expand Down Expand Up @@ -388,13 +431,21 @@ public function it_should_authenticate_the_user_by_credentials_and_return_true_i
->andReturn(true);

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->twice()
->with(Mockery::type(Attempting::class));

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->with(Mockery::type(Validated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->with(Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(Mockery::type(Login::class));

$this->assertTrue($this->guard->once($credentials));
}

Expand Down

0 comments on commit 85d452b

Please sign in to comment.