You are here

public function TestController::authorized in OAuth2 Client 8

Authorized.

The oauth2 server will redirect to the registered redirect_uri, which is handled by the oauth2_client, but then oauth2_client will redirect to the path 'oauth2/test/authorized', which comes here. This is because we registered a redirect on the oauth2_client before jumping to $authentication_uri. While redirecting, oauth2_client will also append to the request the 'extra_param'.

File

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

Class

TestController
Class TestController.

Namespace

Drupal\oauth2_client_test\Controller

Code

public function authorized() {
  if (!\Drupal::csrfToken()
    ->validate($_GET['state'], 'test_client')) {
    return [
      '#markup' => "The parameter 'state' is wrong.\n",
    ];
  }
  $extra_param = $_GET['extra_param'];
  print "extra_param: {$extra_param} <br/>\n";
  $options = [
    'method' => 'POST',
    'data' => http_build_query([
      'grant_type' => 'authorization_code',
      'code' => $_GET['code'],
      'redirect_uri' => oauth2_client_get_redirect_uri(),
    ]),
    'headers' => [
      'Content-Type' => 'application/x-www-form-urlencoded',
      'Authorization' => 'Basic ' . base64_encode('client2:secret2'),
    ],
    'context' => stream_context_create([
      'ssl' => [
        'verify_peer' => FALSE,
        'verify_peer_name' => FALSE,
      ],
    ]),
  ];
  $token_endpoint = Url::fromUserInput('oauth2/token', [
    'absolute' => TRUE,
  ])
    ->toString();
  $result = \Drupal::httpClient()
    ->get($token_endpoint, $options);
  $token = json_decode($result
    ->getBody()
    ->getContents());
  return [
    '#markup' => 'access_token: ' . $token->access_token,
  ];
}