diff --git a/composer.json b/composer.json index c6c78b1e..a1b03123 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "guzzlehttp/guzzle": "^7.6 || ^8.0", "guzzlehttp/promises": "^1.5 || ^2.0 || ^3.0", "guzzlehttp/psr7": "^2.0 || ^3.0", + "psr/clock": "^1.0", "psr/http-factory": "^1.0", "psr/http-message": "^1.1 || ^2.0" }, diff --git a/src/Config.php b/src/Config.php index f0d7f4a7..7e0f3696 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,8 +4,10 @@ namespace Saloon; +use DateTimeImmutable; use Saloon\Enums\PipeOrder; use Saloon\Contracts\Sender; +use Psr\Clock\ClockInterface; use Saloon\Http\PendingRequest; use Saloon\Http\Senders\GuzzleSender; use Saloon\Helpers\MiddlewarePipeline; @@ -52,6 +54,11 @@ final class Config */ private static bool $preventStrayRequests = false; + /** + * Global clock used by built-in time-aware features. + */ + private static ?ClockInterface $clock = null; + /** * Write a custom sender resolver */ @@ -70,6 +77,30 @@ public static function getDefaultSender(): Sender return is_callable($senderResolver) ? $senderResolver() : new self::$defaultSender; } + /** + * Set the global package clock. + */ + public static function setClock(?ClockInterface $clock): void + { + self::$clock = $clock; + } + + /** + * Get the global package clock. + */ + public static function getClock(): ?ClockInterface + { + return self::$clock; + } + + /** + * Resolve the current time. + */ + public static function now(): DateTimeImmutable + { + return self::$clock?->now() ?? new DateTimeImmutable; + } + /** * Update global middleware */ diff --git a/src/Http/Auth/AccessTokenAuthenticator.php b/src/Http/Auth/AccessTokenAuthenticator.php index 17569f29..2af02c4f 100644 --- a/src/Http/Auth/AccessTokenAuthenticator.php +++ b/src/Http/Auth/AccessTokenAuthenticator.php @@ -4,6 +4,7 @@ namespace Saloon\Http\Auth; +use Saloon\Config; use DateTimeImmutable; use Saloon\Http\PendingRequest; use Saloon\Contracts\OAuthAuthenticator; @@ -38,7 +39,7 @@ public function hasExpired(): bool return false; } - return $this->expiresAt->getTimestamp() <= (new DateTimeImmutable)->getTimestamp(); + return $this->expiresAt->getTimestamp() <= Config::now()->getTimestamp(); } /** diff --git a/src/Traits/OAuth2/AuthorizationCodeGrant.php b/src/Traits/OAuth2/AuthorizationCodeGrant.php index a6d9a6a8..9f6fa7c9 100644 --- a/src/Traits/OAuth2/AuthorizationCodeGrant.php +++ b/src/Traits/OAuth2/AuthorizationCodeGrant.php @@ -5,6 +5,7 @@ namespace Saloon\Traits\OAuth2; use DateInterval; +use Saloon\Config; use DateTimeImmutable; use Saloon\Http\Request; use Saloon\Http\Response; @@ -159,7 +160,7 @@ protected function createOAuthAuthenticatorFromResponse(Response $response, ?str $expiresAt = null; if (isset($responseData->expires_in) && is_numeric($responseData->expires_in)) { - $expiresAt = (new DateTimeImmutable)->add( + $expiresAt = Config::now()->add( DateInterval::createFromDateString((int)$responseData->expires_in . ' seconds') ); } diff --git a/src/Traits/OAuth2/ClientCredentialsGrant.php b/src/Traits/OAuth2/ClientCredentialsGrant.php index 213e999c..59154d54 100644 --- a/src/Traits/OAuth2/ClientCredentialsGrant.php +++ b/src/Traits/OAuth2/ClientCredentialsGrant.php @@ -5,6 +5,7 @@ namespace Saloon\Traits\OAuth2; use DateInterval; +use Saloon\Config; use DateTimeImmutable; use Saloon\Http\Request; use Saloon\Http\Response; @@ -64,7 +65,7 @@ protected function createOAuthAuthenticatorFromResponse(Response $response): OAu $expiresAt = null; if (isset($responseData->expires_in) && is_numeric($responseData->expires_in)) { - $expiresAt = (new DateTimeImmutable)->add( + $expiresAt = Config::now()->add( DateInterval::createFromDateString((int)$responseData->expires_in . ' seconds') ); } diff --git a/tests/Feature/Oauth2/AuthCodeFlowConnectorTest.php b/tests/Feature/Oauth2/AuthCodeFlowConnectorTest.php index 0e4f6e83..237e8e6e 100644 --- a/tests/Feature/Oauth2/AuthCodeFlowConnectorTest.php +++ b/tests/Feature/Oauth2/AuthCodeFlowConnectorTest.php @@ -2,12 +2,14 @@ declare(strict_types=1); +use Saloon\Config; use Saloon\Http\Request; use Saloon\Http\Response; use Saloon\Tests\Helpers\Date; use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; use Saloon\Http\OAuth2\GetUserRequest; +use Saloon\Tests\Fixtures\Clock\FixedClock; use Saloon\Exceptions\InvalidStateException; use Saloon\Http\OAuth2\GetAccessTokenRequest; use Saloon\Http\Auth\AccessTokenAuthenticator; @@ -22,6 +24,10 @@ use Saloon\Tests\Fixtures\Connectors\CustomResponseOAuth2Connector; use Saloon\Tests\Fixtures\Requests\OAuth\CustomRefreshTokenRequest; +afterEach(function () { + Config::setClock(null); +}); + test('you can get the redirect url from a connector', function () { $connector = new OAuth2Connector; @@ -109,6 +115,40 @@ expect($authenticator->getExpiresAt())->toBeInstanceOf(DateTimeImmutable::class); }); +test('oauth access tokens derive expiry from the global clock', function () { + $now = new DateTimeImmutable('2026-01-01T00:00:00+00:00'); + $mockClient = new MockClient([ + MockResponse::make(['access_token' => 'access', 'refresh_token' => 'refresh', 'expires_in' => 3600], 200), + ]); + + Config::setClock(new FixedClock($now)); + + $connector = new OAuth2Connector; + $connector->withMockClient($mockClient); + + $authenticator = $connector->getAccessToken('code'); + + expect($authenticator->getExpiresAt())->toEqual($now->modify('+3600 seconds')); +}); + +test('custom oauth authenticators use the global clock for expiry semantics', function () { + $now = new DateTimeImmutable('2026-01-01T00:00:00+00:00'); + $mockClient = new MockClient([ + MockResponse::make(['access_token' => 'access', 'refresh_token' => 'refresh', 'expires_in' => 3600], 200), + ]); + + Config::setClock(new FixedClock($now)); + + $connector = new CustomResponseOAuth2Connector('hello'); + $connector->withMockClient($mockClient); + + $authenticator = $connector->getAccessToken('code'); + + expect($authenticator)->toBeInstanceOf(CustomOAuthAuthenticator::class); + expect($authenticator->getExpiresAt())->toEqual($now->modify('+3600 seconds')); + expect($authenticator->hasExpired())->toBeFalse(); +}); + test('you can tap into the access token request and modify it', function () { $mockClient = new MockClient([ MockResponse::make(['access_token' => 'access', 'refresh_token' => 'refresh', 'expires_in' => 3600], 200), diff --git a/tests/Feature/Oauth2/ClientCredentialsFlowConnectorTest.php b/tests/Feature/Oauth2/ClientCredentialsFlowConnectorTest.php index c6f4a892..e7b175cc 100644 --- a/tests/Feature/Oauth2/ClientCredentialsFlowConnectorTest.php +++ b/tests/Feature/Oauth2/ClientCredentialsFlowConnectorTest.php @@ -2,10 +2,12 @@ declare(strict_types=1); +use Saloon\Config; use Saloon\Http\Request; use Saloon\Http\Response; use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; +use Saloon\Tests\Fixtures\Clock\FixedClock; use Saloon\Http\Auth\AccessTokenAuthenticator; use Saloon\Exceptions\OAuthConfigValidationException; use Saloon\Tests\Fixtures\Connectors\ClientCredentialsConnector; @@ -14,6 +16,10 @@ use Saloon\Tests\Fixtures\Connectors\CustomRequestClientCredentialsConnector; use Saloon\Tests\Fixtures\Requests\OAuth\CustomClientCredentialsAccessTokenRequest; +afterEach(function () { + Config::setClock(null); +}); + test('you can get the authenticator from the connector', function () { $mockClient = new MockClient([ MockResponse::make(['access_token' => 'access', 'expires_in' => 3600], 200), @@ -40,6 +46,22 @@ ]); }); +test('client credentials tokens derive expiry from the global clock', function () { + $now = new DateTimeImmutable('2026-01-01T00:00:00+00:00'); + $mockClient = new MockClient([ + MockResponse::make(['access_token' => 'access', 'expires_in' => 3600], 200), + ]); + + Config::setClock(new FixedClock($now)); + + $connector = new ClientCredentialsConnector; + $connector->withMockClient($mockClient); + + $authenticator = $connector->getAccessToken(); + + expect($authenticator->getExpiresAt())->toEqual($now->modify('+3600 seconds')); +}); + test('you can get the response instead of the authenticator', function () { $mockClient = new MockClient([ MockResponse::make(['access_token' => 'access', 'expires_in' => 3600], 200), diff --git a/tests/Fixtures/Clock/FixedClock.php b/tests/Fixtures/Clock/FixedClock.php new file mode 100644 index 00000000..0d9a9414 --- /dev/null +++ b/tests/Fixtures/Clock/FixedClock.php @@ -0,0 +1,27 @@ +now; + } +} diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index ee0f22db..ee4ce124 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -8,6 +8,7 @@ use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; use Saloon\Http\Senders\GuzzleSender; +use Saloon\Tests\Fixtures\Clock\FixedClock; use Saloon\Exceptions\StrayRequestException; use Saloon\Tests\Fixtures\Senders\ArraySender; use Saloon\Tests\Fixtures\Requests\UserRequest; @@ -16,6 +17,9 @@ afterEach(function () { Config::clearGlobalMiddleware(); Config::$defaultSender = GuzzleSender::class; + Config::setSenderResolver(null); + Config::setClock(null); + Config::allowStrayRequests(); }); test('the config can specify global middleware', function () { @@ -74,6 +78,20 @@ expect($sender)->toBeInstanceOf(GuzzleSender::class); }); +test('you can configure a global clock and resolve now', function () { + $now = new DateTimeImmutable('2026-01-01T00:00:00+00:00'); + $clock = new FixedClock($now); + + Config::setClock($clock); + + expect(Config::getClock())->toBe($clock); + expect(Config::now())->toEqual($now); + + Config::setClock(null); + + expect(Config::getClock())->toBeNull(); +}); + test('you can prevent stray api requests', function () { Config::preventStrayRequests(); diff --git a/tests/Unit/Oauth2/AccessTokenAuthenticatorTest.php b/tests/Unit/Oauth2/AccessTokenAuthenticatorTest.php index f42ef84c..99bbb568 100644 --- a/tests/Unit/Oauth2/AccessTokenAuthenticatorTest.php +++ b/tests/Unit/Oauth2/AccessTokenAuthenticatorTest.php @@ -2,9 +2,15 @@ declare(strict_types=1); +use Saloon\Config; use Saloon\Tests\Helpers\Date; +use Saloon\Tests\Fixtures\Clock\FixedClock; use Saloon\Http\Auth\AccessTokenAuthenticator; +afterEach(function () { + Config::setClock(null); +}); + it('can return if it has expired or not', function () { $accessToken = 'access'; $refreshToken = 'refresh'; @@ -44,3 +50,14 @@ expect($authenticator->isRefreshable())->toBeTrue(); expect($authenticator->isNotRefreshable())->toBeFalse(); }); + +test('it can use the global clock for expiry checks', function () { + $expiresAt = new DateTimeImmutable('2026-01-01T01:00:00+00:00'); + + Config::setClock(new FixedClock(new DateTimeImmutable('2026-01-01T02:00:00+00:00'))); + + $authenticator = new AccessTokenAuthenticator('access', 'refresh', $expiresAt); + + expect($authenticator->hasExpired())->toBeTrue(); + expect($authenticator->hasNotExpired())->toBeFalse(); +});