Browse Source

Make JWT Expiry configurable - Issue 1030 (#116)

* Make JWT expiry configurable using Islandora Core Setting form.

* coding standards
pull/729/head
Seth Shaw 6 years ago committed by dannylamb
parent
commit
fb1802b27d
  1. 6
      src/EventSubscriber/JwtEventSubscriber.php
  2. 20
      src/Form/IslandoraSettingsForm.php

6
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);

20
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);

Loading…
Cancel
Save