Skip to content

Commit a80f2fe

Browse files
committed
fix: Remove ID token if access token cannot be refreshed
1 parent 6f86340 commit a80f2fe

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Client.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use TRSTD\COT\AnonymousConsumerData;
2525
use TRSTD\COT\Exception\UnexpectedErrorException;
2626
use TRSTD\COT\Exception\RequiredParameterMissingException;
27+
use TRSTD\COT\Exception\TokenInvalidException;
2728
use TRSTD\COT\Exception\TokenNotFoundException;
2829
use TRSTD\COT\Util\EncryptionUtils;
2930
use TRSTD\COT\Util\PKCEUtils;
@@ -314,10 +315,17 @@ private function getOrRefreshAccessToken($idToken)
314315
}
315316

316317
if ($shouldRefresh) {
317-
$refreshedToken = $this->getRefreshedToken($token->refreshToken);
318-
319-
if (!$refreshedToken) {
320-
$this->logger->debug('Refresh token is invalid.');
318+
$refreshedToken = null;
319+
try {
320+
$refreshedToken = $this->getRefreshedToken($token->refreshToken);
321+
322+
if (!$refreshedToken) {
323+
$this->logger->debug('Refresh token is invalid.');
324+
throw new TokenInvalidException("Refresh token is invalid.");
325+
}
326+
} catch (Exception $ex) {
327+
$this->logger->debug('Error occurred while refreshing the token: ' . $ex->getMessage());
328+
$this->removeIdentityCookie();
321329
return null;
322330
}
323331

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace TRSTD\COT\Exception;
4+
5+
use Exception;
6+
use RuntimeException;
7+
8+
final class TokenInvalidException extends RuntimeException
9+
{
10+
/**
11+
* TokenInvalidException constructor.
12+
*
13+
* @param string $message The message to log
14+
* @param int $code The error code
15+
* @param Exception|null $previous The previous exception
16+
*/
17+
public function __construct($message = 'Unexpected error', $code = 0, Exception $previous = null)
18+
{
19+
parent::__construct($message, $code, $previous);
20+
}
21+
}

0 commit comments

Comments
 (0)