Skip to content

Commit 9526c2c

Browse files
Add optional last_used_at tracking configuration (#583)
* Add optional last_used_at tracking configuration * Update sanctum.php * Change config key for last used timestamp tracking --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 3758c89 commit 9526c2c

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

src/Guard.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,27 @@ class Guard
3030
*/
3131
protected $provider;
3232

33+
/**
34+
* Whether to track the last used timestamp.
35+
*
36+
* @var bool
37+
*/
38+
protected $trackLastUsedAt;
39+
3340
/**
3441
* Create a new guard instance.
3542
*
3643
* @param \Illuminate\Contracts\Auth\Factory $auth
3744
* @param int $expiration
3845
* @param string $provider
46+
* @param bool $trackLastUsedAt
3947
*/
40-
public function __construct(AuthFactory $auth, $expiration = null, $provider = null)
48+
public function __construct(AuthFactory $auth, $expiration = null, $provider = null, $trackLastUsedAt = true)
4149
{
4250
$this->auth = $auth;
4351
$this->expiration = $expiration;
4452
$this->provider = $provider;
53+
$this->trackLastUsedAt = $trackLastUsedAt;
4554
}
4655

4756
/**
@@ -76,7 +85,9 @@ public function __invoke(Request $request)
7685

7786
event(new TokenAuthenticated($accessToken));
7887

79-
$this->updateLastUsedAt($accessToken);
88+
if ($this->trackLastUsedAt) {
89+
$this->updateLastUsedAt($accessToken);
90+
}
8091

8192
return $tokenable;
8293
}

src/SanctumServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ protected function configureGuard()
103103
protected function createGuard($auth, $config)
104104
{
105105
return new RequestGuard(
106-
new Guard($auth, config('sanctum.expiration'), $config['provider']),
106+
new Guard(
107+
$auth,
108+
config('sanctum.expiration'),
109+
$config['provider'],
110+
config('sanctum.last_used_at', true)
111+
),
107112
request(),
108113
$auth->createUserProvider($config['provider'] ?? null)
109114
);

tests/Feature/GuardTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,65 @@ public static function invalidTokenDataProvider(): array
418418
['Bearer 1ABC|'],
419419
];
420420
}
421+
422+
public function test_last_used_at_is_not_tracked_when_disabled()
423+
{
424+
$factory = Mockery::mock(AuthFactory::class);
425+
426+
$guard = new Guard($factory, null, 'users', false);
427+
428+
$webGuard = Mockery::mock(stdClass::class);
429+
430+
$factory->shouldReceive('guard')
431+
->with('web')
432+
->andReturn($webGuard);
433+
434+
$webGuard->shouldReceive('user')->once()->andReturn(null);
435+
436+
$request = Request::create('/', 'GET');
437+
$request->headers->set('Authorization', 'Bearer test');
438+
439+
$token = PersonalAccessTokenFactory::new()->for(
440+
$user = UserFactory::new()->create(), 'tokenable'
441+
)->create([
442+
'name' => 'Test',
443+
'last_used_at' => null,
444+
]);
445+
446+
$returnedUser = $guard->__invoke($request);
447+
448+
$this->assertEquals($user->id, $returnedUser->id);
449+
$this->assertEquals($token->id, $returnedUser->currentAccessToken()->id);
450+
$this->assertNull($returnedUser->currentAccessToken()->last_used_at);
451+
}
452+
453+
public function test_last_used_at_is_tracked_when_enabled()
454+
{
455+
$factory = Mockery::mock(AuthFactory::class);
456+
457+
$guard = new Guard($factory, null, 'users', true);
458+
459+
$webGuard = Mockery::mock(stdClass::class);
460+
461+
$factory->shouldReceive('guard')
462+
->with('web')
463+
->andReturn($webGuard);
464+
465+
$webGuard->shouldReceive('user')->once()->andReturn(null);
466+
467+
$request = Request::create('/', 'GET');
468+
$request->headers->set('Authorization', 'Bearer test');
469+
470+
$token = PersonalAccessTokenFactory::new()->for(
471+
$user = UserFactory::new()->create(), 'tokenable'
472+
)->create([
473+
'name' => 'Test',
474+
]);
475+
476+
$returnedUser = $guard->__invoke($request);
477+
478+
$this->assertEquals($user->id, $returnedUser->id);
479+
$this->assertEquals($token->id, $returnedUser->currentAccessToken()->id);
480+
$this->assertInstanceOf(DateTimeInterface::class, $returnedUser->currentAccessToken()->last_used_at);
481+
}
421482
}

0 commit comments

Comments
 (0)