You are here

public function OAuth2ServerTest::testJwtBearerGrantType in OAuth2 Server 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/OAuth2ServerTest.php \Drupal\Tests\oauth2_server\Functional\OAuth2ServerTest::testJwtBearerGrantType()

Tests the "JWT bearer" grant type.

File

tests/src/Functional/OAuth2ServerTest.php, line 269

Class

OAuth2ServerTest
The OAuth2 Server admin test case.

Namespace

Drupal\Tests\oauth2_server\Functional

Code

public function testJwtBearerGrantType() {
  $request_time = \Drupal::time()
    ->getRequestTime();
  $sub_property = \Drupal::config('oauth2_server.oauth')
    ->get('user_sub_property');
  $jwt_util = new Jwt();
  $user = $this
    ->drupalCreateUser([
    'use oauth2 server',
  ]);
  $this
    ->drupalLogin($user);
  $token_url = $this
    ->buildUrl(new Url('oauth2_server.token'));
  $jwt_data = [
    'iss' => $this->clientId,
    'exp' => $request_time + 1000,
    'iat' => $request_time,
    'sub' => $user->{$sub_property}->value,
    'aud' => $token_url,
    'jti' => '123456',
  ];
  $data = [
    'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion' => $jwt_util
      ->encode($jwt_data, $this->privateKey, 'RS256'),
  ];
  $response = $this
    ->httpPostRequest($token_url, $data, FALSE);
  $this
    ->assertEqual($response
    ->getStatusCode(), 200, 'The token request completed successfully');
  $payload = json_decode($response
    ->getBody());
  $this
    ->assertTokenResponse($payload, FALSE);
}