You are here

public function Oauth::submitConfigurationForm in Entity Share 8.3

Form submission handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides ClientAuthorizationPluginBase::submitConfigurationForm

File

modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php, line 229

Class

Oauth
Provides Oauth2 based client authorization.

Namespace

Drupal\entity_share_client\Plugin\ClientAuthorization

Code

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $configuration = $this
    ->getConfiguration();

  /** @var \Drupal\entity_share_client\Entity\RemoteInterface $remote */
  $remote = $form_state
    ->get('remote');
  $resetConfiguration = $configuration;
  $provider = $values['credential_provider'];
  $credentials = $values[$provider];
  array_walk($credentials, function (&$value) {
    $value = trim($value);
  });
  $key = $configuration['uuid'];
  if ($provider == 'key') {
    $key = $credentials['id'];
  }
  $configuration['data'] = [
    'credential_provider' => $provider,
    'storage_key' => $key,
  ];
  $this
    ->setConfiguration($configuration);
  try {

    // Try to obtain a token.
    switch ($provider) {
      case 'key':
        $requestCredentials = $this->keyService
          ->getCredentials($this);
        $requestCredentials['username'] = $credentials['username'];
        $requestCredentials['password'] = $credentials['password'];
        $accessToken = $this
          ->initializeToken($remote, $requestCredentials);

        // In case the credentials were previously stored locally, clear
        // the local storage.
        $this->keyValueStore
          ->delete($configuration['uuid']);
        break;
      default:
        $accessToken = $this
          ->initializeToken($remote, $credentials);

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

    // Save the token, using the plugin id appended to the uuid to create a
    // unique key that is distinct from the credential key that may be
    // used for local storage above.
    $this->keyValueStore
      ->set($configuration['uuid'] . '-' . $this
      ->getPluginId(), $accessToken);
    $this->messenger
      ->addStatus($this
      ->t('OAuth token obtained from remote website and stored.'));
  } catch (IdentityProviderException $e) {

    // Failed to get the access token.
    // Reset original configuration.
    $this
      ->setConfiguration($resetConfiguration);
    $this->messenger
      ->addError($this
      ->t('Unable to obtain an OAuth token. The error message is: @message', [
      '@message' => $e
        ->getMessage(),
    ]));
  }
}