Skip to content

Commit

Permalink
Issue 69: Error handling in case the key is empty or not configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Dec 3, 2024
1 parent 9d90a50 commit bc8894a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/Controller/ViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>\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';
Expand All @@ -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');
Expand Down
9 changes: 8 additions & 1 deletion src/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 18 additions & 3 deletions src/WopiTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit bc8894a

Please sign in to comment.