Skip to content

Commit

Permalink
Issue CollaboraOnline#56: Inject services into CoolJwt to avoid Drupa…
Browse files Browse the repository at this point in the history
…l:: calls.
  • Loading branch information
donquixote committed Nov 14, 2024
1 parent c3139c7 commit c70cf32
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/Service/CoolJwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,48 @@

namespace Drupal\collabora_online\Service;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\key\KeyRepositoryInterface;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
* Service with functionality related to the JWT token.
*/
class CoolJwt {

/**
* Constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory service.
* @param \Drupal\key\KeyRepositoryInterface $keyRepository
* Key repository service.
* @param \Psr\Log\LoggerInterface $logger
* Logger channel for this module.
*/
public function __construct(
protected readonly ConfigFactoryInterface $configFactory,
protected readonly KeyRepositoryInterface $keyRepository,
#[Autowire(service: 'logger.channel.collabora_online')]
protected readonly LoggerInterface $logger,
protected readonly AccountInterface $currentUser,
) {}

/**
* Obtains the signing key from the key storage.
*
* @return string
* The key value.
*/
public function getKey() {
$default_config = \Drupal::config('collabora_online.settings');
$default_config = $this->configFactory->get('collabora_online.settings');
$key_id = $default_config->get('cool')['key_id'];

$key = \Drupal::service('key.repository')->getKey($key_id)->getKeyValue();
$key = $this->keyRepository->getKey($key_id)->getKeyValue();
return $key;
}

Expand Down Expand Up @@ -57,7 +80,7 @@ public function verifyTokenForId(
}
}
catch (\Exception $e) {
\Drupal::logger('cool')->error($e->getMessage());
$this->logger->error($e->getMessage());
}
return NULL;
}
Expand Down Expand Up @@ -88,7 +111,7 @@ public function verifyTokenForId(
public function tokenForFileId($id, $ttl, $can_write = FALSE) {
$payload = [
"fid" => $id,
"uid" => \Drupal::currentUser()->id(),
"uid" => $this->currentUser->id(),
"exp" => $ttl,
"wri" => $can_write,
];
Expand All @@ -105,7 +128,7 @@ public function tokenForFileId($id, $ttl, $can_write = FALSE) {
* Token TTL in seconds.
*/
public function getAccessTokenTtl() {
$default_config = \Drupal::config('collabora_online.settings');
$default_config = $this->configFactory->get('collabora_online.settings');
$ttl = $default_config->get('cool')['access_token_ttl'];

return gettimeofday(TRUE) + $ttl;
Expand Down

0 comments on commit c70cf32

Please sign in to comment.