Skip to content

Commit 039a775

Browse files
author
Vítězslav Dvořák
committed
Enhances refresh token error handling
Implements improved error handling for expired refresh tokens by introducing specific exception codes. Updates exception messages and adds tests to ensure proper handling of identity provider exceptions, particularly for cases where refresh tokens expire. Increases robustness of the authentication process by providing clearer feedback to users when re-authorization is required.
1 parent e581dd9 commit 039a775

5 files changed

Lines changed: 254 additions & 60 deletions

File tree

composer.lock

Lines changed: 51 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libexec/csas-access-token.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,29 @@
240240

241241
if ($expiresAt < $expiresSoon) {
242242
// Refresh the token if it is expired or will expire soon
243-
$token->refreshToken(new \SpojeNet\CSas\Auth($token->getApplication()));
243+
try {
244+
$token->refreshToken(new \SpojeNet\CSas\Auth($token->getApplication()));
245+
} catch (\RuntimeException $exception) {
246+
if ($exception->getCode() === 24) {
247+
// Refresh token has expired
248+
if (isset($options['json']) || isset($options['j'])) {
249+
echo json_encode([
250+
'error' => 'refresh_token_expired',
251+
'error_description' => _('Refresh token has expired. Please re-authorize the application.'),
252+
'uuid' => $token->getDataValue('uuid'),
253+
'application_id' => $token->getDataValue('application_id')
254+
], JSON_PRETTY_PRINT);
255+
} else {
256+
echo _('Error: Refresh token has expired. Please re-authorize the application.') . "\n";
257+
echo 'Token UUID: ' . $token->getDataValue('uuid') . "\n";
258+
echo 'Application ID: ' . $token->getDataValue('application_id') . "\n";
259+
}
260+
exit(1);
261+
} else {
262+
// Other runtime exception, re-throw it
263+
throw $exception;
264+
}
265+
}
244266
}
245267

246268
if (isset($options['json']) || isset($options['j'])) {

src/SpojeNet/CSas/Token.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,21 @@ public function refreshToken(AbstractProvider $provider): AccessToken
115115
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $exception) {
116116
$errorData = $exception->getResponseBody();
117117
$errorMessage = $errorData['error_description'] ?? $exception->getMessage();
118+
$errorCode = $errorData['error_code'] ?? null;
118119

119120
// Clear the expired refresh token
120121
$this->setDataValue('refresh_token', null);
121122
$this->dbSync();
122123

123124
$this->addStatusMessage(sprintf(_('Token refresh failed: %s'), $errorMessage), 'error');
124125

125-
throw new \RuntimeException(_('Refresh token has expired'), 24, $exception);
126+
// Check if this is specifically a refresh token expiration error
127+
if ($errorCode === '7109' || strpos($errorMessage, 'expired') !== false) {
128+
throw new \RuntimeException(_('Refresh token has expired'), 24, $exception);
129+
}
130+
131+
// For other OAuth2 errors, throw a different exception
132+
throw new \RuntimeException(sprintf(_('OAuth2 error: %s'), $errorMessage), 25, $exception);
126133
}
127134
}
128135

0 commit comments

Comments
 (0)