Skip to content

Commit 9c07921

Browse files
committed
formatting
1 parent 7c84f1b commit 9c07921

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Release Notes
22

33
## [Unreleased](https://github.com/laravel/sanctum/compare/v2.10.0...2.x)
4+
45
### Added
56
- `Sanctum::$validateCallback` callback for more granular control over access token validation ([#275](https://github.com/laravel/sanctum/pull/275))
67

8+
79
## [v2.10.0 (2021-04-20)](https://github.com/laravel/sanctum/compare/v2.9.4...v2.10.0)
810

911
### Added

src/Guard.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,42 +89,42 @@ protected function supportsTokens($tokenable = null)
8989
}
9090

9191
/**
92-
* Determine if the tokenable model matches the provider's model type.
92+
* Determine if the provided access token is valid.
9393
*
94-
* @param \Illuminate\Database\Eloquent\Model $tokenable
94+
* @param mixed $accessToken
9595
* @return bool
9696
*/
97-
protected function hasValidProvider($tokenable)
97+
protected function isValidAccessToken($accessToken): bool
9898
{
99-
if (is_null($this->provider)) {
100-
return true;
99+
if (! $accessToken) {
100+
return false;
101101
}
102102

103-
$model = config("auth.providers.{$this->provider}.model");
103+
$isValid =
104+
(! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))
105+
&& $this->hasValidProvider($accessToken->tokenable);
104106

105-
return $tokenable instanceof $model;
107+
if ($isValid && is_callable(Sanctum::$accessTokenAuthenticationCallback)) {
108+
$isValid = (bool) (Sanctum::$accessTokenAuthenticationCallback)($accessToken);
109+
}
110+
111+
return $isValid;
106112
}
107113

108114
/**
109-
* Determine if the provided access token is valid.
115+
* Determine if the tokenable model matches the provider's model type.
110116
*
111-
* @param mixed $accessToken
117+
* @param \Illuminate\Database\Eloquent\Model $tokenable
112118
* @return bool
113119
*/
114-
protected function isValidAccessToken($accessToken): bool
120+
protected function hasValidProvider($tokenable)
115121
{
116-
if (! $accessToken) {
117-
return false;
122+
if (is_null($this->provider)) {
123+
return true;
118124
}
119125

120-
$is_valid =
121-
(! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))
122-
&& $this->hasValidProvider($accessToken->tokenable);
123-
124-
if (is_callable(Sanctum::$validateCallback)) {
125-
$is_valid = (bool) (Sanctum::$validateCallback)($accessToken, $is_valid);
126-
}
126+
$model = config("auth.providers.{$this->provider}.model");
127127

128-
return $is_valid;
128+
return $tokenable instanceof $model;
129129
}
130130
}

src/Sanctum.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,18 @@ class Sanctum
1414
public static $personalAccessTokenModel = 'Laravel\\Sanctum\\PersonalAccessToken';
1515

1616
/**
17-
* Indicates if Sanctum's migrations will be run.
17+
* A callback that can add to the validation of the access token.
1818
*
19-
* @var bool
19+
* @var callable|null
2020
*/
21-
public static $runsMigrations = true;
21+
public static $accessTokenAuthenticationCallback;
2222

2323
/**
24-
* A callback that can add to the validation of the access token.
25-
* Receives 2 parameters:
26-
* - (object) The provided access token model.
27-
* - (bool) Whether the guard deemed the access token valid.
24+
* Indicates if Sanctum's migrations will be run.
2825
*
29-
* @var callable|null
26+
* @var bool
3027
*/
31-
public static $validateCallback;
28+
public static $runsMigrations = true;
3229

3330
/**
3431
* Set the current user for the application with the given abilities.
@@ -74,6 +71,17 @@ public static function usePersonalAccessTokenModel($model)
7471
static::$personalAccessTokenModel = $model;
7572
}
7673

74+
/**
75+
* Specify a callback that should be used to authenticate access tokens.
76+
*
77+
* @param callable $callback
78+
* @return void
79+
*/
80+
public static function authenticateAccessTokensUsing(callable $callback)
81+
{
82+
static::$accessTokenAuthenticationCallback = $callback;
83+
}
84+
7785
/**
7886
* Determine if Sanctum's migrations should be run.
7987
*

tests/GuardTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,11 @@ public function test_authentication_fails_if_callback_returns_false()
259259
'token' => hash('sha256', 'test'),
260260
]);
261261

262-
Sanctum::$validateCallback = function ($accessToken, bool $is_valid) {
262+
Sanctum::authenticateAccessTokensUsing(function ($accessToken) {
263263
$this->assertInstanceOf(PersonalAccessToken::class, $accessToken);
264-
$this->assertTrue($is_valid);
265264

266265
return false;
267-
};
266+
});
268267

269268
$user = $requestGuard->setRequest($request)->user();
270269
$this->assertNull($user);

0 commit comments

Comments
 (0)