You are here

public function RsaKeyTest::testRsaPublicDecodeToken in JSON Web Token Authentication (JWT) 8

Verification with RSA public key only.

File

tests/src/Kernel/RsaKeyTest.php, line 66

Class

RsaKeyTest
Tests RSA keys.

Namespace

Drupal\Tests\jwt\Kernel

Code

public function testRsaPublicDecodeToken() {
  $config = $this
    ->config('jwt.config');
  $config
    ->set('algorithm', 'RS256');
  $config
    ->set('key_id', 'jwt_test_rsa2');
  $config
    ->save();
  $path = drupal_get_path('module', 'jwt_test') . '/fixtures/jwt_test_rsa2-private.pem';
  $private_key = file_get_contents($path);
  $exp = \Drupal::time()
    ->getRequestTime() + 1000;
  $payload = [
    'exp' => $exp,
    'test' => [
      'uid' => 999,
    ],
  ];
  $token = JWT::encode($payload, $private_key, 'RS256', 'wxyz');
  $this
    ->assertNotEmpty($token);

  /** @var \Drupal\jwt\Transcoder\JwtTranscoderInterface $transcoder */
  $transcoder = $this->container
    ->get('jwt.transcoder');
  $decoded_jwt = $transcoder
    ->decode($token);
  $this
    ->assertEqual(999, $decoded_jwt
    ->getClaim([
    'test',
    'uid',
  ]));
  $this
    ->assertEqual($exp, $decoded_jwt
    ->getClaim('exp'));
}