Skip to content

Commit e35fc0e

Browse files
committed
TTL
1 parent e950ed8 commit e35fc0e

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Validates the presence and integrity of a JSON Web Token (JWT) in the `Authoriza
8686
'auth' => [
8787
'jwt_public_key' => env('JWT_PUBLIC_KEY_PATH'),
8888
'jwt_algorithm' => env('JWT_ALGORITHM', 'RS256'),
89+
'jwt_cache_ttl' => env('JWT_CACHE_TTL', 3600),
8990
'header' => 'Authorization',
9091
'prefix' => 'Bearer',
9192
],

src/Http/Middleware/ValidateJwt.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Firebase\JWT\Key;
88
use Illuminate\Http\Request;
99
use Illuminate\Support\Facades\Auth;
10+
use Illuminate\Support\Facades\Cache;
1011
use Kroderdev\LaravelMicroserviceCore\Auth\ExternalUser;
1112
use Kroderdev\LaravelMicroserviceCore\Services\PermissionsClient;
1213
use Symfony\Component\HttpFoundation\Response;
@@ -31,7 +32,9 @@ public function handle(Request $request, Closure $next): Response
3132
$token = substr($authHeader, 7);
3233

3334
try {
34-
$publicKey = file_get_contents(config('microservice.auth.jwt_public_key'));
35+
$publicKey = Cache::remember('jwt_cache_ttl', config('microservice.auth.public_key_ttl', 3600), function() {
36+
return file_get_contents(config('microservice.auth.jwt_cache_ttl'));
37+
});
3538

3639
$decoded = JWT::decode($token, new Key($publicKey, config('microservice.auth.jwt_algorithm')));
3740

src/config/microservice.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
*/
5151
'jwt_algorithm' => env('JWT_ALGORITHM', 'RS256'),
5252

53+
54+
/**
55+
* The time-to-live (TTL) in seconds for caching JWT keys.
56+
* Determines how long the JWT key will be stored in cache before it expires.
57+
* Default is 3600 seconds (1 hour).
58+
*/
59+
'jwt_cache_ttl' => env('JWT_CACHE_TTL', 3600),
60+
5361
/**
5462
* Authorization Header:
5563
* The HTTP header from which to extract the JWT token.

0 commit comments

Comments
 (0)