diff --git a/src/EventSubscriber/JwtEventSubscriber.php b/src/EventSubscriber/JwtEventSubscriber.php index aadc35fb..4ea049c4 100644 --- a/src/EventSubscriber/JwtEventSubscriber.php +++ b/src/EventSubscriber/JwtEventSubscriber.php @@ -2,6 +2,7 @@ namespace Drupal\islandora\EventSubscriber; +use Drupal\islandora\Form\IslandoraSettingsForm; use Drupal\jwt\Authentication\Event\JwtAuthValidateEvent; use Drupal\jwt\Authentication\Event\JwtAuthValidEvent; use Drupal\jwt\Authentication\Event\JwtAuthGenerateEvent; @@ -88,7 +89,10 @@ class JwtEventSubscriber implements EventSubscriberInterface { // Standard claims, validated at JWT validation time. $event->addClaim('iat', time()); - $event->addClaim('exp', strtotime('+2 hour')); + $expiry_setting = \Drupal::config(IslandoraSettingsForm::CONFIG_NAME) + ->get(IslandoraSettingsForm::JWT_EXPIRY); + $expiry = $expiry_setting ? $expiry_setting : '+2 hour'; + $event->addClaim('exp', strtotime($expiry)); $event->addClaim('webid', $this->currentUser->id()); $event->addClaim('iss', $base_secure_url); diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php index 60859782..730c1d25 100644 --- a/src/Form/IslandoraSettingsForm.php +++ b/src/Form/IslandoraSettingsForm.php @@ -15,6 +15,7 @@ class IslandoraSettingsForm extends ConfigFormBase { const CONFIG_NAME = 'islandora.settings'; const BROKER_URL = 'broker_url'; + const JWT_EXPIRY = 'jwt_expiry'; /** * {@inheritdoc} @@ -44,6 +45,12 @@ class IslandoraSettingsForm extends ConfigFormBase { '#default_value' => $config->get(self::BROKER_URL) ? $config->get(self::BROKER_URL) : 'tcp://localhost:61613', ]; + $form[self::JWT_EXPIRY] = [ + '#type' => 'textfield', + '#title' => $this->t('JWT Expiry'), + '#default_value' => $config->get(self::JWT_EXPIRY) ? $config->get(self::JWT_EXPIRY) : '+2 hour', + ]; + return parent::buildForm($form, $form_state); } @@ -74,6 +81,18 @@ class IslandoraSettingsForm extends ConfigFormBase { ) ); } + + // Validate jwt expiry as a valid time string. + $expiry = $form_state->getValue(self::JWT_EXPIRY); + if (strtotime($expiry) === FALSE) { + $form_state->setErrorByName( + self::JWT_EXPIRY, + $this->t( + '"@exipry" is not a valid time or interval expression.', + ['@expiry' => $expiry] + ) + ); + } } /** @@ -84,6 +103,7 @@ class IslandoraSettingsForm extends ConfigFormBase { $config ->set(self::BROKER_URL, $form_state->getValue(self::BROKER_URL)) + ->set(self::JWT_EXPIRY, $form_state->getValue(self::JWT_EXPIRY)) ->save(); parent::submitForm($form, $form_state);