Skip to content

Commit a00bcbf

Browse files
committed
Initial commit
0 parents  commit a00bcbf

32 files changed

+2410
-0
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[*]
2+
charset=utf-8
3+
end_of_line=lf
4+
insert_final_newline=false
5+
indent_style=space
6+
indent_size=4
7+
8+
[{*.yml,*.yaml}]
9+
indent_style=space
10+
indent_size=2
11+

.gitattributes

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/tests export-ignore
2+
/.github export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
5+
/.scrutinizer.yml export-ignore
6+
/.travis.yml export-ignore
7+
/.editorconfig export-ignore
8+
/phpunit.xml export-ignore
9+
/changelog.md export-ignore
10+
/README.md export-ignore
11+
/CONTRIBUTING.md export-ignore
12+
/CODE_OF_CONDUCT.md export-ignore

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
vendor/
2+
/.idea
3+
4+
# Rocketeer PHP task runner and deployment package. https://github.com/rocketeers/rocketeer
5+
.rocketeer/
6+
7+
# composer
8+
/composer.lock
9+
/composer.local.*
10+
/composer.phar
11+
12+
# coverage reports
13+
/coverage.clover
14+
coverage.xml
15+
clover.xml
16+
report/

.scrutinizer.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
build:
2+
nodes:
3+
coverage:
4+
tests:
5+
override:
6+
- command: vendor/bin/phpunit --coverage-clover=clover.xml
7+
coverage:
8+
file: clover.xml
9+
format: clover
10+
tools:
11+
php_code_sniffer:
12+
config:
13+
standard: PSR2

