diff --git a/modules/jwt_auth_issuer/config/install/jwt_auth_issuer.config.yml b/modules/jwt_auth_issuer/config/install/jwt_auth_issuer.config.yml new file mode 100644 index 0000000..405de12 --- /dev/null +++ b/modules/jwt_auth_issuer/config/install/jwt_auth_issuer.config.yml @@ -0,0 +1 @@ +max_expiration: 86400 diff --git a/modules/jwt_auth_issuer/config/schema/jwt_auth_issuer.schema.yml b/modules/jwt_auth_issuer/config/schema/jwt_auth_issuer.schema.yml new file mode 100644 index 0000000..4d26297 --- /dev/null +++ b/modules/jwt_auth_issuer/config/schema/jwt_auth_issuer.schema.yml @@ -0,0 +1,7 @@ +jwt_auth_issuer.config: + type: config_object + label: 'JWT Issuer Authentication settings' + mapping: + max_expiration: + type: integer + label: 'The maximum allowed lifetime for a JWT (seconds).' diff --git a/modules/jwt_auth_issuer/jwt_auth_issuer.links.task.yml b/modules/jwt_auth_issuer/jwt_auth_issuer.links.task.yml new file mode 100644 index 0000000..d44d358 --- /dev/null +++ b/modules/jwt_auth_issuer/jwt_auth_issuer.links.task.yml @@ -0,0 +1,5 @@ +jwt_auth_issuer.config_form: + title: 'JWT Issuer Configuration' + route_name: jwt_auth_issuer.config_form + base_route: jwt.jwt_config_form + weight: 99 diff --git a/modules/jwt_auth_issuer/jwt_auth_issuer.routing.yml b/modules/jwt_auth_issuer/jwt_auth_issuer.routing.yml index 4683644..bf9cc4c 100644 --- a/modules/jwt_auth_issuer/jwt_auth_issuer.routing.yml +++ b/modules/jwt_auth_issuer/jwt_auth_issuer.routing.yml @@ -7,3 +7,13 @@ jwt_auth_issuer.jwt_auth_issuer_controller_generateToken: _auth: [ 'jwt_auth', 'basic_auth', 'cookie' ] requirements: _user_is_logged_in: 'TRUE' + +jwt_auth_issuer.config_form: + path: '/admin/config/system/jwt/auth-issuer' + defaults: + _form: '\Drupal\jwt_auth_issuer\Form\IssuerConfigForm' + _title: 'JWT Issuer Configuration' + requirements: + _permission: 'administer jwt' + options: + _admin_route: TRUE diff --git a/modules/jwt_auth_issuer/jwt_auth_issuer.services.yml b/modules/jwt_auth_issuer/jwt_auth_issuer.services.yml index fb7b00b..92a7427 100644 --- a/modules/jwt_auth_issuer/jwt_auth_issuer.services.yml +++ b/modules/jwt_auth_issuer/jwt_auth_issuer.services.yml @@ -1,6 +1,6 @@ services: jwt_auth_issuer.subscriber: class: Drupal\jwt_auth_issuer\EventSubscriber\JwtAuthIssuerSubscriber - arguments: [ '@current_user' ] + arguments: [ '@current_user', '@config.factory' ] tags: - { name: event_subscriber } diff --git a/modules/jwt_auth_issuer/src/EventSubscriber/JwtAuthIssuerSubscriber.php b/modules/jwt_auth_issuer/src/EventSubscriber/JwtAuthIssuerSubscriber.php index 4f34524..342a25d 100644 --- a/modules/jwt_auth_issuer/src/EventSubscriber/JwtAuthIssuerSubscriber.php +++ b/modules/jwt_auth_issuer/src/EventSubscriber/JwtAuthIssuerSubscriber.php @@ -2,6 +2,7 @@ namespace Drupal\jwt_auth_issuer\EventSubscriber; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Session\AccountInterface; use Drupal\jwt\Authentication\Event\JwtAuthEvents; use Drupal\jwt\Authentication\Event\JwtAuthGenerateEvent; @@ -21,14 +22,24 @@ class JwtAuthIssuerSubscriber implements EventSubscriberInterface { */ protected $currentUser; + /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + /** * Constructor. * * @param \Drupal\Core\Session\AccountInterface $user * The current user. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. */ - public function __construct(AccountInterface $user) { + public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory) { $this->currentUser = $user; + $this->configFactory = $config_factory; } /** @@ -47,9 +58,12 @@ class JwtAuthIssuerSubscriber implements EventSubscriberInterface { * The event. */ public function setStandardClaims(JwtAuthGenerateEvent $event) { + $config = $this->configFactory->get('jwt_auth_issuer.config'); $event->addClaim('iat', time()); - // @todo: make these more configurable. - $event->addClaim('exp', strtotime('+1 hour')); + $expiration = $config->get('max_expiration'); + if ($expiration > 0) { + $event->addClaim('exp', time() + $expiration); + } } /** diff --git a/modules/jwt_auth_issuer/src/Form/IssuerConfigForm.php b/modules/jwt_auth_issuer/src/Form/IssuerConfigForm.php new file mode 100644 index 0000000..d3f59e6 --- /dev/null +++ b/modules/jwt_auth_issuer/src/Form/IssuerConfigForm.php @@ -0,0 +1,64 @@ + $this->t('Configure the JWT issuer.'), + ]; + + $form['max_expiration'] = [ + '#type' => 'number', + '#required' => TRUE, + '#title' => $this->t('The maximum allowed lifetime for a JWT (seconds)'), + '#default_value' => $this->config('jwt_auth_issuer.config')->get('max_expiration'), + '#description' => $this->t('Use 0 to not use expiration at all.'), + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + + $values = $form_state->getValues(); + + if (isset($values['max_expiration'])) { + $this->config('jwt_auth_issuer.config')->set('max_expiration', $values['max_expiration'])->save(); + }; + } + +} diff --git a/tests/src/Functional/JwtAuthIssuerTest.php b/tests/src/Functional/JwtAuthIssuerTest.php new file mode 100644 index 0000000..554aeab --- /dev/null +++ b/tests/src/Functional/JwtAuthIssuerTest.php @@ -0,0 +1,190 @@ +adminUser = $this->drupalCreateUser([ + 'administer jwt', + ]); + + $this->user = $this->drupalCreateUser([ + 'access content', + ]); + } + + /** + * Test admin form updates and authentication with unlimited expiration. + */ + public function testUnlimitedExpiration() { + $assertSession = $this->assertSession(); + $this->drupalLogin($this->adminUser); + + // Set jwt token expiration to 0, which is unlimited. + $url = Url::fromRoute('jwt_auth_issuer.config_form'); + $this->drupalGet($url); + $edit = [ + 'max_expiration' => 0, + ]; + $this->submitForm($edit, 'Save configuration'); + $assertSession->pageTextContains('The configuration options have been saved. '); + + /** @var \Drupal\jwt\Transcoder\JwtTranscoderInterface $transcoder */ + $transcoder = $this->container->get('jwt.transcoder'); + $this->drupalLogin($this->user); + + /** @var \Drupal\jwt\Authentication\Provider\JwtAuth $auth */ + $auth = $this->container->get('jwt.authentication.jwt'); + $token = $auth->generateToken(); + $decoded_jwt = $transcoder->decode($token); + $this->assertEquals($this->user->id(), $decoded_jwt->getClaim([ + 'drupal', + 'uid', + ])); + foreach (['jwt_test.11.1', 'jwt_test.11.2'] as $route_name) { + $url = Url::fromRoute($route_name); + foreach (['Authorization', 'JWT-Authorization'] as $header_name) { + $headers = [ + $header_name => 'Bearer ' . $token, + ]; + $this->drupalGet($url, [], $headers); + $assertSession->statusCodeEquals(200); + $assertSession->pageTextContains($this->user->getAccountName()); + $this->assertNull($this->getSession() + ->getResponseHeader('X-Drupal-Cache')); + $this->assertFalse(strpos($this->getSession() + ->getResponseHeader('Cache-Control'), 'public'), 'Cache-Control is not set to public'); + $this->user->block()->save(); + $this->drupalGet($url, [], $headers); + $this->assertSession()->statusCodeEquals(403); + $this->user->activate()->save(); + + // This is needed to prevent the Authorization header from the last loop + // being sent again by the mink session. + $this->mink->resetSessions(); + $headers = [ + $header_name => 'Bearer ' . $this->randomMachineName(), + ]; + $this->drupalGet($url, [], $headers); + + // Bad jwt token does not authenticate the user. + $assertSession->pageTextNotContains($this->user->getAccountName()); + $assertSession->statusCodeEquals(403); + $this->mink->resetSessions(); + } + } + } + + /** + * Test admin form updates and authentication with limited expiration. + */ + public function testLimitedExpiration() { + $assertSession = $this->assertSession(); + $this->drupalLogin($this->adminUser); + + // Set jwt token expiration to 1 second. + $url = Url::fromRoute('jwt_auth_issuer.config_form'); + $this->drupalGet($url); + $edit = [ + 'max_expiration' => 1, + ]; + $this->submitForm($edit, 'Save configuration'); + $assertSession->pageTextContains('The configuration options have been saved. '); + + /** @var \Drupal\jwt\Transcoder\JwtTranscoderInterface $transcoder */ + $transcoder = $this->container->get('jwt.transcoder'); + $this->drupalLogin($this->user); + + /** @var \Drupal\jwt\Authentication\Provider\JwtAuth $auth */ + $auth = $this->container->get('jwt.authentication.jwt'); + $token = $auth->generateToken(); + $decoded_jwt = $transcoder->decode($token); + $this->assertEquals($this->user->id(), $decoded_jwt->getClaim([ + 'drupal', + 'uid', + ])); + + // Sleep for 1 second to make sure the JWT token has expired. + sleep(1); + + foreach (['jwt_test.11.1', 'jwt_test.11.2'] as $route_name) { + $url = Url::fromRoute($route_name); + foreach (['Authorization', 'JWT-Authorization'] as $header_name) { + $headers = [ + $header_name => 'Bearer ' . $token, + ]; + $this->drupalGet($url, [], $headers); + $assertSession->statusCodeEquals(403); + $this->assertNull($this->getSession() + ->getResponseHeader('X-Drupal-Cache')); + $this->assertFalse(strpos($this->getSession() + ->getResponseHeader('Cache-Control'), 'public'), 'Cache-Control is not set to public'); + $this->user->block()->save(); + $this->drupalGet($url, [], $headers); + $this->assertSession()->statusCodeEquals(403); + $this->user->activate()->save(); + + // This is needed to prevent the Authorization header from the last loop + // being sent again by the mink session. + $this->mink->resetSessions(); + $headers = [ + $header_name => 'Bearer ' . $this->randomMachineName(), + ]; + $this->drupalGet($url, [], $headers); + + // Bad jwt token does not authenticate the user. + $assertSession->pageTextNotContains($this->user->getAccountName()); + $assertSession->statusCodeEquals(403); + $this->mink->resetSessions(); + } + } + } + +}