Browse Source

Modifies Guzzle client handler to generate a new JWT for each request. (#119)

pull/729/head
Seth Shaw 6 years ago committed by dannylamb
parent
commit
f7f143754e
  1. 27
      src/Flysystem/Fedora.php

27
src/Flysystem/Fedora.php

@ -7,6 +7,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\flysystem\Plugin\FlysystemPluginInterface; use Drupal\flysystem\Plugin\FlysystemPluginInterface;
use Drupal\flysystem\Plugin\FlysystemUrlTrait; use Drupal\flysystem\Plugin\FlysystemUrlTrait;
use Drupal\islandora\Flysystem\Adapter\FedoraAdapter; use Drupal\islandora\Flysystem\Adapter\FedoraAdapter;
use Drupal\jwt\Authentication\Provider\JwtAuth;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Islandora\Chullo\IFedoraApi; use Islandora\Chullo\IFedoraApi;
@ -48,13 +49,10 @@ class Fedora implements FlysystemPluginInterface, ContainerFactoryPluginInterfac
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
// Construct Authorization header using jwt token.
$jwt = $container->get('jwt.authentication.jwt');
$auth = 'Bearer ' . $jwt->generateToken();
// Construct guzzle client to middleware that adds the header. // Construct guzzle client to middleware that adds JWT.
$stack = HandlerStack::create(); $stack = HandlerStack::create();
$stack->push(static::addHeader('Authorization', $auth)); $stack->push(static::addJwt($container->get('jwt.authentication.jwt')));
$client = new Client([ $client = new Client([
'handler' => $stack, 'handler' => $stack,
'base_uri' => $configuration['root'], 'base_uri' => $configuration['root'],
@ -71,22 +69,19 @@ class Fedora implements FlysystemPluginInterface, ContainerFactoryPluginInterfac
/** /**
* Guzzle middleware to add a header to outgoing requests. * Guzzle middleware to add a header to outgoing requests.
* *
* @param string $header * @param \Drupal\jwt\Authentication\Provider\JwtAuth $jwt
* Header name. * JWT.
* @param string $value
* Header value.
*/ */
public static function addHeader($header, $value) { public static function addJwt(JwtAuth $jwt) {
return function (callable $handler) use ($header, $value) { return function (callable $handler) use ($jwt) {
return function ( return function (
RequestInterface $request, RequestInterface $request,
array $options array $options
) use ( ) use (
$handler, $handler,
$header, $jwt
$value ) {
) { $request = $request->withHeader('Authorization', 'Bearer ' . $jwt->generateToken());
$request = $request->withHeader($header, $value);
return $handler($request, $options); return $handler($request, $options);
}; };
}; };

Loading…
Cancel
Save