Skip to content

Commit 9637ccf

Browse files
committed
Add dedicated token lifetime for client credentials grant
1 parent 729ac2a commit 9637ccf

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

CHANGELOG.md

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

33
## [Unreleased](https://github.com/laravel/passport/compare/v13.4.4...13.x)
44

5+
* [13.x] Add dedicated token lifetime for client credentials grant by [@sajanp](https://github.com/sajanp) in https://github.com/laravel/passport/pull/1880
6+
57
## [v13.4.4](https://github.com/laravel/passport/compare/v13.4.3...v13.4.4) - 2026-02-09
68

79
* [13.x] Supports Laravel 13 by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/passport/pull/1885

src/Passport.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class Passport
7474
*/
7575
public static ?DateInterval $personalAccessTokensExpireIn = null;
7676

77+
/**
78+
* The interval when client credentials tokens expire.
79+
*/
80+
public static ?DateInterval $clientCredentialsTokensExpireIn = null;
81+
7782
/**
7883
* The name for API token cookies.
7984
*/
@@ -324,6 +329,20 @@ public static function personalAccessTokensExpireIn(DateTimeInterface|DateInterv
324329
: $date;
325330
}
326331

332+
/**
333+
* Get or set when client credentials grant tokens expire.
334+
*/
335+
public static function clientCredentialsTokensExpireIn(DateTimeInterface|DateInterval|null $date = null): ?DateInterval
336+
{
337+
if (is_null($date)) {
338+
return static::$clientCredentialsTokensExpireIn;
339+
}
340+
341+
return static::$clientCredentialsTokensExpireIn = $date instanceof DateTimeInterface
342+
? Date::now()->diff($date)
343+
: $date;
344+
}
345+
327346
/**
328347
* Get or set the name for API token cookies.
329348
*/

src/PassportServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function (AuthorizationServer $server): void {
158158
}
159159

160160
$server->enableGrantType(
161-
new ClientCredentialsGrant, Passport::tokensExpireIn()
161+
new ClientCredentialsGrant, Passport::clientCredentialsTokensExpireIn() ?? Passport::tokensExpireIn()
162162
);
163163

164164
if (Passport::$implicitGrantEnabled) {

tests/Feature/ClientCredentialsGrantTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ClientCredentialsGrantTest extends PassportTestCase
1616

1717
protected function setUp(): void
1818
{
19+
Passport::$clientCredentialsTokensExpireIn = null;
20+
1921
parent::setUp();
2022

2123
Passport::tokensCan([
@@ -114,4 +116,20 @@ public function testUnauthorizedClient()
114116
$json['error_description']
115117
);
116118
}
119+
120+
public function testCustomClientCredentialsTokenExpiration()
121+
{
122+
Passport::clientCredentialsTokensExpireIn(new \DateInterval('P7D'));
123+
124+
$client = ClientFactory::new()->asClientCredentials()->create();
125+
126+
$json = $this->post('/oauth/token', [
127+
'grant_type' => 'client_credentials',
128+
'client_id' => $client->getKey(),
129+
'client_secret' => $client->plainSecret,
130+
])->assertOk()->json();
131+
132+
// 7 days = 604800 seconds
133+
$this->assertEqualsWithDelta(604800, $json['expires_in'], 2);
134+
}
117135
}

0 commit comments

Comments
 (0)