You are here

public function OAuth2::getAuthenticationOptions in Migrate Plus 8.5

Same name and namespace in other branches
  1. 8.2 src/Plugin/migrate_plus/authentication/OAuth2.php \Drupal\migrate_plus\Plugin\migrate_plus\authentication\OAuth2::getAuthenticationOptions()
  2. 8.3 src/Plugin/migrate_plus/authentication/OAuth2.php \Drupal\migrate_plus\Plugin\migrate_plus\authentication\OAuth2::getAuthenticationOptions()
  3. 8.4 src/Plugin/migrate_plus/authentication/OAuth2.php \Drupal\migrate_plus\Plugin\migrate_plus\authentication\OAuth2::getAuthenticationOptions()

Performs authentication, returning any options to be added to the request.

@link http://docs.guzzlephp.org/en/latest/request-options.html

Return value

array Options (such as Authentication headers) to be added to the request.

Overrides AuthenticationPluginInterface::getAuthenticationOptions

File

src/Plugin/migrate_plus/authentication/OAuth2.php, line 32

Class

OAuth2
Provides OAuth2 authentication for the HTTP resource.

Namespace

Drupal\migrate_plus\Plugin\migrate_plus\authentication

Code

public function getAuthenticationOptions() {
  $handlerStack = HandlerStack::create();
  $client = new Client([
    'handler' => $handlerStack,
    'base_uri' => $this->configuration['base_uri'],
    'auth' => 'oauth2',
  ]);
  switch ($this->configuration['grant_type']) {
    case 'authorization_code':
      $grant_type = new AuthorizationCode($client, $this->configuration);
      break;
    case 'client_credentials':
      $grant_type = new ClientCredentials($client, $this->configuration);
      break;
    case 'urn:ietf:params:oauth:grant-type:jwt-bearer':
      $grant_type = new JwtBearer($client, $this->configuration);
      break;
    case 'password':
      $grant_type = new PasswordCredentials($client, $this->configuration);
      break;
    case 'refresh_token':
      $grant_type = new RefreshToken($client, $this->configuration);
      break;
    default:
      throw new MigrateException("Unrecognized grant_type {$this->configuration['grant_type']}.");
  }
  $middleware = new OAuthMiddleware($client, $grant_type);
  return [
    'headers' => [
      'Authorization' => 'Bearer ' . $middleware
        ->getAccessToken()
        ->getToken(),
    ],
  ];
}