Skip to content

Commit

Permalink
try to provide more details in the TokenExchangeFailedException when …
Browse files Browse the repository at this point in the history
…token exchange fails

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
  • Loading branch information
julien-nc committed Nov 27, 2024
1 parent d097d04 commit fba2265
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/token_exchange.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ if (class_exists('OCA\UserOIDC\Event\ExchangedTokenRequestedEvent')) {
$this->eventDispatcher->dispatchTyped($event);
} catch (OCA\UserOIDC\Exception\TokenExchangeFailedException $e) {
$this->logger->debug('Failed to exchange token: ' . $e->getMessage());
$error = $e->getError();
$errorDescription = $e->getErrorDescription();
if ($error && $errorDescription) {
$this->logger->debug('Token exchange error response from the IdP: ' . $error . ' (' . $errorDescription . ')');
}
}
$token = $event->getToken();
if ($token === null) {
Expand Down
18 changes: 18 additions & 0 deletions lib/Exception/TokenExchangeFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,22 @@
use Exception;

class TokenExchangeFailedException extends Exception {

public function __construct(
$message = '',
$code = 0,
$previous = null,
private ?string $error = null,
private ?string $errorDescription = null,
) {
parent::__construct($message, $code, $previous);
}

public function getError(): ?string {
return $this->error;
}

public function getErrorDescription(): ?string {
return $this->errorDescription;
}
}
23 changes: 23 additions & 0 deletions lib/Service/TokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace OCA\UserOIDC\Service;

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use OCA\UserOIDC\AppInfo\Application;
use OCA\UserOIDC\Db\ProviderMapper;
use OCA\UserOIDC\Exception\TokenExchangeFailedException;
Expand Down Expand Up @@ -268,6 +270,27 @@ public function getExchangedToken(string $targetAudience): Token {
['provider_id' => $loginToken->getProviderId()],
);
return new Token($tokenData);
} catch (ClientException|ServerException $e) {
$response = $e->getResponse();
$body = (string)$response->getBody();
$this->logger->error('[TokenService] Failed to exchange token, client/server error in the exchange request', ['response_body' => $body, 'exception' => $e]);

$parsedBody = json_decode(trim($body), true);
if (is_array($parsedBody) && isset($parsedBody['error'], $parsedBody['error_description'])) {
throw new TokenExchangeFailedException(
'Failed to exchange token, client/server error in the exchange request: ' . $body,
0,
$e,
$parsedBody['error'],
$parsedBody['error_description'],
);
} else {
throw new TokenExchangeFailedException(
'Failed to exchange token, client/server error in the exchange request: ' . $body,
0,
$e,
);
}
} catch (\Exception|\Throwable $e) {
$this->logger->error('[TokenService] Failed to exchange token ', ['exception' => $e]);
throw new TokenExchangeFailedException('Failed to exchange token, error in the exchange request', 0, $e);
Expand Down

0 comments on commit fba2265

Please sign in to comment.