You are here

protected function AuthenticationOAuthTest::createAuthenticationPlugin in Entity Share 8.3

Helper function to create the authentication plugin.

Parameters

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

\Drupal\entity_share_client\Entity\RemoteInterface $remote: The "Remote" entity.

Return value

\Drupal\entity_share_client\ClientAuthorization\ClientAuthorizationInterface The Entity share Authorization plugin.

Overrides EntityShareClientFunctionalTestBase::createAuthenticationPlugin

2 calls to AuthenticationOAuthTest::createAuthenticationPlugin()
AuthenticationOAuthTest::setUp in modules/entity_share_client/tests/src/Functional/AuthenticationOAuthTest.php
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 118

Class

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

Namespace

Drupal\Tests\entity_share_client\Functional

Code

protected function createAuthenticationPlugin(UserInterface $user, RemoteInterface $remote) {

  // Create all needed OAuth-related entities on the "server" side.
  $this
    ->serverOauthSetup();
  $plugin = $this->authPluginManager
    ->createInstance('oauth');
  $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());
  }

  // Override Guzzle HTTP client options.
  // This is mandatory because otherwise in testing environment there would
  // be a redirection from POST /oauth/token to GET /oauth/token.
  // @see GuzzleHttp\RedirectMiddleware::modifyRequest().
  $request_options = [
    RequestOptions::HTTP_ERRORS => FALSE,
    RequestOptions::ALLOW_REDIRECTS => [
      'strict' => TRUE,
    ],
  ];
  $site_settings = Settings::getAll();
  $site_settings['http_client_config'] = $request_options;
  new Settings($site_settings);

  // Obtain the access token from server.
  $credentials = [
    'username' => $user
      ->getAccountName(),
    'password' => $user->passRaw,
    'client_id' => $this->clients[$user
      ->id()]
      ->uuid(),
    'client_secret' => $this->clientSecret,
    'authorization_path' => '/oauth/authorize',
    'token_path' => '/oauth/token',
  ];
  $access_token = '';
  try {
    $access_token = $plugin
      ->initializeToken($remote, $credentials);
  } catch (\Exception $e) {

    // Do nothing.
  }

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

  // Remove the username and password.
  unset($credentials['username']);
  unset($credentials['password']);
  $storage_key = $configuration['uuid'];
  $this->keyValueStore
    ->set($storage_key, $credentials);

  // Save the token.
  $this->keyValueStore
    ->set($storage_key . '-' . $plugin
    ->getPluginId(), $access_token);

  // We are using key value store for local credentials storage.
  $configuration['data'] = [
    'credential_provider' => 'entity_share',
    'storage_key' => $storage_key,
  ];
  $plugin
    ->setConfiguration($configuration);
  return $plugin;
}