You are here

class SalesforceOAuthPlugin in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_oauth/src/Plugin/SalesforceAuthProvider/SalesforceOAuthPlugin.php \Drupal\salesforce_oauth\Plugin\SalesforceAuthProvider\SalesforceOAuthPlugin

Salesforce OAuth user-agent flow auth provider plugin.

Plugin annotation


@Plugin(
  id = "oauth",
  label = @Translation("Salesforce OAuth User-Agent"),
  credentials_class = "\Drupal\salesforce_oauth\Consumer\SalesforceOAuthCredentials"
)

Hierarchy

Expanded class hierarchy of SalesforceOAuthPlugin

File

modules/salesforce_oauth/src/Plugin/SalesforceAuthProvider/SalesforceOAuthPlugin.php, line 19

Namespace

Drupal\salesforce_oauth\Plugin\SalesforceAuthProvider
View source
class SalesforceOAuthPlugin extends SalesforceAuthProviderPluginBase {

  /**
   * Credentials.
   *
   * @var \Drupal\salesforce_oauth\Consumer\SalesforceOAuthCredentials
   */
  protected $credentials;

  /**
   * {@inheritdoc}
   */
  public static function defaultConfiguration() {
    $defaults = parent::defaultConfiguration();
    return array_merge($defaults, [
      'consumer_secret' => '',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['consumer_key'] = [
      '#title' => $this
        ->t('Salesforce consumer key'),
      '#type' => 'textfield',
      '#description' => $this
        ->t('Consumer key of the Salesforce remote application you want to grant access to'),
      '#required' => TRUE,
      '#default_value' => $this
        ->getCredentials()
        ->getConsumerKey(),
    ];
    $form['consumer_secret'] = [
      '#title' => $this
        ->t('Salesforce consumer secret'),
      '#type' => 'textfield',
      '#description' => $this
        ->t('Consumer secret of the Salesforce remote application.'),
      '#required' => TRUE,
      '#default_value' => $this
        ->getCredentials()
        ->getConsumerSecret(),
    ];
    $form['login_url'] = [
      '#title' => $this
        ->t('Login URL'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getCredentials()
        ->getLoginUrl(),
      '#description' => $this
        ->t('Enter a login URL, either https://login.salesforce.com or https://test.salesforce.com.'),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);

    // Write the config id to private temp store, so that we can use the same
    // callback URL for all OAuth applications in Salesforce.

    /** @var \Drupal\Core\TempStore\PrivateTempStore $tempstore */
    $tempstore = \Drupal::service('tempstore.private')
      ->get('salesforce_oauth');
    $tempstore
      ->set('config_id', $form_state
      ->getValue('id'));
    try {
      $path = $this
        ->getAuthorizationEndpoint();
      $query = [
        'redirect_uri' => $this
          ->getCredentials()
          ->getCallbackUrl(),
        'response_type' => 'code',
        'client_id' => $this
          ->getCredentials()
          ->getConsumerKey(),
      ];

      // Send the user along to the Salesforce OAuth login form. If successful,
      // the user will be redirected to {redirect_uri} to complete the OAuth
      // handshake, and thence to the entity listing. Upon failure, the user
      // redirect URI will send the user back to the edit form.
      $form_state
        ->setResponse(new TrustedRedirectResponse(Url::fromUri($path . '?' . http_build_query($query))
        ->toString()));
    } catch (\Exception $e) {
      $form_state
        ->setError($form, $this
        ->t("Error during authorization: %message", [
        '%message' => $e
          ->getMessage(),
      ]));
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
SalesforceAuthProviderInterface::AUTH_ENDPOINT_PATH constant
SalesforceAuthProviderInterface::AUTH_TOKEN_PATH constant
SalesforceAuthProviderInterface::LATEST_API_VERSION constant
SalesforceAuthProviderInterface::SOAP_CLASS_PATH constant
SalesforceAuthProviderPluginBase::$configuration protected property Configuration.
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 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::getCredentials public function Return the credentials configured for this auth provider instance. Overrides SalesforceAuthProviderInterface::getCredentials 1
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::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 1
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::refreshAccessToken public function Perform a refresh of the given token. Overrides SalesforceAuthProviderInterface::refreshAccessToken 3
SalesforceAuthProviderPluginBase::refreshIdentity public function Given a token, fetch the SF identity. Overrides SalesforceAuthProviderInterface::refreshIdentity
SalesforceAuthProviderPluginBase::requestAccessToken public function 1
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::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
SalesforceAuthProviderPluginBase::__construct public function SalesforceOAuthPlugin constructor. 2
SalesforceOAuthPlugin::$credentials protected property Credentials. Overrides SalesforceAuthProviderPluginBase::$credentials
SalesforceOAuthPlugin::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
SalesforceOAuthPlugin::defaultConfiguration public static function Default configuration for this plugin type. Overrides SalesforceAuthProviderPluginBase::defaultConfiguration
SalesforceOAuthPlugin::getConsumerSecret public function
SalesforceOAuthPlugin::submitConfigurationForm public function Form submission handler. Overrides SalesforceAuthProviderPluginBase::submitConfigurationForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.