Skip to content

Commit

Permalink
Merge pull request #1 from jonathangreen/issue-2776737
Browse files Browse the repository at this point in the history
Read key name from module configuration
  • Loading branch information
gabesullice committed Feb 9, 2017
2 parents 02b3686 + d588fd2 commit 56055ce
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions modules/jwt_auth_issuer/src/Controller/JwtAuthIssuerController.php
Expand Up @@ -45,20 +45,31 @@ class JwtAuthIssuerController extends ControllerBase {
*/
protected $eventDispatcher;

/**
* The secret to be used for the JWT.
*
* @var string
*/
protected $secret;

/**
* {@inheritdoc}
*
* @param \Drupal\jwt\Transcoder\JwtTranscoderInterface
* The JWT transcoder service.
* @param string $secret
* The secret to be used for the JWT.
* @param \Drupal\key\KeyRepositoryInterface $keyRepo
* The key module repository service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher service.
*/
public function __construct(JwtTranscoderInterface $transcoder, KeyRepositoryInterface $key_repo, EventDispatcherInterface $event_dispatcher) {
public function __construct(JwtTranscoderInterface $transcoder, $secret, KeyRepositoryInterface $key_repo, EventDispatcherInterface $event_dispatcher) {
$this->transcoder = $transcoder;
$this->keyRepo = $key_repo;
$this->eventDispatcher = $event_dispatcher;
$this->secret = $secret;
$this->transcoder->setSecret($this->secret);
}

/**
Expand All @@ -67,12 +78,13 @@ public function __construct(JwtTranscoderInterface $transcoder, KeyRepositoryInt
public static function create(ContainerInterface $container) {
$transcoder = $container->get('jwt.transcoder');
$key_repo = $container->get('key.repository');

$secret = $key_repo->getKey('jwt_key')->getKeyValue();
$transcoder->setSecret($secret);
$key_name = $container->get('config.factory')->get('jwt.config')->get('key_id');
$key = $key_repo->getKey($key_name);
$secret = $key ? $key->getKeyValue() : NULL;

return new static(
$transcoder,
$secret,
$key_repo,
$container->get('event_dispatcher')
);
Expand All @@ -86,7 +98,14 @@ public static function create(ContainerInterface $container) {
*/
public function tokenResponse() {
$response = new \stdclass();

if($this->secret === NULL) {
$response->error = "Please set a key in the JWT admin page.";
return new JsonResponse($response, 500);
}

$response->token = $this->generateToken();

return new JsonResponse($response);
}

Expand Down

0 comments on commit 56055ce

Please sign in to comment.