public function RsaKeyTest::testRsaGenerateToken in JSON Web Token Authentication (JWT) 8
Verify generation and verification with RSA.
File
- tests/
src/ Kernel/ RsaKeyTest.php, line 38
Class
- RsaKeyTest
- Tests RSA keys.
Namespace
Drupal\Tests\jwt\KernelCode
public function testRsaGenerateToken() {
$config = $this
->config('jwt.config');
$config
->set('algorithm', 'RS256');
$config
->set('key_id', 'jwt_test_rsa');
$config
->save();
$account = $this
->createUser([
'access content',
]);
$this
->setCurrentUser($account);
$auth = $this->container
->get('jwt.authentication.jwt');
$token = $auth
->generateToken();
$this
->assertNotEmpty($token);
/** @var \Drupal\jwt\Transcoder\JwtTranscoderInterface $transcoder */
$transcoder = $this->container
->get('jwt.transcoder');
$decoded_jwt = $transcoder
->decode($token);
$this
->assertEqual($account
->id(), $decoded_jwt
->getClaim([
'drupal',
'uid',
]));
// Test decoding with the matched and mis-matched public keys.
$path = drupal_get_path('module', 'jwt_test') . '/fixtures/jwt_test_rsa-public.pem';
$public_key = file_get_contents($path);
$payload = JWT::decode($token, $public_key, [
'RS256',
]);
$this
->assertEqual($account
->id(), $payload->drupal->uid);
$path = drupal_get_path('module', 'jwt_test') . '/fixtures/jwt_test_rsa2-public.pem';
$public_key = file_get_contents($path);
$this
->expectException(SignatureInvalidException::class);
$payload = JWT::decode($token, $public_key, [
'RS256',
]);
}