Skip to content

Commit

Permalink
fix: update refresh tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyDeHaas committed Oct 8, 2024
1 parent 8370151 commit 3837245
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
64 changes: 48 additions & 16 deletions Tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,68 @@ public function it_should_return_credentials(): void
}

/** @test */
public function it_should_refresh_access_token_if_needed(): void
public function it_should_refresh_access_token(): void
{
$mockResponse = new GuzzleResponse(200, [], json_encode(['access_token' => 'newAccessToken'], JSON_THROW_ON_ERROR));
// Given
$refreshToken = 'testRefreshToken';
$clientId = 123;
$initialAccessToken = 'initialAccessToken';
$newAccessToken = 'newAccessToken';
$baseUrl = 'http://example.com';

$mockResponse = new GuzzleResponse(200, [], json_encode(['access_token' => $newAccessToken]));

$mockGuzzleClient = $this->createMock(GuzzleClient::class);
$mockGuzzleClient->method('request')->willReturn($mockResponse);

$mockTokenService = $this->createMock(TokenService::class);
$mockTokenService->method('isValid')->willReturn(false);

$client = new Client('expiredAccessToken', 'expiredRefreshToken', null, 123, $mockGuzzleClient, $mockTokenService);

$client->refreshAccessTokenIfNeeded();

$this->assertEquals('newAccessToken', $client->getAccessToken());
$client = new Client(
$initialAccessToken,
$refreshToken,
$baseUrl,
$clientId,
$mockGuzzleClient,
$mockTokenService
);

// When
$response = $client->refreshTokensIfNeeded($refreshToken, $clientId);

// Then
$this->assertEquals(200, $response->status());
$this->assertEquals($newAccessToken, $response->body()['access_token']);
$this->assertTrue($response->body()['refreshed']);
}

/** @test */
public function it_should_refresh_access_token(): void
public function it_should_not_refresh_access_token_when_valid(): void
{
$refreshToken = 'testRefreshToken';
$clientId = 123;
// Given
$refreshToken = 'testRefreshToken';
$clientId = 123;
$initialAccessToken = 'validAccessToken';
$baseUrl = 'http://example.com';

$mockTokenService = $this->createMock(TokenService::class);
$mockTokenService->method('isValid')->willReturn(true);

$mockResponse = new GuzzleResponse(200, [], json_encode(['access_token' => 'newAccessToken']));
$mockGuzzleClient = $this->createMock(GuzzleClient::class);
$mockGuzzleClient->method('request')->willReturn($mockResponse);

$client = new Client(null, null, null, null, $mockGuzzleClient);
$response = $client->refreshAccessToken($refreshToken, $clientId);
$client = new Client(
$initialAccessToken,
$refreshToken,
$baseUrl,
$clientId,
$mockGuzzleClient,
$mockTokenService
);

// When
$response = $client->refreshTokensIfNeeded($refreshToken, $clientId);

// Then
$this->assertEquals(200, $response->status());
$this->assertEquals('newAccessToken', $response->body()['access_token']);
$this->assertFalse($response->body()['refreshed']);
}
}
14 changes: 12 additions & 2 deletions src/Service/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PlugAndPay\Sdk\Contract\ClientInterface;
use PlugAndPay\Sdk\Entity\Response;
use PlugAndPay\Sdk\Exception\ExceptionFactory;
use PlugAndPay\Sdk\Exception\InvalidTokenException;
use PlugAndPay\Sdk\Exception\NotFoundException;
use PlugAndPay\Sdk\Exception\ValidationException;
use PlugAndPay\Sdk\Support\Str;
Expand Down Expand Up @@ -226,9 +227,14 @@ public function getCredentials(string $code, string $codeVerifier, string $redir
* @throws JsonException
* @throws NotFoundException
* @throws ValidationException
* @throws InvalidTokenException
*/
public function refreshAccessToken(string $refreshToken, int $clientId): Response
public function refreshTokensIfNeeded(string $refreshToken, int $clientId): Response
{
if ($this->tokenService->isValid($this->accessToken)) {
return new Response(200, ['refreshed' => false]);
}

$response = $this->request(self::METHOD_POST, '/oauth/token', [
'grant_type' => 'refresh_token',
'client_id' => $clientId,
Expand All @@ -245,6 +251,10 @@ public function refreshAccessToken(string $refreshToken, int $clientId): Respons
$this->guzzleClient
);

return $guzzleResponse;
$this->accessToken = $responseData['access_token'];

$responseData['refreshed'] = true;

return new Response($guzzleResponse->status(), $responseData);
}
}

0 comments on commit 3837245

Please sign in to comment.