You are here

public function TokenTest::testSuccessfulRs256TokenDecoding in Auth0 Single Sign On 8.2

Test a successful RS256 token decoding.

Return value

void

Throws

\Exception See Auth0\SDK\JWTVerifier::verifyAndDecode().

File

vendor/auth0/auth0-php/tests/API/Helpers/TokenGeneratorTest.php, line 383

Class

TokenTest
Class TokenTest

Namespace

Auth0\Tests\Api\Helpers

Code

public function testSuccessfulRs256TokenDecoding() {

  // Mock the JWKFetcher object.
  $mocked_jwks = $this
    ->getMockBuilder(JWKFetcher::class)
    ->setMethods([
    'getKeys',
  ])
    ->getMock();
  $mocked_jwks
    ->method('getKeys')
    ->willReturn(uniqid());

  // Mock the JWT object.
  $expected_sub = uniqid();
  $verifier_args = [
    'valid_audiences' => [
      self::CLIENT_ID,
    ],
    'client_secret' => self::CLIENT_SECRET,
    'supported_algs' => [
      'RS256',
    ],
    'authorized_iss' => [
      '__valid_iss__',
    ],
    'jwks_path' => 'path/to/custom/jwks.json',
  ];
  $mocked_jwt = $this
    ->getMockBuilder(JWTVerifier::class)
    ->setConstructorArgs([
    $verifier_args,
    $mocked_jwks,
  ])
    ->setMethods([
    'decodeToken',
  ])
    ->getMock();
  $mocked_jwt
    ->method('decodeToken')
    ->willReturn((object) [
    'sub' => $expected_sub,
  ]);
  $head_obj = new \stdClass();
  $head_obj->typ = 'JWT';
  $head_obj->alg = 'RS256';
  $head_obj->kid = uniqid();
  $jwt_head = JWT::urlsafeB64Encode(JWT::jsonEncode($head_obj));
  $payload_obj = new \stdClass();
  $payload_obj->aud = self::CLIENT_ID;
  $payload_obj->iss = '__valid_iss__';
  $jwt_payload = JWT::urlsafeB64Encode(JWT::jsonEncode($payload_obj));
  $jwt = $jwt_head . '.' . $jwt_payload . '.' . uniqid();
  $decoded = $mocked_jwt
    ->verifyAndDecode($jwt);
  $this
    ->assertObjectHasAttribute('sub', $decoded);
  $this
    ->assertEquals($expected_sub, $decoded->sub);
}