Skip to content
Snippets Groups Projects
Commit d2a29120 authored by Ivan Duarte's avatar Ivan Duarte Committed by Aaron Bauman
Browse files

Issue #3057022 by jidrone: Support for Gov Cloud

parent da93bdb5
No related branches found
No related tags found
Loading
<?php
namespace Drupal\salesforce_jwt\Consumer;
/**
* JWT Gov Cloud credentials.
*/
class JWTGovCloudCredentials extends JWTCredentials {
/**
* Token URL for JWT OAuth authentication.
*
* @var string
*/
protected $tokenUrl;
/**
* {@inheritdoc}
*/
public function __construct($consumerKey, $loginUrl, $loginUser, $keyId, $tokenUrl) {
parent::__construct($consumerKey, $loginUrl, $loginUser, $keyId);
$this->tokenUrl = $tokenUrl;
}
/**
* Constructor helper.
*
* @param array $configuration
* Plugin configuration.
*
* @return \Drupal\salesforce_jwt\Consumer\JWTGovCloudCredentials
* Credentials, valid or not.
*/
public static function create(array $configuration) {
return new static($configuration['consumer_key'], $configuration['login_url'], $configuration['login_user'], $configuration['encrypt_key'], $configuration['token_url']);
}
/**
* Token url getter.
*
* @return string
* The token url.
*/
public function getTokenUrl() {
return $this->tokenUrl;
}
/**
* {@inheritdoc}
*/
public function isValid() {
return !empty($this->loginUser) && !empty($this->consumerId) && !empty($this->keyId) && !empty($this->tokenUrl);
}
}
<?php
namespace Drupal\salesforce_jwt\Plugin\SalesforceAuthProvider;
use Drupal\Core\Form\FormStateInterface;
use OAuth\Common\Http\Uri\Uri;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* JWT Oauth plugin.
*
* @Plugin(
* id = "jwt_govcloud",
* label = @Translation("Salesforce JWT OAuth for GovCloud"),
* credentials_class = "\Drupal\salesforce_jwt\Consumer\JWTGovCloudCredentials"
* )
*/
class SalesforceJWTGovCloudPlugin extends SalesforceJWTPlugin {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$configuration = array_merge(self::defaultConfiguration(), $configuration);
return new static($configuration, $plugin_id, $plugin_definition, $container->get('salesforce.http_client_wrapper'), $container->get('salesforce.auth_token_storage'), $container->get('key.repository'));
}
/**
* {@inheritdoc}
*/
public static function defaultConfiguration() {
$defaults = parent::defaultConfiguration();
return array_merge($defaults, [
'token_url' => '',
]);
}
/**
* {@inheritdoc}
*/
public function getTokenUrl() {
return $this->getCredentials()->getTokenUrl();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['token_url'] = [
'#title' => t('Token URL'),
'#type' => 'textfield',
'#default_value' => $this->getCredentials()->getTokenUrl(),
'#description' => t('Enter a token URL, like https://yourcompany.my.salesforce.com'),
'#required' => TRUE,
];
return $form;
}
/**
* Overrides AbstractService::requestAccessToken for jwt-bearer flow.
*
* This is only intended to use the token url instead of login url.
*
* @param string $assertion
* The JWT assertion.
* @param string $state
* Not used.
*
* @return \OAuth\Common\Token\TokenInterface
* Access Token.
*
* @throws \OAuth\Common\Http\Exception\TokenResponseException
*/
public function requestAccessToken($assertion, $state = NULL) {
$data = [
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $assertion,
];
$response = $this->httpClient->retrieveResponse(new Uri($this->getTokenUrl() . static::AUTH_TOKEN_PATH), $data, ['Content-Type' => 'application/x-www-form-urlencoded']);
$token = $this->parseAccessTokenResponse($response);
$this->storage->storeAccessToken($this->service(), $token);
return $token;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment