You are here

private function AuthenticationOAuthTest::setupAuthorizationPluginWithKey in Entity Share 8.3

Helper function: updates the existing OAuth plugin to use Key storage.

Parameters

\Drupal\user\UserInterface $account: The user whose credentials will be used for the plugin.

1 call to AuthenticationOAuthTest::setupAuthorizationPluginWithKey()
AuthenticationOAuthTest::testImport in modules/entity_share_client/tests/src/Functional/AuthenticationOAuthTest.php
Test that correct entities are created with different authentications.

File

modules/entity_share_client/tests/src/Functional/AuthenticationOAuthTest.php, line 305

Class

AuthenticationOAuthTest
Functional test class for import with "OAuth" authorization.

Namespace

Drupal\Tests\entity_share_client\Functional

Code

private function setupAuthorizationPluginWithKey(UserInterface $account) {
  $plugin = $this->remote
    ->getAuthPlugin();
  $configuration = $plugin
    ->getConfiguration();

  // To properly test, delete the cached key used in the previous run.
  if ($this->keyValueStore
    ->get($configuration['uuid'] . '-' . $plugin
    ->getPluginId()) instanceof AccessTokenInterface) {
    $this->keyValueStore
      ->delete($configuration['uuid'] . '-' . $plugin
      ->getPluginId());
  }

  // Obtain the access token from server again, but now we are using the
  // credentials saved in the Key.
  $credentials = $this->keyService
    ->getCredentials($plugin);
  $credentials['username'] = $account
    ->getAccountName();
  $credentials['password'] = $account->passRaw;
  $request_options = [
    RequestOptions::HTTP_ERRORS => FALSE,
    RequestOptions::ALLOW_REDIRECTS => [
      'strict' => TRUE,
    ],
  ];
  $access_token = '';
  try {
    $access_token = $plugin
      ->initializeToken($this->remote, $credentials, $request_options);
  } catch (\Exception $e) {

    // Do nothing.
  }

  // Since this is an important part of OAuth functionality,
  // assert that it is successful.
  $this
    ->assertNotEmpty($access_token, 'The new access token is not empty.');

  // Save the obtained key.
  $this->keyValueStore
    ->set($configuration['uuid'] . '-' . $plugin
    ->getPluginId(), $access_token);

  // Save the new configuration of the plugin.
  $configuration['data'] = [
    'credential_provider' => 'key',
    'storage_key' => 'key_oauth_' . $account
      ->id(),
  ];
  $plugin
    ->setConfiguration($configuration);

  // Save the "Remote" config entity.
  $this->remote
    ->mergePluginConfig($plugin);
  $this->remote
    ->save();
}