forked from Islandora/islandora
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More flexible headers for STOMP messages, theoretically.
- Loading branch information
1 parent
d7cd5db
commit fc1d1ef
Showing
9 changed files
with
254 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora\Event; | ||
|
||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\Core\Session\AccountInterface; | ||
|
||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Event used to build headers for STOMP. | ||
*/ | ||
class StompHeaderEvent implements StompHeaderEventInterface { | ||
|
||
/** | ||
* Stashed entity, for context. | ||
* | ||
* @var \Drupal\Core\Entity\EntityInterface | ||
*/ | ||
protected $entity; | ||
|
||
/** | ||
* Stashed user info, for context. | ||
* | ||
* @var \Drupal\Core\Session\AccountInterface | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* The set of headers. | ||
* | ||
* @var \Symfony\Component\HttpFoundation\ParameterBag | ||
*/ | ||
protected $headers; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(EntityInterface $entity, AccountInterface $user) { | ||
$this->entity = $entity; | ||
$this->user = $user; | ||
$this->headers = new ParameterBag(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getEntity() { | ||
return $this->entity; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getUser() { | ||
return $this->user; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getHeaders() { | ||
return $this->headers; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora\Event; | ||
|
||
/** | ||
* Typification for handling exceptions specific to STOMP header generation. | ||
*/ | ||
class StompHeaderEventException extends \Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora\Event; | ||
|
||
/** | ||
* Contract for representing an event to build headers for STOMP messages. | ||
*/ | ||
interface StompHeaderEventInterface { | ||
|
||
const EVENT_NAME = 'islandora.stomp.header_event'; | ||
|
||
/** | ||
* Get the headers being built for STOMP. | ||
* | ||
* XXX: Ironically, using ParameterBag instead of HeaderBag due to case- | ||
* sensitivity: In the context of HTTP, headers are case insensitive (and is | ||
* what HeaderBag is intended; however, STOMP headers are case sensitive. | ||
* | ||
* @return \Symfony\Component\HttpFoundation\ParameterBag | ||
* The headers | ||
*/ | ||
public function getHeaders(); | ||
|
||
/** | ||
* Fetch the entity provided as context. | ||
* | ||
* @return \Drupal\Core\Entity\EntityInterface | ||
* The entity provided as context. | ||
*/ | ||
public function getEntity(); | ||
|
||
/** | ||
* Fetch the user provided as context. | ||
* | ||
* @return \Drupal\Core\Session\AccountInterface | ||
* The user provided as context. | ||
*/ | ||
public function getUser(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora\EventSubscriber; | ||
|
||
use Drupal\islandora\Event\StompHeaderEventInterface; | ||
use Drupal\jwt\Authentication\Provider\JwtAuth; | ||
|
||
use Drupal\Core\Messenger\MessengerInterface; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Base STOMP header listener. | ||
*/ | ||
class StompHeaderEventSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* The JWT auth service. | ||
* | ||
* @var \Drupal\jwt\Authentication\Provider\JwtAuth | ||
*/ | ||
protected $auth; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct( | ||
JwtAuth $auth | ||
) { | ||
$this->auth = $auth; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
return [ | ||
StompHeaderEventInterface::EVENT_NAME => 'baseAuth', | ||
]; | ||
} | ||
|
||
/** | ||
* Event callback; generate and add base authorization header if none is set. | ||
*/ | ||
public function baseAuth(StompHeaderEventInterface $stomp_event) { | ||
$headers = $stomp_event->getHeaders(); | ||
if (!$headers->has('Authorization')) { | ||
$token = $this->auth->generateToken(); | ||
if (empty($token)) { | ||
// JWT does not seem to be properly configured. | ||
// phpcs:ignore DrupalPractice.General.ExceptionT.ExceptionT | ||
throw new StompHeaderEventException($this->t('Error getting JWT token for message. Check JWT Configuration.')); | ||
} | ||
else { | ||
$headers->set('Authorization', "Bearer $token"); | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.