Skip to content

Commit

Permalink
🔖 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MarceauKa committed May 19, 2024
1 parent 8c236c7 commit 35bf482
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.phpunit.cache
/.idea
/.vscode
/vendor
Expand Down
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: php
php:
- 8.1
- 8.0
- 7.4
- 7.3
- 8.3
- 8.2
before_script: composer install
script: vendor/bin/phpunit -c phpunit.xml
script: vendor/bin/phpunit -c phpunit.xml
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 3.0.0

- Laravel 11 support [#34](https://github.com/404labfr/laravel-auth-checker/pull/34), thanks to [gboquizosanchez](https://github.com/gboquizosanchez)
- Bump minimal Laravel version to 11
- Bump minimal PHP version to 8.2
- Use `#[Test]` annotation in tests instead of `@test`

## 2.0.0

- Bump minimal Laravel version to 8.x
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"jenssegers/agent": "^2.6"
},
"require-dev": {
"phpunit/phpunit": "^11.0.1",
"phpunit/phpunit": "^11.0",
"orchestra/testbench": "^9.0"
},
"license": "MIT",
Expand Down
110 changes: 43 additions & 67 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,69 @@

- [Requirements](#requirements)
- [Installation](#installation)
- [Access Collected Data](#access-collected-data)
- [Logins](#logins)
- [Devices](#devices)
- [Events](#events)
- [Practical usage](#practical-usage)
- [Usage](#usage)
- [Authenticatable model](#authenticatable-model)
- [Logins](#logins)
- [Devices](#devices)
- [Events](#events)
- [Tests](#tests)
- [Contributors](#contributors)
- [Licence](#licence)


## Requirements
### Requirements

- Laravel 9 to 10
- PHP 8.0 to 8.2
| Version | Release |
|:--------:|:------------------------------------------------------------------:|
| 11 | [3.0](https://github.com/404labfr/laravel-auth-checker/tree/3.0.0) |
| 9, 10 | [2.0](https://github.com/404labfr/laravel-auth-checker/tree/2.0.0) |
| 8, 9 | [1.7](https://github.com/404labfr/laravel-auth-checker/tree/1.7.0) |
| 6, 7 | [1.6](https://github.com/404labfr/laravel-auth-checker/tree/1.6.2) |

### Laravel support
## Installation

| Version | Release |
|:--------:|:-------:|
| 9, 10 | 2.0 |
| 8, 9 | 1.7 |
| 6, 7 | 1.6 |
| 5.8 | 1.2 |
| 5.7, 5.6 | 1.1 |
* Require the package: `composer require lab404/laravel-auth-checker`
* Publish migration files: `php artisan vendor:publish --tag=auth-checker`
* Migrate your database: `php artisan migrate`
* Configure your `Authenticatable` model (see below)

## Installation
## Usage

- Require it with Composer:
This library collects login data and devices data about your users.

```bash
composer require lab404/laravel-auth-checker
```
### Authenticatable model

Your `Authenticatable` model (usually `User`) must implement the `HasLoginsAndDevicesInterface` interface.

- Add to your **User** model the `Lab404\AuthChecker\Models\HasLoginsAndDevices` trait and the `Lab404\AuthChecker\Interfaces\HasLoginsAndDevicesInterface` interface.
The trait `HasLoginsAndDevices` is provided with for a working default implementation.

```php
use Lab404\AuthChecker\Models\HasLoginsAndDevices;
use Lab404\AuthChecker\Interfaces\HasLoginsAndDevicesInterface;

class User extends Authenticatable implements HasLoginsAndDevicesInterface
{
use Notifiable, HasLoginsAndDevices;
// ...
use HasLoginsAndDevices;
}
```

- Publish migrations and migrate your database:
Once configured, you can access the following methods

```bash
php artisan vendor:publish --tag=auth-checker
php artisan migrate
```

Note: Migrations are published in case you need to customize migration timestamps to integrate to your existing project.
- `logins()` returns all logins
- `auths()` returns all successful login attemps
- `fails()` returns all failed login attempts
- `lockouts()` returns all lockouts

## Access collected data
Each login returned is associated with the `Device` model used

This library collects login data and devices data about your users.
- `devices()` returns all devices used by the user to authenticate.

### Logins

Calling `$user->logins` outputs:

```php
// Your user model:
$logins = $user->logins;
// Output:
[
[
'ip_address' => '1.2.3.4',
Expand All @@ -81,21 +81,21 @@ $logins = $user->logins;
],
'created_at' => '2017-03-25 11:42:00',
],
// ... and more
// ...
]
```

Also, you can directly access logins by their type:
Also, you can directly access logins by their type

- `$user->auths`, returns successful logins (via `Login::TYPE_LOGIN`)
- `$user->fails`, returns failed logins (via `Login::TYPE_FAILED`)
- `$user->lockouts`, returns locked out logins (via `Login::TYPE_LOCKOUT`)

### Devices

Calling `$user->devices` outputs:

```php
// Your user model:
$devices = $user->devices;
// Outputs:
[
[
'platform' => 'OS X',
Expand All @@ -109,45 +109,21 @@ $devices = $user->devices;
// See logins
],
],
// ... and more
// ...
]
```

## Roadmap

- [x] Log user authentication
- [x] Collect IP addresses
- [x] Collect devices
- [x] Get user's login history
- [x] Get devices history
- [x] Capture failed logins
- [x] Capture lockout logins
- [ ] Trust / Untrust devices
- [ ] Notify user when an unknown device log in
### Events

## Events
There are many events available that can be used to add features to your app

There are many events available that can be used to add features to your app:
- `LoginCreated` is fired when a user authenticates.
- `DeviceCreated` is fired when a new device is created for a user.
- `FailedAuth` is fired when a user fails to log in.
- `LockoutAuth` is fired when authentication is locked for a user (too many attempts).

Each event passes a `Login` model and a `Device` model to your listeners.

## Practical usage

Once the trait `HasLoginsAndDevices` is added to your `User` model, it is extended with these methods:

- `logins()` returns all logins
- `auths()` returns all successful login attemps
- `fails()` returns all failed login attempts
- `lockouts()` returns all lockouts

Each login returned is associated with the `Device` model used.

- `devices()` returns all devices used by the user to authenticate.

## Tests

```bash
Expand Down
11 changes: 6 additions & 5 deletions tests/AuthCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Lab404\AuthChecker\Models\Device;
use Lab404\AuthChecker\Models\Login;
use Lab404\AuthChecker\Services\AuthChecker;
use PHPUnit\Framework\Attributes\Test;

class AuthCheckerTest extends TestCase
{
Expand All @@ -27,7 +28,7 @@ public function setUp(): void
$this->agent = $this->app->make('agent');
}

/** @test */
#[Test]
public function it_can_be_accessed_from_container()
{
$this->assertInstanceOf(AuthChecker::class, $this->manager);
Expand All @@ -40,7 +41,7 @@ public function it_can_be_accessed_from_container()
$this->assertEquals(0, $this->manager->getLoginThrottleConfig());
}

/** @test */
#[Test]
public function it_matches_device_when_attributes_are_empty()
{
$device = new Device(['platform' => 'OS X', 'platform_version' => '10_12_2', 'browser' => 'Chrome']);
Expand All @@ -49,7 +50,7 @@ public function it_matches_device_when_attributes_are_empty()
$this->assertTrue($result);
}

/** @test */
#[Test]
public function it_matches_device_with_attributes()
{
$device = new Device(['platform' => 'OS X', 'platform_version' => '10_12_0', 'browser' => 'Chrome']);
Expand All @@ -73,7 +74,7 @@ public function it_matches_device_with_attributes()
$this->assertTrue($result);
}

/** @test */
#[Test]
public function it_should_log_device_login()
{
$device = new Device();
Expand All @@ -100,7 +101,7 @@ public function it_should_log_device_login()
$this->assertTrue($result);
}

/** @test */
#[Test]
public function it_should_not_log_device_login()
{
$device = new Device();
Expand Down
13 changes: 7 additions & 6 deletions tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Lab404\AuthChecker\Events\LockoutAuth;
use Lab404\AuthChecker\Services\AuthChecker;
use Lab404\Tests\Stubs\Models\User;
use PHPUnit\Framework\Attributes\Test;

class EventsTest extends TestCase
{
Expand All @@ -29,13 +30,13 @@ public function setUp(): void
$this->dispatcher = $this->app['events'];
}

/** @test */
#[Test]
public function it_registers_listeners()
{
$this->assertTrue($this->dispatcher->hasListeners(Login::class));
}

/** @test */
#[Test]
public function it_registers_login_and_device_on_new_autentication()
{
/** @var Agent $agent */
Expand All @@ -56,7 +57,7 @@ public function it_registers_login_and_device_on_new_autentication()
$this->assertEquals('Chrome', $device->browser);
}

/** @test */
#[Test]
public function it_creates_failed_login()
{
$user = User::first();
Expand All @@ -67,7 +68,7 @@ public function it_creates_failed_login()
$this->assertEquals(1, $user->logins()->count());
}

/** @test */
#[Test]
public function it_creates_lockouts_login()
{
$user = User::first();
Expand All @@ -80,7 +81,7 @@ public function it_creates_lockouts_login()
$this->assertEquals(1, $user->logins()->count());
}

/** @test */
#[Test]
public function it_fires_failed_auth_event()
{
Event::fake();
Expand All @@ -94,7 +95,7 @@ public function it_fires_failed_auth_event()
});
}

/** @test */
#[Test]
public function it_fires_lockout_auth_event()
{
Event::fake();
Expand Down
11 changes: 6 additions & 5 deletions tests/HasLoginsAndDevicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Lab404\Tests\Stubs\Models\CustomDevice;
use Lab404\Tests\Stubs\Models\CustomLogin;
use Lab404\Tests\Stubs\Models\User;
use PHPUnit\Framework\Attributes\Test;

class HasLoginsAndDevicesTest extends TestCase
{
Expand All @@ -25,7 +26,7 @@ public function setUp(): void
$this->config = $this->app->make('config');
}

/** @test */
#[Test]
public function it_can_access_logins()
{
$user = User::first();
Expand All @@ -43,7 +44,7 @@ public function it_can_access_logins()
$this->assertEquals(1, $user->lockouts()->count());
}

/** @test */
#[Test]
public function it_can_access_logins_with_custom_models()
{
$this->config->set('auth-checker.models.login', CustomLogin::class);
Expand All @@ -63,7 +64,7 @@ public function it_can_access_logins_with_custom_models()
$this->config->set('auth-checker.models.login', null);
}

/** @test */
#[Test]
public function it_can_access_devices()
{
$user = User::first();
Expand All @@ -76,7 +77,7 @@ public function it_can_access_devices()
$this->assertEquals(3, $user->devices()->count());
}

/** @test */
#[Test]
public function it_can_access_devices_with_custom_models()
{
$this->config->set('auth-checker.models.device', CustomDevice::class);
Expand All @@ -92,7 +93,7 @@ public function it_can_access_devices_with_custom_models()
$this->config->set('auth-checker.models.device', null);
}

/** @test */
#[Test]
public function it_detects_devices()
{
$user = User::first();
Expand Down

0 comments on commit 35bf482

Please sign in to comment.