JwtTranscoder.php in JSON Web Token Authentication (JWT) 8.0
File
src/Transcoder/JwtTranscoder.php
View source
<?php
namespace Drupal\jwt\Transcoder;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\jwt\JsonWebToken\JsonWebToken;
use Drupal\jwt\JsonWebToken\JsonWebTokenInterface;
use Drupal\key\KeyRepositoryInterface;
use Firebase\JWT\JWT;
class JwtTranscoder implements JwtTranscoderInterface {
protected $transcoder;
protected $algorithm;
protected $algorithmType;
protected $secret = NULL;
protected $privateKey = NULL;
protected $publicKey = NULL;
public static function getAlgorithmOptions() {
return [
'HS256' => 'HMAC using SHA-256 (HS256)',
'HS384' => 'HMAC using SHA-384 (HS384)',
'HS512' => 'HMAC using SHA-512 (HS512)',
'RS256' => 'RSASSA-PKCS1-v1_5 using SHA-256 (RS256)',
];
}
public static function getAlgorithmType($algorithm) {
switch ($algorithm) {
case 'HS256':
case 'HS384':
case 'HS512':
return 'jwt_hs';
case 'RS256':
return 'jwt_rs';
default:
return NULL;
}
}
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') {
$this
->setSecret($key_value);
}
elseif ($this->algorithmType == 'jwt_rs') {
$this
->setPrivateKey($key_value);
}
}
}
}
public function setSecret($secret) {
$this->secret = $secret;
}
public function setAlgorithm($algorithm) {
$this->algorithm = $algorithm;
$this->algorithmType = $this
->getAlgorithmType($algorithm);
}
public function setPrivateKey($private_key, $derive_public_key = TRUE) {
$key_context = openssl_pkey_get_private($private_key);
$key_details = openssl_pkey_get_details($key_context);
if ($key_details === FALSE || $key_details['type'] != OPENSSL_KEYTYPE_RSA) {
return FALSE;
}
$this->privateKey = $private_key;
if ($derive_public_key) {
$this->publicKey = $key_details['key'];
}
return TRUE;
}
public function setPublicKey($public_key) {
$key_context = openssl_pkey_get_public($public_key);
$key_details = openssl_pkey_get_details($key_context);
if ($key_details === FALSE || $key_details['type'] != OPENSSL_KEYTYPE_RSA) {
return FALSE;
}
$this->publicKey = $public_key;
return TRUE;
}
public function decode($jwt) {
$key = $this
->getKey('decode');
$algorithms = [
$this->algorithm,
];
try {
$token = $this->transcoder
->decode($jwt, $key, $algorithms);
} catch (\Exception $e) {
throw JwtDecodeException::newFromException($e);
}
return new JsonWebToken($token);
}
public function encode(JsonWebTokenInterface $jwt) {
$key = $this
->getKey('encode');
if ($key === NULL) {
return FALSE;
}
$encoded = $this->transcoder
->encode($jwt
->getPayload(), $key, $this->algorithm);
return $encoded;
}
protected function getKey($operation) {
if ($this->algorithmType == 'jwt_hs') {
return $this->secret;
}
elseif ($this->algorithmType == 'jwt_rs') {
if ($operation == 'encode') {
return $this->privateKey;
}
elseif ($operation == 'decode') {
return $this->publicKey;
}
}
return NULL;
}
}