You are here

public function OAuth2ServerTest::testOpenIdConnectAuthorizationCodeFlow in OAuth2 Server 8

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

Tests the OpenID Connect authorization code flow.

File

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

Class

OAuth2ServerTest
The OAuth2 Server admin test case.

Namespace

Drupal\Tests\oauth2_server\Functional

Code

public function testOpenIdConnectAuthorizationCodeFlow() {
  $user = $this
    ->drupalCreateUser([
    'use oauth2 server',
  ]);
  $this
    ->drupalLogin($user);

  // Perform authorization without the offline_access scope.
  // No refresh_token should be returned from the /token endpoint.
  $response = $this
    ->authorizationCodeRequest('code', 'openid');
  $redirect_url_params = $this
    ->getRedirectParams($response);
  $authorization_code = $redirect_url_params['code'];
  $token_url = $this
    ->buildUrl(new Url('oauth2_server.token'));
  $data = [
    'grant_type' => 'authorization_code',
    'code' => $authorization_code,
    'redirect_uri' => $this->redirectUri,
  ];
  $response = $this
    ->httpPostRequest($token_url, $data);
  $this
    ->assertEqual($response
    ->getStatusCode(), 200, 'The token request completed successfully');
  $payload = json_decode($response
    ->getBody());
  $this
    ->assertTokenResponse($payload, FALSE);
  if (!empty($payload->id_token)) {
    $this
      ->assertIdToken($payload->id_token);
  }
  else {
    $this
      ->assertTrue(FALSE, 'The token request returned an id_token.');
  }

  // Perform authorization witho the offline_access scope.
  // A refresh_token should be returned from the /token endpoint.
  $response = $this
    ->authorizationCodeRequest('code', 'openid offline_access');
  $redirect_url_params = $this
    ->getRedirectParams($response);
  $authorization_code = $redirect_url_params['code'];
  $token_url = $this
    ->buildUrl(new Url('oauth2_server.token'));
  $data = [
    'grant_type' => 'authorization_code',
    'code' => $authorization_code,
    'redirect_uri' => $this->redirectUri,
  ];
  $response = $this
    ->httpPostRequest($token_url, $data);
  $this
    ->assertEqual($response
    ->getStatusCode(), 200, 'The token request completed successfully');
  $payload = json_decode($response
    ->getBody());
  $this
    ->assertTokenResponse($payload);
  if (!empty($payload->id_token)) {
    $this
      ->assertIdToken($payload->id_token);
  }
  else {
    $this
      ->assertTrue(FALSE, 'The token request returned an id_token.');
  }
}