Skip to content

Commit

Permalink
added ip address to login events
Browse files Browse the repository at this point in the history
  • Loading branch information
SPie committed Sep 4, 2019
1 parent f635033 commit 317817a
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/Auth/JWTGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,13 @@ public function issueAccessToken(JWTAuthenticatable $user): JWT
*/
public function login(array $credentials = []): JWTGuardContract
{
$this->dispatchEvent(new LoginAttempt($credentials));
$this->dispatchEvent(new LoginAttempt($credentials, $this->getRequest()->ip()));

$user = $this->getProvider()->retrieveByCredentials($credentials);

if ($this->isInvalidUser($user, $credentials)) {
$this
->dispatchEvent(new FailedLoginAttempt($credentials))
->dispatchEvent(new FailedLoginAttempt($credentials, $this->getRequest()->ip()))
->setAccessToken(null)
->user = null;

Expand All @@ -439,7 +439,7 @@ public function login(array $credentials = []): JWTGuardContract
->setAccessToken($this->issueAccessToken($user))
->setUser($user);

$this->dispatchEvent(new Login($this->user(), $this->getAccessToken(), $credentials));
$this->dispatchEvent(new Login($this->user(), $this->getAccessToken(), $this->getRequest()->ip()));

return $this;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Events/FailedLoginAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*
* @package SPie\LaravelJWT\Events
*/
final class FailedLoginAttempt implements Event
final class FailedLoginAttempt implements Event, IpAddressable
{
use IpAddress;

/**
* @var array
Expand All @@ -18,11 +19,13 @@ final class FailedLoginAttempt implements Event
/**
* FailedLoginAttempt constructor.
*
* @param array $credentials
* @param array $credentials
* @param string|null $ipAddress
*/
public function __construct(array $credentials)
public function __construct(array $credentials, string $ipAddress = null)
{
$this->credentials = $credentials;
$this->ipAddress = $ipAddress;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/Events/IpAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace SPie\LaravelJWT\Events;

/**
* Trait IpAddress
*
* @package SPie\LaravelJWT\Events
*/
trait IpAddress
{
/**
* @var string|null
*/
private $ipAddress;

/**
* @return string|null
*/
public function getIpAddress(): ?string
{
return $this->ipAddress;
}
}
16 changes: 16 additions & 0 deletions src/Events/IpAddressable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SPie\LaravelJWT\Events;

/**
* Interface IpAddressable
*
* @package SPie\LaravelJWT\Events
*/
interface IpAddressable
{
/**
* @return string|null
*/
public function getIpAddress(): ?string;
}
22 changes: 5 additions & 17 deletions src/Events/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
*
* @package SPie\LaravelJWT\Events
*/
final class Login implements Event
final class Login implements Event, IpAddressable
{
use IpAddress;

/**
* @var JWTAuthenticatable
Expand All @@ -23,23 +24,18 @@ final class Login implements Event
*/
private $accessToken;

/**
* @var array
*/
private $credentials;

/**
* LoginEvent constructor.
*
* @param JWTAuthenticatable $user
* @param JWT $accessToken
* @param array $credentials
* @param string|null $ipAddress
*/
public function __construct(JWTAuthenticatable $user, JWT $accessToken, array $credentials = [])
public function __construct(JWTAuthenticatable $user, JWT $accessToken, string $ipAddress = null)
{
$this->user = $user;
$this->accessToken = $accessToken;
$this->credentials = $credentials;
$this->ipAddress = $ipAddress;
}

/**
Expand All @@ -57,12 +53,4 @@ public function getAccessToken(): JWT
{
return $this->accessToken;
}

/**
* @return array
*/
public function getCredentials(): array
{
return $this->credentials;
}
}
9 changes: 6 additions & 3 deletions src/Events/LoginAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*
* @package SPie\LaravelJWT\Events
*/
final class LoginAttempt implements Event
final class LoginAttempt implements Event, IpAddressable
{
use IpAddress;

/**
* @var array
Expand All @@ -18,11 +19,13 @@ final class LoginAttempt implements Event
/**
* LoginAttempt constructor.
*
* @param array $credentials
* @param array $credentials
* @param string|null $ipAddress
*/
public function __construct(array $credentials)
public function __construct(array $credentials, string $ipAddress = null)
{
$this->credentials = $credentials;
$this->ipAddress = $ipAddress;
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tests/RequestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace SPie\LaravelJWT\Test;

use Illuminate\Http\Request;
use Mockery as m;
use Mockery\MockInterface;

/**
* Trait RequestHelper
*
* @package SPie\LaravelJWT\Test
*/
trait RequestHelper
{
//region Mocks

/**
* @return Request|MockInterface
*/
private function createRequest(): Request
{
return m::mock(Request::class)
->shouldAllowMockingProtectedMethods()
->makePartial();
}

/**
* @param Request|MockInterface $request
* @param string|null $ipAddress
*
* @return $this
*/
private function mockRequestIp(MockInterface $request, ?string $ipAddress)
{
$request
->shouldReceive('ip')
->andReturn($ipAddress);

return $this;
}

//endregion
}
18 changes: 15 additions & 3 deletions tests/Unit/FailedLoginAttemptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@ final class FailedLoginAttemptTest extends TestCase
*/
public function testConstruct(): void
{
$credentials = [
$this->getFaker()->uuid => $this->getFaker()->uuid,
];
$credentials = [$this->getFaker()->uuid => $this->getFaker()->uuid];
$ipAddress = $this->getFaker()->ipv4;

$failedLoginAttempt = new FailedLoginAttempt($credentials, $ipAddress);

$this->assertEquals($credentials, $failedLoginAttempt->getCredentials());
$this->assertEquals($ipAddress, $failedLoginAttempt->getIpAddress());
}

/**
* @return void
*/
public function testConstructWithoutOptionalParameters(): void
{
$credentials = [$this->getFaker()->uuid => $this->getFaker()->uuid];

$this->assertEquals($credentials, (new FailedLoginAttempt($credentials))->getCredentials());
}
Expand Down
Loading

0 comments on commit 317817a

Please sign in to comment.