.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
- 7.3
7+
8+
before_script:
9+
- travis_retry composer self-update
10+
- travis_retry composer install --prefer-source --no-interaction
11+
12+
fast_finish: true

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Roberto Belotti
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Laravel Auth-log
2+
User authentication log for Laravel 5 and 6.
3+
This package allows you to log user's authentication and force its logout if necessary!
4+
5+
## System requirements
6+
7+
Set `session.driver` value
8+
> To use this package the only allowed values of `session.driver` are `file`, `database`, `redis` (at the moment).
9+
10+
## Install`
11+
12+
You can install the package via composer:
13+
```sh
14+
$ composer require biscolab/laravel-authlog
15+
```
16+
Laravel 5.5 (or greater) uses package auto-discovery, so doesn't require you to manually add the Service Provider, but if you don't use auto-discovery `AuthLogServiceProvider` must be registered in `config/app.php`:
17+
```php
18+
'providers' => [
19+
...
20+
Biscolab\LaravelAuthLog\AuthLogServiceProvider::class,
21+
];
22+
```
23+
You can use the facade for shorter code. Add `AuthLog` to your aliases:
24+
```php
25+
'aliases' => [
26+
...
27+
'AuthLog' => Biscolab\LaravelAuthLog\Facades\AuthLog::class,
28+
];
29+
```
30+
31+
## Publish package
32+
Create `config/authlog.php` configuration file using the following artisan command:
33+
```sh
34+
$ php artisan vendor:publish --provider="Biscolab\LaravelAuthLog\AuthLogServiceProvider"
35+
```
36+
37+
## Configuration
38+
39+
Edit `config/authlog.php`
40+
41+
| Variable name | Type | Description | Default value |
42+
|----------------|-----------------|---------------------|-----------------|
43+
| `safe_mode` | `bool` | Avoid to force logout by yourself | `true` |
44+
| `enabled` | `bool` | If `true` the package is active and user's authentication will be logged | `true` |
45+
| `skip_ip` | `string` | A whitelist of IP addresses (CSV format) that, if recognized, disable the package | `''` |
46+
| `table_name` | `string` | The name of the AuthLog database table | `authlog` |
47+
| `authlog_model` | `string` | AuthLog class. You can change ìt **BUT** your custom class **MUST** implements `'Biscolab\LaravelAuthLog\Models\AuthLogInterface'` | `'Biscolab\LaravelAuthLog\Models\AuthLog'` |
48+
| `session_model` | `string` | Session class. You can change ìt **BUT** your custom class **MUST** implements `'Biscolab\LaravelAuthLog\Models\SessionInterface'` | `'Biscolab\LaravelAuthLog\Models\Session'` |
49+
| `session_auth_log_id_key` | `string` | Session key used to store your AuthLog ID | `'auth_log_id'` |
50+
| `add_auth_log_id_to_ajax_response` | `bool` | If `true` AuthLog ID will be added to your AJAX responses | `true` |
51+
| `ajax_response_auth_log_id_name` | `string` | AJAX response key used to send your AuthLog ID | `'auth_log_id'` |
52+
| `add_auth_log_id_header_to_http_response` | `bool` | If `true` AuthLog ID will be added to your response headers | `true` |
53+
| `auth_log_id_header_nameauth_log_id_header_name` | `string` | AuthLog ID header name | `'X-Auth-Log-Id'` |
54+
55+
> Remember to run the `php artisan config:cache` command
56+
57+
## Database
58+
59+
Run migrations
60+
61+
```sh
62+
php artisan migrate
63+
```
64+
65+
AuthLog database table will be created.
66+
67+
68+
## Middleware
69+
70+
### Register `AuthLogMiddleware
71+
Register `AuthLogMiddleware` in `app/Http/Kernel.php`. This middleware will handle user authentication session ID.
72+
73+
```php
74+
75+
protected $routeMiddleware = [
76+
...
77+
'auth.log' => \Biscolab\LaravelAuthLog\Middleware\AuthLogMiddleware::class
78+
];
79+
80+
````
81+
82+
### Add `AuthLogMiddleware` to routes
83+
84+
```php
85+
Route::group(['middleware' => ['auth.log']], function() {
86+
87+
// Your routes
88+
});
89+
```
90+
91+
## Handle logged users
92+
### Artisan Command
93+
94+
To handle auth sessions type the following artisan command
95+
96+
```sh
97+
php artisan authlog:logged
98+
```
99+
100+
The list of logged users will be shown
101+
102+
```sh
103+
+--------+------------------------------------------+-----------------------+---------------------+
104+
| Log ID | Session ID | User | Logged @ |
105+
+--------+------------------------------------------+-----------------------+---------------------+
106+
| 604 | teq4LmVM4u4sdhFTKnGsKeWs3IBOLAIOXB1c4ioy | Roberto Belotti (#22) | 2019-09-25 22:56:33 |
107+
+--------+------------------------------------------+-----------------------+---------------------+
108+
109+
Type Log ID to kill session. Type "exit" to quit:
110+
```
111+
112+
Now you can either quit typing `exit` or force user logout typing the specific Log ID, in this case `604`.
113+
114+
```
115+
> 604
116+
117+
Session "teq4LmVM4u4sdhFTKnGsKeWs3IBOLAIOXB1c4ioy" deleted. "Roberto Belotti" user logged out
118+
No logged user, please type "exit" to quit
119+
```

composer.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "biscolab/laravel-authlog",
3+
"description": "User's authentication log package for Laravel 5 and 6",
4+
"version": "0.0.1",
5+
"license": "MIT",
6+
"type": "library",
7+
"keywords": [
8+
"auth",
9+
"laravel"
10+
],
11+
"homepage": "https://biscolab.com/laravel-authlog",
12+
"authors": [
13+
{
14+
"name": "Roberto Belotti",
15+
"email": "[email protected]",
16+
"homepage": "https://biscolab.com",
17+
"role": "Developer"
18+
}
19+
],
20+
"require": {
21+
"php": "^7.1",
22+
"laravel/framework": "^5.5|^6.0",
23+
"illuminate/support": "^5.5|^6.0"
24+
},
25+
"require-dev": {
26+
"orchestra/testbench": "~3.0",
27+
"phpunit/phpunit": "6.*|7.*"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Biscolab\\LaravelAuthLog\\": "src/"
32+
},
33+
"files": [
34+
"src/helpers.php"
35+
]
36+
},
37+
"autoload-dev": {
38+
"psr-4": {
39+
"Biscolab\\LaravelAuthLog\\Tests\\": "tests/"
40+
}
41+
},
42+
"scripts": {
43+
"test": "vendor/bin/phpunit --colors=always"
44+
},
45+
"extra": {
46+
"laravel": {
47+
"providers": [
48+
"Biscolab\\LaravelAuthLog\\LaravelAuthLogServiceProvider"
49+
],
50+
"aliases": {
51+
"AuthLog": "Biscolab\\LaravelAuthLog\\Facades\\AuthLog"
52+
}
53+
}
54+
}
55+
}

config/authlog.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2019 - present
4+
* Laravel Auth Log - authlog.php
5+
* author: Roberto Belotti - [email protected]
6+
* web : robertobelotti.com, github.com/biscolab
7+
* Initial version created on: 13/9/2019
8+
* MIT license: https://github.com/biscolab/laravel-authlog/blob/master/LICENSE
9+
*/
10+
11+
return [
12+
13+
/**
14+
* If "true" it avoids to "kill" your own session
15+
*/
16+
'safe_mode' => env('AUTHLOG_SAFE_MODE', true),
17+
18+
/**
19+
* If "true" Auth Log package is enabled
20+
*/
21+
'enabled' => env('AUTHLOG_ENABLED', true),
22+
23+
/**
24+
* Coming connections from following IP(s) won't be recorded (CSV format)
25+
*/
26+
'skip_ip' => env('AUTHLOG_SKIP_IP', null),
27+
28+
/**
29+
* AuthLog model table name
30+
*/
31+
'table_name' => 'authlog',
32+
33+
/**
34+
* AuthLog model class MUST implements Biscolab\LaravelAuthLog\Models\AuthLogInterface
35+
*/
36+
'authlog_model' => 'Biscolab\LaravelAuthLog\Models\AuthLog',
37+
38+
/**
39+
* Session model class MUST implements Biscolab\LaravelAuthLog\Models\SessionInterface
40+
*/
41+
'session_model' => 'Biscolab\LaravelAuthLog\Models\Session',
42+
43+
/**
44+
* Session key used to store your AuthLog ID
45+
*/
46+
'session_auth_log_id_key' => 'auth_log_id',
47+
48+
/**
49+
* If "true" AuthLog ID will be added to your AJAX responses
50+
*/
51+
'add_auth_log_id_to_ajax_response' => true,
52+
53+
/**
54+
* AJAX response key used to send your AuthLog ID
55+
*/
56+
'ajax_response_auth_log_id_name' => 'auth_log_id',
57+
58+
/**
59+
* If "true" AuthLog ID will be added to your response headers
60+
*/
61+
'add_auth_log_id_header_to_http_response' => true,
62+
63+
/**
64+
* AuthLog ID header name
65+
*/
66+
'auth_log_id_header_name' => 'X-Auth-Log-Id',
67+
68+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
/**
8+
* Class CreateAuthlogTable
9+
*/
10+
class CreateAuthlogTable extends Migration
11+
{
12+
13+
/**
14+
* Run the migrations.
15+
*
16+
* @return void
17+
*/
18+
public function up()
19+
{
20+
21+
Schema::create(config('authlog.table_name'), function (Blueprint $table) {
22+
23+
$table->increments('id');
24+
$table->unsignedInteger('user_id');
25+
$table->unsignedInteger('blame_on_user_id')->nullable();
26+
$table->string('ip', 255)->nullable()->default('');
27+
$table->string('session_id', 255)->nullable()->default('');
28+
$table->text('user_agent')->nullable();
29+
$table->boolean('killed_from_console')->default(false);
30+
$table->dateTime('logged_out_at')->nullable();
31+
$table->dateTime('created_at');
32+
$table->dateTime('updated_at');
33+
34+
$table->index('session_id', 'session_id');
35+
$table->index('user_id', 'user_id');
36+
$table->index('blame_on_user_id', 'blame_on_user_id');
37+
$table->index(['logged_out_at', 'blame_on_user_id', 'killed_from_console'], 'logged_out_at');
38+
39+
$table->foreign('blame_on_user_id', 'fk_authlog_blame_on_user')->references('id')->on('users')->onDelete('RESTRICT
40+
')->onUpdate('RESTRICT');
41+
$table->foreign('user_id', 'fk_authlog_user')->references('id')->on('users')->onDelete('RESTRICT
42+
')->onUpdate('RESTRICT');
43+
44+
});
45+
46+
}
47+
48+
/**
49+
* Reverse the migrations.
50+
*
51+
* @return void
52+
*/
53+
public function down()
54+
{
55+
56+
Schema::dropIfExists(config('authlog.table_name'));
57+
}
58+
}

0 commit comments

Comments
 (0)