Commit 3642a176 authored by john_a's avatar john_a Committed by john_a
Browse files

Issue #3060349 by john_a: /v2/token for Server to Server Integration Auth

parent d06e8ab1
......@@ -3,7 +3,8 @@ client_secret: ""
validate_json: true
do_not_send: false
base_url: "https://www.exacttargetapis.com"
request_token_url: "https://auth.exacttargetapis.com/v1/requestToken"
request_token_url: "https://YOUR_SUBDOMAIN.auth.exacttargetapis.com"
request_token_version: "v2"
login_attempts_max: 3
request_token_wait: 10
token: "0"
......
......@@ -19,6 +19,9 @@ marketing_cloud.settings:
request_token_url:
type: string
label: 'Salesforce auth token request URL'
request_token_version:
type: string
label: 'Salesforce auth token request version'
base_url:
type: string
label: 'Salesforce API URL base'
......
......@@ -18,3 +18,34 @@ function marketing_cloud_update_8001() {
$config->clear($key)->save();
}
}
/**
* Add new request_token_version settiing and change request_token_url to domain only.
*/
function marketing_cloud_update_8002() {
$config = \Drupal::service('config.factory')
->getEditable('marketing_cloud.settings');
$requestTokenUrl = $config->get('request_token_url');
$update_backup = $requestTokenUrl;
// Set version to v2 by default, unless already set to v1.
if ($requestTokenUrl !== NULL && strpos($requestTokenUrl, 'v1')) {
$requestTokenVersion = 'v1';
} else {
$requestTokenVersion = 'v2';
}
// Ensure request_token_url containas only the scheme and host.
$parts = parse_url($requestTokenUrl);
$requestTokenUrl = $parts['scheme'] . "://" . $parts['host'];
$config->set('request_token_url', $requestTokenUrl)
->set('request_token_version', $requestTokenVersion)
->save(TRUE);
// Preserve the data from before the update for contrib update.
if ($update_backup) {
\Drupal::keyValue('update_backup')
->set('marketing_cloud_update_8002', $update_backup);
}
}
......@@ -73,6 +73,17 @@ class MarketingCloudSettings extends ConfigFormBase {
'#default_value' => $config->get('request_token_url'),
];
$form['request_token_version'] = [
'#type' => 'select',
'#title' => $this->t('Salesforce auth token request version'),
'#description' => $this->t('The newer version of token request requires different parameters to the legacy v1.'),
'#options' => [
'v1',
'v2',
],
'#default_value' => $config->get('request_token_version'),
];
$form['login_attempts_max'] = [
'#type' => 'textfield',
'#title' => $this->t('Max login attempts'),
......@@ -114,6 +125,7 @@ class MarketingCloudSettings extends ConfigFormBase {
$config->set('do_not_send', $form_state->getValue('do_not_send'));
$config->set('base_url', $form_state->getValue('base_url'));
$config->set('request_token_url', $form_state->getValue('request_token_url'));
$config->set('request_token_version', $form_state->getValue('request_token_version'));
$config->set('login_attempts_max', $form_state->getValue('login_attempts_max'));
$config->set('request_token_wait', $form_state->getValue('request_token_wait'));
$config->save();
......
......@@ -85,19 +85,13 @@ class MarketingCloudSession {
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData', ['%missingData' => 'client_secret']);
$token_requisites = FALSE;
}
$url = $this->config
->get('request_token_url');
if (empty($url)) {
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData', ['%missingData' => 'request_token_url']);
$token_requisites = FALSE;
}
// Make token request/s.
if ($token_requisites) {
$loginAttempts = 0;
$loginAttemptsMax = $this->config->get('login_attempts_max');
while (!$token && $loginAttempts++ < $loginAttemptsMax) {
$token = $this->requestToken($url, $clientId, $clientSecret);
$token = $this->requestToken($clientId, $clientSecret);
}
}
......@@ -112,8 +106,6 @@ class MarketingCloudSession {
/**
* Perform the API call to request a valid token.
*
* @param string $url
* The Marketing Cloud token request URL.
* @param string $clientId
* The marketing Cloud client ID.
* @param string $clientSecret
......@@ -122,16 +114,37 @@ class MarketingCloudSession {
* @return bool|string
* The result of the token request, or FALSE on failure.
*/
private function requestToken($url, $clientId, $clientSecret) {
private function requestToken($clientId, $clientSecret) {
try {
\Drupal::logger(__METHOD__)->info('%message', ['%message' => 'Fetching a new token.']);
$url = $this->config
->get('request_token_url');
$requestVersion = $this->config
->get('request_token_version');
switch ($requestVersion) {
case 'v1':
$url .= '/v1/requestToken';
$formParams = [
'clientId' => $clientId,
'clientSecret' => $clientSecret,
];
break;
case 'v2':
default:
$url .= '/v2/token';
$formParams = [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
];
break;
}
$response = \Drupal::httpClient()->post($url, [
'verify' => FALSE,
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
'clientId' => $clientId,
'clientSecret' => $clientSecret,
],
'form_params' => $formParams,
]);
$data = json_decode($response->getBody());
return $data->accessToken;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment