HTTPlug plugin to automatically refresh expired OAuth2 access tokens.
Find a file
2025-12-06 07:54:54 +01:00
.github/workflows Add PHP 8.5 to CI runs 2025-12-06 07:54:54 +01:00
src Switch to PER coding style 2025-10-19 14:52:02 +02:00
tests Switch to PER coding style 2025-10-19 14:52:02 +02:00
.gitignore Upgrade to Pest 3.x 2025-04-19 07:56:42 +02:00
.php-cs-fixer.dist.php Switch to PER coding style 2025-10-19 14:52:02 +02:00
composer.json Upgrade to Pest 4.x 2025-10-19 15:19:04 +02:00
LICENSE.md Initial commit 2022-01-05 15:50:32 +01:00
phpstan.neon.dist Lower PHPStan level 2025-04-19 08:21:34 +02:00
phpunit.xml.dist Update Pest to 2.0 2023-09-22 20:47:51 +02:00
README.md Bump min required PHP version to 8.4 2025-10-19 14:43:00 +02:00

PHP AutoRefreshOAuth2TokenPlugin

Packagist build Coverage Status

A HTTPlug plugin to automatically refresh expired OAuth2 access tokens.

Requirements

Installation

Via Composer:

composer require jwilsson/auto-refresh-oauth2-token-plugin

Usage

This assumes you have an instantiated Refresh Token grant and Token object from the jwilsson/oauth2-client library. A full Token object complete with access token, refresh token, and expiry information is expected.

use Http\Client\Common\PluginClient;
use JWilsson\AutoRefreshOAuth2TokenPlugin;

$autoRefreshOAuth2TokenPlugin = new AutoRefreshOAuth2TokenPlugin(
    $token,
    $refreshTokenGrant,
    $options, // Options for the plugin, see below
    $refreshTokenOptions // Additional options to pass to RefreshToken::requestAccessToken()
);

$pluginClient = new PluginClient(
    $myHttpClient,
    [$autoRefreshOAuth2TokenPlugin]
);

$response = $pluginClient->sendRequest($myRequest);

// Remember to grab the token object after each call, it might have been updated with new information
$refreshedToken = $autoRefreshOAuth2TokenPlugin->getToken();

Options

  • threshold - Threshold in seconds for how close to the token's expiry time it should be considered expired. Default is 300 (5 minutes).