From bc8894a7873a4e0556f2b16042ef81ef56680ad7 Mon Sep 17 00:00:00 2001 From: Andreas Hennings Date: Tue, 3 Dec 2024 23:32:33 +0100 Subject: [PATCH] Issue 69: Error handling in case the key is empty or not configured. --- src/Controller/ViewerController.php | 18 +++++++++++++++++- src/Controller/WopiController.php | 9 ++++++++- src/WopiTokenManager.php | 21 ++++++++++++++++++--- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/Controller/ViewerController.php b/src/Controller/ViewerController.php index f7b85e81..e7cbf423 100644 --- a/src/Controller/ViewerController.php +++ b/src/Controller/ViewerController.php @@ -95,7 +95,20 @@ public function editor(MediaInterface $media, Request $request, $edit = FALSE) { ); } - $render_array = $this->getViewerRender($media, $wopi_client_url, $edit, $options); + try { + $render_array = $this->getViewerRender($media, $wopi_client_url, $edit, $options); + } + catch (CollaboraNotAvailableException $e) { + $this->getLogger('cool')->warning( + "Cannot show the viewer/editor.
\n" . Error::DEFAULT_ERROR_MESSAGE, + Error::decodeException($e) + [], + ); + return new Response( + (string) $this->t('The Collabora Online editor/viewer is not available.'), + Response::HTTP_BAD_REQUEST, + ['content-type' => 'text/plain'], + ); + } $render_array['#theme'] = 'collabora_online_full'; $render_array['#attached']['library'][] = 'collabora_online/cool.frame'; @@ -122,6 +135,9 @@ public function editor(MediaInterface $media, Request $request, $edit = FALSE) { * * @return array|array{error: string} * A stub render element array, or an array with an error on failure. + * + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException + * The key to use by Collabora is empty or not configured. */ protected function getViewerRender(MediaInterface $media, string $wopi_client, bool $can_write, $options = NULL) { $cool_settings = $this->config('collabora_online.settings')->get('cool'); diff --git a/src/Controller/WopiController.php b/src/Controller/WopiController.php index 49ec06f8..e21ac30d 100644 --- a/src/Controller/WopiController.php +++ b/src/Controller/WopiController.php @@ -12,6 +12,7 @@ namespace Drupal\collabora_online\Controller; +use Drupal\collabora_online\Exception\CollaboraNotAvailableException; use Drupal\collabora_online\MediaHelper; use Drupal\collabora_online\WopiTokenManager; use Drupal\Component\Datetime\TimeInterface; @@ -327,7 +328,13 @@ protected function verifyTokenForMediaId( string $token, int|string $expected_media_id, ): array|null { - $values = $this->tokenManager->decodeAndVerify($token); + try { + $values = $this->tokenManager->decodeAndVerify($token); + } + catch (CollaboraNotAvailableException $e) { + $this->getLogger('cool')->warning('A token cannot be decoded: @message', ['@mesage' => $e->getMessage()]); + return NULL; + } if ($values === NULL) { return NULL; } diff --git a/src/WopiTokenManager.php b/src/WopiTokenManager.php index ffeabc76..d69f2bbb 100644 --- a/src/WopiTokenManager.php +++ b/src/WopiTokenManager.php @@ -14,6 +14,7 @@ namespace Drupal\collabora_online; +use Drupal\collabora_online\Exception\CollaboraNotAvailableException; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Session\AccountInterface; use Drupal\key\KeyRepositoryInterface; @@ -61,6 +62,9 @@ public function __construct( * @return array|null * Data decoded from the token, or NULL on failure or if the token has * expired. + * + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException + * The key to use by Collabora is empty or not configured. */ public function decodeAndVerify( #[\SensitiveParameter] @@ -106,6 +110,9 @@ public function decodeAndVerify( * The access token. * * @param-out int|float $expire_timestamp_reference + * + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException + * The key to use by Collabora is empty or not configured. */ public function encode(array $payload, int|float|null &$expire_timestamp_reference = NULL): string { $expire_timestamp_reference = $this->getExpireTimestamp(); @@ -136,12 +143,20 @@ protected function getExpireTimestamp(): float { * * @return string * The key value. + * + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException + * The key to use by Collabora is empty or not configured. */ protected function getKey(): string { $default_config = $this->configFactory->get('collabora_online.settings'); - $key_id = $default_config->get('cool')['key_id']; - - $key = $this->keyRepository->getKey($key_id)->getKeyValue(); + $key_id = $default_config->get('cool')['key_id'] ?? ''; + if (!$key_id) { + throw new CollaboraNotAvailableException('No key was chosen for use in Collabora.'); + } + $key = $this->keyRepository->getKey($key_id)?->getKeyValue(); + if (!$key) { + throw new CollaboraNotAvailableException(sprintf("The key with id '%s' is empty or does not exist.", $key_id)); + } return $key; }