Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
juliomotol committed Jun 3, 2022
2 parents aba01d3 + 322ee87 commit 3f53a12
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ build
composer.lock
coverage
phpunit.xml
vendor
vendor
.php-cs-fixer.cache
1 change: 0 additions & 1 deletion .php-cs-fixer.cache

This file was deleted.

22 changes: 17 additions & 5 deletions src/AuthTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
*/
public function init(): void
{
if (! $this->session->get($this->session_key)) {
if (! $this->lastActiveAt()) {
$this->reset();
}
}
Expand All @@ -41,11 +41,11 @@ public function check(string $guard = null): bool
return false;
}

$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.
if ($timeoutAt->greaterThan(Carbon::now())) {
if ($this->lastActiveAt()
->addMinutes(config('auth-timeout.timeout'))
->greaterThan(Carbon::now())
) {
return true;
}

Expand All @@ -68,4 +68,16 @@ public function reset(): void
{
$this->session->put($this->session_key, (string)Carbon::now());
}

public function lastActiveAt(): ?Carbon
{
if ($lastActivity = $this->session->get($this->session_key)) {
// In v2, `$lastActivity` was stored as `int` using `time`. To preseve compatibility
// with v3, lets first check if it is numeric then parse it back to `int` just in case
// Laravel's session store messes with its type.
return Carbon::parse(is_numeric($lastActivity) ? (int)$lastActivity : $lastActivity);
}

return null;
}
}
7 changes: 7 additions & 0 deletions src/Contracts/AuthTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace JulioMotol\AuthTimeout\Contracts;

use Carbon\Carbon;

interface AuthTimeout
{
/**
Expand All @@ -18,4 +20,9 @@ public function check(string $guard = null): bool;
* Reset the user's timeout session.
*/
public function reset(): void;

/**
* Get the last active session time.
*/
public function lastActiveAt(): ?Carbon;
}
1 change: 1 addition & 0 deletions src/Facades/AuthTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @method static void init()
* @method static bool check($guard = null)
* @method static void reset()
* @method static ?Carbon\Carbon lastActiveAt()
*
* @see \JulioMotol\AuthTimeout\AuthTimeout
*/
Expand Down
22 changes: 18 additions & 4 deletions tests/AuthTimeoutMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Http\Request;
use Illuminate\Session\SessionManager;
use JulioMotol\AuthTimeout\Events\AuthTimeoutEvent;
use JulioMotol\AuthTimeout\Facades\AuthTimeout;
use JulioMotol\AuthTimeout\Middleware\AuthTimeoutMiddleware;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

Expand Down Expand Up @@ -51,13 +52,13 @@ public function should_assign_session_for_new_login()
$this->hasAuth();
$this->runMiddleware();

$this->assertEquals((string)Carbon::now(), $this->session->get(config('auth-timeout.session')));
$this->assertNotNull(AuthTimeout::lastActiveAt());
}

/** @test */
public function should_reset_session_when_not_timedout()
{
$init_time = time();
$startTime = Carbon::now();

$this->hasAuth();
$this->runMiddleware();
Expand All @@ -66,7 +67,7 @@ public function should_reset_session_when_not_timedout()

$this->runMiddleware();

$this->assertNotEquals($init_time, $this->session->get(config('auth-timeout.session')));
$this->assertNotEquals($startTime, AuthTimeout::lastActiveAt());
}

/** @test */
Expand All @@ -83,7 +84,7 @@ public function should_timeout_when_idled()

$this->expectsEvents(AuthTimeoutEvent::class);
$this->assertNull($this->auth->user());
$this->assertNull($this->session->get(config('auth-timeout.session')));
$this->assertNull(AuthTimeout::lastActiveAt());
}

/** @test */
Expand Down Expand Up @@ -111,6 +112,19 @@ public function can_modify_redirection()
}
}

/** @test */
public function is_backwards_compatible_with_v2()
{
$this->session->put(config('auth-timeout.session'), time());

$this->hasAuth();
$this->runMiddleware();

$this->travel(config('auth-timeout.timeout') - 1)->minutes();

$this->runMiddleware();
}

private function hasAuth()
{
$user = new User(['name' => 'Unit Test User']);
Expand Down

0 comments on commit 3f53a12

Please sign in to comment.