Skip to content

Commit

Permalink
🚀 v3.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
juliomotol committed Apr 18, 2022
2 parents 2361e89 + d88bd42 commit aba01d3
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 211 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs.dist --allow-risky=yes
args: --config=.php-cs-fixer.dist.php --allow-risky=yes

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
commit_message: 🎨 Fix styling
14 changes: 7 additions & 7 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ on: [push, pull_request]

jobs:
test:
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.0, 7.4, 7.3]
laravel: [8.*]
stability: [prefer-lowest, prefer-stable]
php: [8.0, 8.1]
laravel: [8.*, 9.*]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 9.*
testbench: 7.*

name: PHP@${{ matrix.php }} - Laravel@${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
name: PHP@${{ matrix.php }} - Laravel@${{ matrix.laravel }}

steps:
- name: Checkout code
Expand All @@ -36,6 +36,6 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
composer update --prefer-stable --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"php":"8.1.3","version":"3.7.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Middleware\/AuthTimeoutMiddleware.php":4129622174,"src\/ServiceProvider.php":927265595,"src\/Contracts\/AuthTimeout.php":1156893801,"src\/AuthTimeout.php":2527386968,"src\/Facades\/AuthTimeout.php":2318115822,"src\/Events\/AuthTimeoutEvent.php":4267931844,"tests\/AuthTimeoutMiddlewareTest.php":1363096094,"tests\/TestCase.php":2184034891,"tests\/User.php":1311257019}}
13 changes: 5 additions & 8 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->notPath('bootstrap/*')
->notPath('storage/*')
->notPath('resources/view/mail/*')
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
Expand All @@ -13,14 +10,14 @@
->ignoreDotFiles(true)
->ignoreVCS(true);

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline_array' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
Expand All @@ -31,7 +28,7 @@
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method',
'method' => 'one',
],
],
'method_argument_space' => [
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v3.1.0 (2021-04-18)

### Added

- Add `AuthTimeoutMiddleware::setRedirectTo()` method.
- Support for Laravel 9.x

### Changed

- Update syntax to PHP8.0.

### Removed

- Remove support for PHP7.4 and lower.

## v3.0.1 (2021-01-09)

### Added
Expand Down
70 changes: 21 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/juliomotol/laravel-auth-timeout.svg?style=flat-square)](https://packagist.org/packages/juliomotol/laravel-auth-timeout)
[![Total Downloads](https://img.shields.io/packagist/dt/juliomotol/laravel-auth-timeout.svg?style=flat-square)](https://packagist.org/packages/juliomotol/laravel-auth-timeout)

A small Laravel 8 package that handles Authentication Timeouts.
A small Laravel 8+ package that handles Authentication Timeouts.

When upgrading to v3, please see the [CHANGELOG.md](./CHANGELOG.md).

Expand All @@ -19,45 +19,31 @@ There are times where we want to log out a user when they haven't done and reque
'lifetime' => 15,
```

But this also affects the CSRF token and we don't want that. That is where Laravel Auth Timeout comes in.
But this affects the entirety of the session. But it doesnt have to be and that is where Laravel Auth Timeout comes in.

Laravel Auth Timeout is a small middleware package that checks if the user had made any request in a set of time. If they have reached the idle time limit, they are then logged out on their next request. Thanks to Brian Matovu's [article](http://bmatovu.com/laravel-session-timeout-auto-logout/).

## Tables of Contents

- [Installation](#installation)
- [Config](#config)
- [Content of the configuration](#content-of-the-configuration)
- [Usage](#usage)
- [Quick Start](#quick-start)
- [Custom Guards](#custom-guards)
- [AuthTimeoutEvent](#authtimeoutevent)
- [Redirection](#redirection)
- [AuthTimeout Facade](#authtimeout-facade)

## Installation

You can install the package via composer:

```sh
composer require juliomotol/laravel-auth-timeout
```

> This package uses [auto-discovery](https://laravel.com/docs/5.5/packages#package-discovery), so you don't have to do anything. It works out of the box.
## Config

If you want to make changes in the configuration you can publish the config file using:

```sh
php artisan vendor:publish --provider="JulioMotol\AuthTimeout\ServiceProvider"
```

### Content of the configuration

| Key | Default value | Description |
| ------- | ---------------------- | ----------------------------------------- |
| session | `"last_activity_time"` | The name of the session token to be used. |
| timeout | `15` | The timeout duration in minutes. |

> If you want to make changes in the configuration you can publish the config file using:
>
> ```sh
> php artisan vendor:publish --provider="JulioMotol\AuthTimeout\ServiceProvider"
> ```
## Usage
### Quick Start
Expand Down Expand Up @@ -123,39 +109,25 @@ class FooEventListener

### Redirection

To modify the redirection when a user has timed out, you'll need extend the `AuthTimeoutMiddleware` then override the `redirectTo()` method.
To modify the redirection when a user has timed out, you can use `AuthTimeoutMiddleware::setRedirectTo()` within your `AppServiceProvider` to set a redirection callback.

```php
<?php

namespace App\Http\Middleware;

use JulioMotol\AuthTimeout\Middleware\AuthTimeoutMiddleware as BaseMiddleware;

class AuthTimeoutMiddleware extends BaseMiddleware
class AppServiceProvider extends ServiceProvider
{
/**
* Get the path the user should be redirected to when they timed out.
*
* @param \Illuminate\Http\Request $request
* @param mixed $guard
*
* @return mixed
*/
protected function redirectTo($request, $guard = null)
public function boot()
{
switch($guard){
case 'web.admin':
return route('auth.admin.login');
default:
return route('auth.login');
}
AuthTimeoutMiddleware::setRedirectTo(function ($request, $guard){
switch($guard){
case 'web.admin':
return route('auth.admin.login');
default:
return route('auth.login');
}
});
}
}
```

> Don't forget to use your extended `AuthTimeoutMiddleware` in the `Kernel.php`.
### AuthTimeout Facade

This package also provides a facade with the following methods:
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
}
],
"require": {
"php": "^7.3|^8.0",
"illuminate/auth": "^8.0",
"illuminate/events": "^8.0",
"illuminate/session": "^8.0",
"illuminate/support": "^8.0"
"php": "^8.0",
"illuminate/auth": "^8.0|^9.0",
"illuminate/events": "^8.0|^9.0",
"illuminate/session": "^8.0|^9.0",
"illuminate/support": "^8.0|^9.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"orchestra/testbench": "^6.0",
"friendsofphp/php-cs-fixer": "^3.0",
"orchestra/testbench": "^6.0|^7.0",
"phpunit/phpunit": "^9.3"
},
"autoload": {
Expand Down
65 changes: 15 additions & 50 deletions src/AuthTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,37 @@

class AuthTimeout implements AuthTimeoutContract
{
/**
* The Authentication Manager.
*
* @var \Illuminate\Auth\AuthManager
*/
protected $auth;

/**
* The Event Dispatcher.
*
* @var \Illuminate\Events\Dispatcher
*/
protected $event;

/**
* The Session Manager.
*
* @var \Illuminate\Session\SessionManager
*/
protected $session;

/**
* The session key.
*
* @var string
*/
protected $session_name;

/**
* Create an AuthTimeoutMiddleware.
*
* @param \Illuminate\Auth\AuthManager $auth
* @param \Illuminate\Events\Dispatcher $event
* @param \Illuminate\Session\SessionManager $session
*/
public function __construct(AuthManager $auth, Dispatcher $event, SessionManager $session)
{
$this->auth = $auth;
$this->event = $event;
$this->session = $session;

$this->session_name = config('auth-timeout.session');
protected string $session_key;

public function __construct(
protected AuthManager $auth,
protected Dispatcher $event,
protected SessionManager $session
) {
$this->session_key = config('auth-timeout.session');
}

/**
* {@inheritdoc}
*/
public function init()
public function init(): void
{
// When no session had been set yet, we'll set one now.
if (! $this->session->get($this->session_name)) {
if (! $this->session->get($this->session_key)) {
$this->reset();
}
}

/**
* {@inheritdoc}
*/
public function check($guard = null)
public function check(string $guard = null): bool
{
// When there are no user's logged in, we'll return false.
if ($this->auth->guard($guard)->guest()) {
return false;
}

$lastActiveAt = Carbon::parse($this->session->get($this->session_name));
$lastActiveAt = Carbon::parse($this->session->get($this->session_key));
$timeoutAt = $lastActiveAt->addMinutes(config('auth-timeout.timeout'));

// Now lets check if they are still within the timeout threshold.
Expand All @@ -91,16 +56,16 @@ public function check($guard = null)

$this->auth->guard($guard)->logout();
$this->event->dispatch(new AuthTimeoutEvent($user, $guard));
$this->session->forget($this->session_name);
$this->session->forget($this->session_key);

return false;
}

/**
* {@inheritdoc}
*/
public function reset()
public function reset(): void
{
$this->session->put($this->session_name, (string)Carbon::now());
$this->session->put($this->session_key, (string)Carbon::now());
}
}
14 changes: 3 additions & 11 deletions src/Contracts/AuthTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ interface AuthTimeout
{
/**
* Initialize the timeout session when no has been set yet.
*
* @return void;
*/
public function init();
public function init(): void;

/**
* Check if a user has timed out. Returns `true` if not yet timed out and `false` otherwise.
*
* @param mixed $guard
*
* @return bool
*/
public function check($guard = null);
public function check(string $guard = null): bool;

/**
* Reset the user's timeout session.
*
* @return void
*/
public function reset();
public function reset(): void;
}
Loading

0 comments on commit aba01d3

Please sign in to comment.