You are here

public function BynderApi::finishOAuthTokenRetrieval in Bynder 8.3

Same name and namespace in other branches
  1. 8 src/BynderApi.php \Drupal\bynder\BynderApi::finishOAuthTokenRetrieval()
  2. 8.2 src/BynderApi.php \Drupal\bynder\BynderApi::finishOAuthTokenRetrieval()
  3. 4.0.x src/BynderApi.php \Drupal\bynder\BynderApi::finishOAuthTokenRetrieval()

Finishes the access token retrieval after the user has been redirected.

When Bynder redirects the user back after the successful login this function takes over and gets the access token and stores it for the future use.

Parameters

Request $request: The current request.

Overrides BynderApiInterface::finishOAuthTokenRetrieval

File

src/BynderApi.php, line 221

Class

BynderApi
Bynder API service.

Namespace

Drupal\bynder

Code

public function finishOAuthTokenRetrieval(Request $request) {
  $bynder_settings = $this->configFactory
    ->get('bynder.settings');
  $session_data = $this->session
    ->get('bynder', []);
  $bynder_configuration = [
    'consumerKey' => $bynder_settings
      ->get('consumer_key'),
    'consumerSecret' => $bynder_settings
      ->get('consumer_secret'),
    'token' => $request->query
      ->get('oauth_token'),
    'tokenSecret' => $session_data['request_token']['oauth_token_secret'],
    'baseUrl' => $bynder_settings
      ->get('account_domain'),
    'requestOptions' => [
      'timeout' => $bynder_settings
        ->get('timeout'),
    ],
  ];

  // @TODO Statically cache API object?
  $this->bynderApi = BynderApiFactory::create($bynder_configuration);
  foreach ($this->bynderApi
    ->getAccessToken()
    ->wait() as $key => $item) {
    $session_data['access_token'][$key] = $item;
  }
  unset($session_data['request_token']);
  $session_data['config_hash'] = $this->state
    ->get('bynder_config_hash');
  $this->session
    ->set('bynder', $session_data);
}