Newer
Older
<?php
namespace Drupal\salesforce;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\salesforce\Rest\SalesforceIdentity;

Aaron Bauman
committed
use Drupal\salesforce\Storage\SalesforceAuthTokenStorageInterface;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Token\TokenInterface;
use OAuth\OAuth2\Service\Salesforce;

Aaron Bauman
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class SalesforceAuthProviderPluginBase extends Salesforce implements SalesforceAuthProviderInterface {
use StringTranslationTrait;
use DependencySerializationTrait;
use MessengerTrait;
/**
* @var \Drupal\salesforce\Consumer\SalesforceCredentials
*/
protected $credentials;
/**
* Token storage.
*
* @var \Drupal\salesforce\Storage\SalesforceAuthTokenStorageInterface
*/

Aaron Bauman
committed
* Provider id, e.g. jwt, oauth.
*
* @var string
*/
protected $pluginId;
/**
* Plugin definition.
*
* @var array
*/
protected $pluginDefinition;
/**
* Instance id, e.g. "sandbox1" or "production".

Aaron Bauman
committed
/**
* 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;

Aaron Bauman
committed
$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);

Aaron Bauman
committed
return new static($configuration, $plugin_id, $plugin_definition, $container->get('salesforce.http_client_wrapper'), $container->get('salesforce.auth_token_storage'));
}
public static function defaultConfiguration() {
return [
'consumer_key' => '',
'login_url' => 'https://test.salesforce.com',
];
}

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

Aaron Bauman
committed
return $this->pluginId;
}
/**
* {@inheritdoc}
*/
public function getPluginDefinition() {

Aaron Bauman
committed
return $this->pluginDefinition;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
}
/**
* {@inheritdoc}
*/
public function getConfiguration($key = NULL) {
if ($key !== NULL) {
return !empty($this->configuration[$key]) ? $this->configuration[$key] : NULL;
}
return $this->configuration;
}
/**
* {@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) {

Aaron Bauman
committed
$this->setConfiguration($form_state->getValue('provider_settings'));
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
if ($form_state->getResponse() instanceof TrustedRedirectResponse) {
// If we're redirecting off-site, do not proceed with save operation.
// We'll finish saving form input when we complete the OAuth handshake
// from Salesforce.
return FALSE;
}

Aaron Bauman
committed
// Initialize identity if token is available.
if (!$this->hasAccessToken()) {
return TRUE;
}
$this->refreshIdentity($token);
}
catch (\Exception $e) {
watchdog_exception('salesforce', $e);
$this->messenger()->addError($e->getMessage());
$form_state->disableRedirect();
return FALSE;
}
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* {@inheritdoc}
*/
public function requestAccessToken($code, $state = NULL) {
$token = parent::requestAccessToken($code, $state);
$this->refreshIdentity($token);
return $token;
}
/**
* {@inheritdoc}
*/
public function refreshAccessToken(TokenInterface $token) {
$token = parent::refreshAccessToken($token);
$this->refreshIdentity($token);
return $token;
}
/**
* {@inheritdoc}
*/
public function refreshIdentity(TokenInterface $token) {
$headers = [
'Authorization' => 'OAuth ' . $token->getAccessToken(),
'Content-type' => 'application/json',
];
$data = $token->getExtraParams();
$response = $this->httpClient->retrieveResponse(new Uri($data['id']), [], $headers);
$identity = new SalesforceIdentity($response);
$this->storage->storeIdentity($this->service(), $identity);
return $identity;
}

Aaron Bauman
committed
public function getCredentials() {

Aaron Bauman
committed
if (empty($this->credentials) || !$this->credentials->isValid()) {

Aaron Bauman
committed
$pluginDefinition = $this->getPluginDefinition();
$this->credentials = $pluginDefinition['credentials_class']::create($this->configuration);
}
return $this->credentials;
}
/**
* {@inheritdoc}
*/
public function getAuthorizationEndpoint() {

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

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

Aaron Bauman
committed
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') {
$identity = $this->getIdentity();
if (empty($identity)) {
throw new IdentityNotFoundException();
return $identity->getUrl($api_type, $this->getApiVersion());
}
/**
* {@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() {
$identity = $this->storage->retrieveIdentity($this->id());
if (empty($identity)) {
throw new IdentityNotFoundException();
}
return $identity;
}
/**
* {@inheritdoc}
*/
public function service() {
return $this->id();
}
/**
* Accessor to the storage adapter to be able to retrieve tokens.
*
* @return \Drupal\salesforce\Storage\SalesforceAuthTokenStorageInterface
*/
public function getStorage() {
return $this->storage;
}