You are here

abstract class SalesforceAuthProviderPluginBase in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 src/SalesforceAuthProviderPluginBase.php \Drupal\salesforce\SalesforceAuthProviderPluginBase
  2. 5.0.x src/SalesforceAuthProviderPluginBase.php \Drupal\salesforce\SalesforceAuthProviderPluginBase

Shared methods for auth providers.

Hierarchy

Expanded class hierarchy of SalesforceAuthProviderPluginBase

2 files declare their use of SalesforceAuthProviderPluginBase
SalesforceOAuthPlugin.php in src/Plugin/SalesforceAuthProvider/SalesforceOAuthPlugin.php
TestSalesforceAuthProvider.php in src/Tests/TestSalesforceAuthProvider.php

File

src/SalesforceAuthProviderPluginBase.php, line 19

Namespace

Drupal\salesforce
View source
abstract class SalesforceAuthProviderPluginBase extends Salesforce implements SalesforceAuthProviderInterface {
  use StringTranslationTrait;
  use DependencySerializationTrait;
  use MessengerTrait;

  /**
   * Credentials.
   *
   * @var \Drupal\salesforce\Consumer\SalesforceCredentials
   */
  protected $credentials;

  /**
   * Configuration.
   *
   * @var array
   */
  protected $configuration;

  /**
   * Token storage.
   *
   * @var \Drupal\salesforce\Storage\SalesforceAuthTokenStorageInterface
   */
  protected $storage;

  /**
   * Provider id, e.g. jwt, oauth.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * Plugin definition.
   *
   * @var array
   */
  protected $pluginDefinition;

  /**
   * Instance id, e.g. "sandbox1" or "production".
   *
   * @var string
   */
  protected $id;

  /**
   * SalesforceOAuthPlugin constructor.
   *
   * @param array $configuration
   *   Plugin configuration.
   * @param string $plugin_id
   *   Plugin id.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \OAuth\Common\Http\Client\ClientInterface $httpClient
   *   The oauth http client.
   * @param \Drupal\salesforce\Storage\SalesforceAuthTokenStorageInterface $storage
   *   Auth token storage service.
   *
   * @throws \OAuth\OAuth2\Service\Exception\InvalidScopeException
   *   Comment.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientInterface $httpClient, SalesforceAuthTokenStorageInterface $storage) {
    $this->id = !empty($configuration['id']) ? $configuration['id'] : NULL;
    $this->configuration = $configuration;
    $this->pluginDefinition = $plugin_definition;
    $this->pluginId = $plugin_id;
    $this->credentials = $this
      ->getCredentials();
    parent::__construct($this
      ->getCredentials(), $httpClient, $storage, [], new Uri($this
      ->getCredentials()
      ->getLoginUrl()));
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $configuration = array_merge(static::defaultConfiguration(), $configuration);
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('salesforce.http_client_wrapper'), $container
      ->get('salesforce.auth_token_storage'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultConfiguration() {
    return [
      'consumer_key' => '',
      'login_url' => 'https://test.salesforce.com',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this
      ->getPluginDefinition()['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->id;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginId() {
    return $this->pluginId;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginDefinition() {
    return $this->pluginDefinition;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration($key = NULL) {
    if ($key !== NULL) {
      return !empty($this->configuration[$key]) ? $this->configuration[$key] : NULL;
    }
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function getLoginUrl() {
    return $this->credentials
      ->getLoginUrl();
  }

  /**
   * {@inheritdoc}
   */
  public function getConsumerKey() {
    return $this->credentials
      ->getConsumerKey();
  }

