Skip to content

Commit f7ac422

Browse files
committed
Laravel 9
2 parents 29bd9bf + 180d263 commit f7ac422

File tree

5 files changed

+79
-48
lines changed

5 files changed

+79
-48
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ jobs:
1212
- 7.4
1313
perfer:
1414
- stable
15-
container:
16-
image: nauxliu/php-ci-image:${{ matrix.php_version }}
1715
steps:
1816
- uses: actions/checkout@master
1917
- name: Install Dependencies
2018
run: composer install --prefer-dist --no-interaction --no-suggest
21-
- name: Run PHP-CS-Fxier
22-
run: composer check-style
2319

2420
phpunit:
2521
name: phpunit
@@ -28,13 +24,11 @@ jobs:
2824
fail-fast: false
2925
matrix:
3026
php_version:
31-
- 7.3
3227
- 7.4
3328
- 8.0
29+
- 8.1
3430
perfer:
3531
- stable
36-
container:
37-
image: nauxliu/php-ci-image:${{ matrix.php_version }}
3832
steps:
3933
- uses: actions/checkout@master
4034
- name: Install Dependencies
@@ -52,8 +46,6 @@ jobs:
5246
- 7.4
5347
perfer:
5448
- stable
55-
container:
56-
image: nauxliu/php-ci-image:${{ matrix.php_version }}
5749
steps:
5850
- uses: actions/checkout@master
5951
- name: Install laravel/framework:^6.0

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Make [laravel/passport](https://github.com/laravel/passport) token cacheable.
44

5+
[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)
6+
57

68
## Installing
79

@@ -33,6 +35,20 @@ return [
3335
];
3436
```
3537

38+
39+
## :heart: Sponsor me
40+
41+
[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)
42+
43+
如果你喜欢我的项目并想支持它,[点击这里 :heart:](https://github.com/sponsors/overtrue)
44+
45+
46+
## Project supported by JetBrains
47+
48+
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
49+
50+
[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)
51+
3652
## Contributing
3753

3854
You can contribute in one of three ways:

phpunit.xml.dist

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false">
11-
<testsuites>
12-
<testsuite name="Application Test Suite">
13-
<directory>./tests/</directory>
14-
</testsuite>
15-
</testsuites>
16-
<filter>
17-
<whitelist>
18-
<directory suffix=".php">src/</directory>
19-
</whitelist>
20-
</filter>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Application Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
2113
</phpunit>

src/CacheTokenRepository.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function __construct(string $cacheKeyPrefix = null, int $expiresInSeconds
5353
*
5454
* @return \Laravel\Passport\Token
5555
*/
56-
public function find($id)
56+
public function find($id): ?Token
5757
{
58-
return $this->store()->remember(
58+
return $this->cacheStore()->remember(
5959
$this->itemKey($id),
6060
\now()->addSeconds($this->expiresInSeconds),
6161
function () use ($id) {
@@ -72,9 +72,9 @@ function () use ($id) {
7272
*
7373
* @return \Laravel\Passport\Token|null
7474
*/
75-
public function findForUser($id, $userId)
75+
public function findForUser($id, $userId): ?Token
7676
{
77-
return $this->store()->remember(
77+
return $this->cacheStore()->remember(
7878
$this->itemKey($id),
7979
\now()->addSeconds($this->expiresInSeconds),
8080
function () use ($id, $userId) {
@@ -92,7 +92,7 @@ function () use ($id, $userId) {
9292
*/
9393
public function forUser($userId): Collection
9494
{
95-
return $this->store()->remember(
95+
return $this->cacheStore()->remember(
9696
$this->itemKey($userId),
9797
\now()->addSeconds($this->expiresInSeconds),
9898
function () use ($userId) {
@@ -109,9 +109,9 @@ function () use ($userId) {
109109
*
110110
* @return \Laravel\Passport\Token|null
111111
*/
112-
public function getValidToken($user, $client)
112+
public function getValidToken($user, $client): ?Token
113113
{
114-
return $this->store()->remember(
114+
return $this->cacheStore()->remember(
115115
$this->itemKey($user->getKey()),
116116
\now()->addSeconds($this->expiresInSeconds),
117117
function () use ($client, $user) {
@@ -124,15 +124,22 @@ function () use ($client, $user) {
124124
);
125125
}
126126

127-
public function itemKey(string $key)
127+
public function itemKey(string $key): string
128128
{
129129
return $this->cacheKeyPrefix . $key;
130130
}
131131

132-
public function store(): Repository
132+
public function cacheStore(): Repository
133133
{
134134
$store = Cache::store($this->cacheStore);
135135

136-
return $store instanceof TaggableStore ? $store->tags($this->cacheTags) : $store;
136+
return $store->getStore() instanceof TaggableStore ? $store->tags($this->cacheTags) : $store;
137+
}
138+
139+
public function revokeAccessToken($id)
140+
{
141+
parent::revokeAccessToken($id);
142+
143+
$this->cacheStore()->forget($this->itemKey($id));
137144
}
138145
}

tests/FeatureTest.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Tests;
44

5+
use Illuminate\Auth\RequestGuard;
56
use Illuminate\Contracts\Hashing\Hasher;
67
use Illuminate\Contracts\Routing\Registrar;
78
use Illuminate\Support\Facades\Auth;
89
use Illuminate\Support\Str;
910
use Laravel\Passport\Client;
1011
use Laravel\Passport\ClientRepository;
12+
use Laravel\Passport\TokenRepository;
1113

1214
class FeatureTest extends TestCase
1315
{
@@ -54,43 +56,65 @@ public function test_it_can_cache_token()
5456
$user->password = $this->app->make(Hasher::class)->make($password);
5557
$user->save();
5658

59+
$tokenRepository = app(TokenRepository::class);
60+
5761
/** @var Client $client */
58-
app(ClientRepository::class)->createPersonalAccessClient($user->id, 'Personal Token Client', 'http://localhost');
62+
$client = app(ClientRepository::class)->createPersonalAccessClient($user->id, 'Personal Token Client', 'http://localhost');
5963

6064
/** @var Registrar $router */
6165
$router = $this->app->make(Registrar::class);
6266

63-
$token = $user->createToken('test')->accessToken;
67+
$accessToken = $user->createToken('test')->accessToken;
6468

6569
$router->get('/foo', function () {
6670
return 'bar';
6771
})->middleware('auth:api');
6872

69-
$query = $this->getQueryLog(function () use ($token, $user, $router) {
73+
$query = $this->getQueryLog(function () use ($accessToken, $user, $router) {
7074
$this->getJson('/foo')->assertStatus(401);
71-
$this->withHeader('Authorization', 'Bearer ' . $token)->getJson('/foo')->assertSuccessful()->assertSee('bar');
75+
$this->withHeader('Authorization', 'Bearer ' . $accessToken)->getJson('/foo')->assertSuccessful()->assertSee('bar');
7276
});
7377

7478
$this->assertCount(3, $query);
7579

7680
// token cached
77-
$query = $this->getQueryLog(function () use ($token, $user, $router) {
81+
$query = $this->getQueryLog(function () use ($accessToken, $user, $router) {
7882
$router->get('/me', function () {
7983
return Auth::user();
8084
})->middleware('auth:api');
8185

82-
$this->withHeader('Authorization', 'Bearer ' . $token)->getJson('/me')->assertSuccessful()->assertJsonFragment([
86+
$this->withHeader('Authorization', 'Bearer ' . $accessToken)->getJson('/me')->assertSuccessful()->assertJsonFragment([
8387
'id' => $user->id,
8488
'email' => $user->email,
8589
]);
8690

87-
$this->withHeader('Authorization', 'Bearer ' . $token)->getJson('/me')->assertOk();
88-
$this->withHeader('Authorization', 'Bearer ' . $token)->getJson('/me')->assertOk();
89-
$this->withHeader('Authorization', 'Bearer ' . $token)->getJson('/me')->assertOk();
90-
$this->withHeader('Authorization', 'Bearer ' . $token)->getJson('/me')->assertOk();
91+
$this->withHeader('Authorization', 'Bearer ' . $accessToken)->getJson('/me')->assertOk();
92+
$this->withHeader('Authorization', 'Bearer ' . $accessToken)->getJson('/me')->assertOk();
93+
$this->withHeader('Authorization', 'Bearer ' . $accessToken)->getJson('/me')->assertOk();
94+
$this->withHeader('Authorization', 'Bearer ' . $accessToken)->getJson('/me')->assertOk();
9195
});
9296

9397
$this->assertCount(0, $query);
98+
99+
100+
// revoke token
101+
$token = $tokenRepository->findValidToken($user, $client);
102+
$this->assertTrue($tokenRepository->cacheStore()->has(app(TokenRepository::class)->itemKey($token->id)));
103+
104+
$tokenRepository->revokeAccessToken($token->id);
105+
$token->refresh();
106+
$this->assertTrue($token->revoked);
107+
108+
$this->assertFalse($tokenRepository->cacheStore()->has($tokenRepository->itemKey($token->id)));
109+
110+
// logout
111+
RequestGuard::macro('logout', function () {
112+
$this->user = null;
113+
});
114+
Auth::guard('api')->logout();
115+
116+
// request with revoked token
117+
$this->withHeader('Authorization', 'Bearer ' . $accessToken)->getJson('/me')->assertUnauthorized();
94118
}
95119

96120
protected function getQueryLog(\Closure $callback): \Illuminate\Support\Collection

0 commit comments

Comments
 (0)