You are here

public function TestController::clientIntegration in OAuth2 Client 8

Client Integration.

Use the client 'client2' for getting an authorization code. This is done with the help of the module oauth2_client, because 'client2' is registered for it (its return_uri belongs to oauth2_client). Before jumping to $authentication_uri, register an internal redirect with oauth2_client.

Try it by opening in browser:

  • $base_url/oauth2/test-client-integration

File

tests/oauth2_client_test/src/Controller/TestController.php, line 59

Class

TestController
Class TestController.

Namespace

Drupal\oauth2_client_test\Controller

Code

public function clientIntegration() {
  $state = \Drupal::csrfToken()
    ->get('test_client');
  oauth2_client_set_redirect($state, [
    'uri' => 'oauth2/test-authorized',
    'params' => [
      'extra_param' => 'This will be appended to the request on redirect.',
    ],
  ]);
  $query_params = [
    'response_type' => 'code',
    'client_id' => 'client2',
    'redirect_uri' => oauth2_client_get_redirect_uri(),
    'state' => $state,
  ];
  $endpoint = Url::fromUserInput('oauth2/authorize', [
    'absolute' => TRUE,
  ])
    ->toString();
  $authentication_uri = $endpoint . '?' . http_build_query($query_params);
  return new RedirectResponse($authentication_uri);
}