  /**
   * {@inheritdoc}
   */
  public function getConsumerSecret() {
    return $this->credentials
      ->getConsumerSecret();
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this
      ->setConfiguration($form_state
      ->getValue('provider_settings'));
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {

    // Initialize identity if token is available.
    if (!$this
      ->hasAccessToken()) {
      return TRUE;
    }
    $token = $this
      ->getAccessToken();
    $headers = [
      'Authorization' => 'OAuth ' . $token
        ->getAccessToken(),
      'Content-type' => 'application/json',
    ];
    $data = $token
      ->getExtraParams();
    $response = $this->httpClient
      ->retrieveResponse(new Uri($data['id']), [], $headers);
    $identity = $this
      ->parseIdentityResponse($response);
    $this->storage
      ->storeIdentity($this
      ->service(), $identity);
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getCredentials() {
    if (!$this->credentials || !$this->credentials
      ->isValid()) {
      $pluginDefinition = $this
        ->getPluginDefinition();
      $this->credentials = $pluginDefinition['credentials_class']::create($this->configuration);
    }
    return $this->credentials;
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthorizationEndpoint() {
    return new Uri($this
      ->getCredentials()
      ->getLoginUrl() . static::AUTH_ENDPOINT_PATH);
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessTokenEndpoint() {
    return new Uri($this
      ->getCredentials()
      ->getLoginUrl() . static::AUTH_TOKEN_PATH);
  }

  /**
   * {@inheritdoc}
   */
  public function hasAccessToken() {
    return $this->storage ? $this->storage
      ->hasAccessToken($this
      ->id()) : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessToken() {
    return $this->storage
      ->retrieveAccessToken($this
      ->id());
  }

  /**
   * {@inheritdoc}
   */
  public function revokeAccessToken() {
    return $this->storage
      ->clearToken($this
      ->id());
  }

  /**
   * {@inheritdoc}
   */
  public function getInstanceUrl() {
    return $this
      ->getAccessToken()
      ->getExtraParams()['instance_url'];
  }

  /**
   * {@inheritdoc}
   */
  public function getApiEndpoint($api_type = 'rest') {
    $url =& drupal_static(self::CLASS . __FUNCTION__ . $api_type);
    if (!isset($url)) {
      $identity = $this
        ->getIdentity();
      if (empty($identity)) {
        return FALSE;
      }
      if (is_string($identity)) {
        $url = $identity;
      }
      elseif (isset($identity['urls'][$api_type])) {
        $url = $identity['urls'][$api_type];
      }
      $url = str_replace('{version}', $this
        ->getApiVersion(), $url);
    }
    return $url;
  }

  /**
   * {@inheritdoc}
   */
  public function getApiVersion() {
    $version = \Drupal::config('salesforce.settings')
      ->get('rest_api_version.version');
    if (empty($version) || \Drupal::config('salesforce.settings')
      ->get('use_latest')) {
      return self::LATEST_API_VERSION;
    }
    return \Drupal::config('salesforce.settings')
      ->get('rest_api_version.version');
  }

  /**
   * {@inheritdoc}
   */
  public function getIdentity() {
    return $this->storage
      ->retrieveIdentity($this
      ->id());
  }

  /**
   * {@inheritdoc}
   */
  public function service() {
    return $this
      ->id();
  }

  /**
   * Handle the identity response from Salesforce.
   *
   * @param string $responseBody
   *   JSON identity response from Salesforce.
   *
   * @return array
   *   The identity.
   *
   * @throws \OAuth\Common\Http\Exception\TokenResponseException
   */
  protected function parseIdentityResponse($responseBody) {
    $data = json_decode($responseBody, TRUE);
    if (NULL === $data || !is_array($data)) {
      throw new TokenResponseException('Unable to parse response.');
    }
    elseif (isset($data['error'])) {
      throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
    }
    return $data;
  }

  /**
   * Accessor to the storage adapter to be able to retrieve tokens.
   *
   * @return \Drupal\salesforce\Storage\SalesforceAuthTokenStorageInterface
   *   The token storage.
   */
  public function getStorage() {
    return $this->storage;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginFormInterface::buildConfigurationForm public function Form constructor. 36
SalesforceAuthProviderInterface::AUTH_ENDPOINT_PATH constant
SalesforceAuthProviderInterface::AUTH_TOKEN_PATH constant
SalesforceAuthProviderInterface::LATEST_API_VERSION constant
SalesforceAuthProviderInterface::refreshAccessToken public function Perform a refresh of the given token. 1
SalesforceAuthProviderInterface::SOAP_CLASS_PATH constant
SalesforceAuthProviderPluginBase::$configuration protected property Configuration.
SalesforceAuthProviderPluginBase::$credentials protected property Credentials. 1
SalesforceAuthProviderPluginBase::$id protected property Instance id, e.g. "sandbox1" or "production".
SalesforceAuthProviderPluginBase::$pluginDefinition protected property Plugin definition.
SalesforceAuthProviderPluginBase::$pluginId protected property Provider id, e.g. jwt, oauth.
SalesforceAuthProviderPluginBase::$storage protected property Token storage.
SalesforceAuthProviderPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SalesforceAuthProviderPluginBase::defaultConfiguration public static function Default configuration for this plugin type. Overrides SalesforceAuthProviderInterface::defaultConfiguration 1
SalesforceAuthProviderPluginBase::getAccessToken public function Access token for this plugin. Overrides SalesforceAuthProviderInterface::getAccessToken
SalesforceAuthProviderPluginBase::getAccessTokenEndpoint public function Access token URL for this plugin type. Overrides SalesforceAuthProviderInterface::getAccessTokenEndpoint
SalesforceAuthProviderPluginBase::getApiEndpoint public function API Url for this plugin. Overrides SalesforceAuthProviderInterface::getApiEndpoint
SalesforceAuthProviderPluginBase::getApiVersion public function Get the globally configured API version to use. Overrides SalesforceAuthProviderInterface::getApiVersion
SalesforceAuthProviderPluginBase::getAuthorizationEndpoint public function Authorization URL for this plugin type. Overrides SalesforceAuthProviderInterface::getAuthorizationEndpoint
SalesforceAuthProviderPluginBase::getConfiguration public function
SalesforceAuthProviderPluginBase::getConsumerKey public function Consumer key for the connected OAuth app. Overrides SalesforceAuthProviderInterface::getConsumerKey
SalesforceAuthProviderPluginBase::getConsumerSecret public function Consumer secret for the connected OAuth app. Overrides SalesforceAuthProviderInterface::getConsumerSecret 1
SalesforceAuthProviderPluginBase::getCredentials public function Return the credentials configured for this auth provider instance. Overrides SalesforceAuthProviderInterface::getCredentials
SalesforceAuthProviderPluginBase::getIdentity public function Identify for this connection. Overrides SalesforceAuthProviderInterface::getIdentity
SalesforceAuthProviderPluginBase::getInstanceUrl public function Instance URL for this connection. Overrides SalesforceAuthProviderInterface::getInstanceUrl 1
SalesforceAuthProviderPluginBase::getLoginUrl public function Login URL, e.g. https://login.salesforce.com, for this plugin. Overrides SalesforceAuthProviderInterface::getLoginUrl
SalesforceAuthProviderPluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
SalesforceAuthProviderPluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
SalesforceAuthProviderPluginBase::getStorage public function Accessor to the storage adapter to be able to retrieve tokens.
SalesforceAuthProviderPluginBase::hasAccessToken public function TRUE if the connection has a token, regardless of validity. Overrides SalesforceAuthProviderInterface::hasAccessToken
SalesforceAuthProviderPluginBase::id public function Id of this service. Overrides SalesforceAuthProviderInterface::id
SalesforceAuthProviderPluginBase::label public function Label of this service. Overrides SalesforceAuthProviderInterface::label
SalesforceAuthProviderPluginBase::parseIdentityResponse protected function Handle the identity response from Salesforce.
SalesforceAuthProviderPluginBase::revokeAccessToken public function Clear the access token for this auth provider plugin. Overrides SalesforceAuthProviderInterface::revokeAccessToken
SalesforceAuthProviderPluginBase::save public function Callback for configuration form after saving config entity. Overrides SalesforceAuthProviderInterface::save
SalesforceAuthProviderPluginBase::service public function The auth provider service. Overrides SalesforceAuthProviderInterface::service
SalesforceAuthProviderPluginBase::setConfiguration public function
SalesforceAuthProviderPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
SalesforceAuthProviderPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
SalesforceAuthProviderPluginBase::__construct public function SalesforceOAuthPlugin constructor. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.