You are here

public function JwtTranscoder::__construct in JSON Web Token Authentication (JWT) 8

Same name and namespace in other branches
  1. 8.0 src/Transcoder/JwtTranscoder.php \Drupal\jwt\Transcoder\JwtTranscoder::__construct()

Constructs a new JwtTranscoder.

Parameters

\Firebase\JWT\JWT $php_jwt: The JWT library object.

Drupal\Core\Config\ConfigFactoryInterface $configFactory: Drupal config factory to retrieve the configuration information.

Drupal\key\KeyRepositoryInterface $key_repo: The Key repository to retrieve the key.

File

src/Transcoder/JwtTranscoder.php, line 100

Class

JwtTranscoder
Class JwtTranscoder.

Namespace

Drupal\jwt\Transcoder

Code

public function __construct(JWT $php_jwt, ConfigFactoryInterface $configFactory, KeyRepositoryInterface $key_repo) {
  $this->transcoder = $php_jwt;
  $key_id = $configFactory
    ->get('jwt.config')
    ->get('key_id');
  $this
    ->setAlgorithm($configFactory
    ->get('jwt.config')
    ->get('algorithm'));
  if (isset($key_id)) {
    $key = $key_repo
      ->getKey($key_id);
    if (!is_null($key)) {
      $key_value = $key
        ->getKeyValue();
      if ($this->algorithmType == 'jwt_hs') {

        // Symmetric algorithm so we set the secret.
        $this
          ->setSecret($key_value);
      }
      elseif ($this->algorithmType == 'jwt_rs') {

        // Asymmetric algorithm so we set the private key if possible.
        if (strpos($key_value, '-----BEGIN PUBLIC KEY-----') !== FALSE) {
          $this
            ->setPublicKey($key_value);
        }
        else {
          $this
            ->setPrivateKey($key_value);
        }
      }
    }
  }
}