Commit c903edd0 authored by john_a's avatar john_a Committed by John.Avery
Browse files

Issue #3195685 by john_a: Handle token expiration automatically in the MarketingCloudSession class

parent 8a7f8da8
......@@ -7,5 +7,6 @@ do_not_send: false
base_url: "https://www.exacttargetapis.com"
request_token_url: "https://YOUR_SUBDOMAIN.auth.exacttargetapis.com"
request_token_version: "v2"
login_attempts_max: 3
request_token_expire: 1200
request_token_wait: 10
login_attempts_max: 3
......@@ -31,9 +31,12 @@ marketing_cloud.settings:
base_url:
type: string
label: 'Salesforce API URL base'
login_attempts_max:
request_token_expire:
type: integer
label: 'Max login attempts'
label: 'TTL in seconds for the MC token'
request_token_wait:
type: integer
label: 'Seconds to wait before token re-request'
login_attempts_max:
type: integer
label: 'Max login attempts'
......@@ -83,7 +83,8 @@ function marketing_cloud_update_8004() {
*
* @see https://www.drupal.org/project/marketing_cloud/issues/3193522
*/
function marketing_cloud_update_8005() {
function marketing_cloud_update_8005()
{
$settings = Drupal::configFactory()->getEditable('marketing_cloud.settings');
if ($settings->get('scope') === NULL) {
$settings->set('scope', '')->save();
......@@ -92,3 +93,14 @@ function marketing_cloud_update_8005() {
$settings->set('account_id', 0)->save();
}
}
/**
* Add request_token_expire settings value.
*/
function marketing_cloud_update_8006() {
$default_request_token_expire = 1200;
$settings = Drupal::configFactory()->getEditable('marketing_cloud.settings');
if ($settings->get('request_token_expire') === NULL) {
$settings->set('request_token_expire', $default_request_token_expire)->save();
}
}
......@@ -99,11 +99,11 @@ class MarketingCloudSettings extends ConfigFormBase {
'#default_value' => $config->get('request_token_version'),
];
$form['login_attempts_max'] = [
$form['request_token_expire'] = [
'#type' => 'textfield',
'#title' => $this->t('Max login attempts'),
'#description' => $this->t('The maximum attempts at a valid token request.'),
'#default_value' => $config->get('login_attempts_max'),
'#title' => $this->t('TTL for the Marketing cloud token'),
'#description' => $this->t('Life of the marketing Cloud token in seconds. MC default is 20 minutes (1200s)'),
'#default_value' => $config->get('request_token_expire'),
];
$form['request_token_wait'] = [
......@@ -113,6 +113,13 @@ class MarketingCloudSettings extends ConfigFormBase {
'#default_value' => $config->get('request_token_wait'),
];
$form['login_attempts_max'] = [
'#type' => 'textfield',
'#title' => $this->t('Max login attempts'),
'#description' => $this->t('The maximum attempts at a valid token request.'),
'#default_value' => $config->get('login_attempts_max'),
];
$form['reset_token'] = [
'#type' => 'submit',
'#description' => $this->t("The SF token should be completely automated. But in rare cases where this becomes stuck, use this link to reset the token and it's state."),
......@@ -143,8 +150,9 @@ class MarketingCloudSettings extends ConfigFormBase {
$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_expire', $form_state->getValue('request_token_expire'));
$config->set('request_token_wait', $form_state->getValue('request_token_wait'));
$config->set('login_attempts_max', $form_state->getValue('login_attempts_max'));
$config->save();
parent::submitForm($form, $form_state);
......
......@@ -38,8 +38,7 @@ class MarketingCloudSession {
* MarketingCloudSession constructor.
*/
public function __construct() {
$this->config = \Drupal::configFactory()
->getEditable('marketing_cloud.settings');
$this->config = \Drupal::configFactory()->getEditable('marketing_cloud.settings');
$this->state = \Drupal::state();
}
......@@ -65,12 +64,13 @@ class MarketingCloudSession {
if ($this->state->get('marketing_cloud_requesting_token') == TRUE) {
// Wait n seconds to prevent overloading token request with simultaneous
// requests.
sleep($this->config
->get('requestToken_wait'));
sleep($this->config->get('requestToken_wait'));
}
$token = $this->state->get('marketing_cloud_token');
if ($token != FALSE && !$forceLogin) {
$expired = \Drupal::time()->getCurrentTime() >= $this->state->get('marketing_cloud_request_token_expire');
$recalculate_token = $expired || $forceLogin || empty($token);
if (!$recalculate_token) {
return $token;
}
......@@ -80,8 +80,8 @@ class MarketingCloudSession {
// Validate required params for token request.
$token_requisites = TRUE;
$clientId = $this->config
->get('client_id');
$clientId = $this->config->get('client_id');
if (empty($clientId)) {
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData',
['%missingData' => 'client_id']
......@@ -91,7 +91,9 @@ class MarketingCloudSession {
$clientSecret = $this->config
->get('client_secret');
if (empty($clientSecret)) {
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData', ['%missingData' => 'client_secret']);
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData',
['%missingData' => 'client_secret']
);
$token_requisites = FALSE;
}
......@@ -108,6 +110,8 @@ class MarketingCloudSession {
$this->state->set('marketing_cloud_token', $token);
$this->state->set('marketing_cloud_requesting_token', FALSE);
$expiry_time = \Drupal::time()->getCurrentTime() + $this->state->get('marketing_cloud_request_token_expire');
$this->state->set('marketing_cloud_request_token_expire', $expiry_time);
return $token;
}
......
......@@ -72,15 +72,15 @@ class MarketingCloudTest extends BrowserTestBase {
public function testMarketingCloudSession() {
// Expected start values.
$this->assertTrue($this->config('marketing_cloud.settings')->get('do_not_send'));
$this->assertNull(\Drupal::state()->get('token'));
$this->assertNull(\Drupal::state()->get('requesting_token'));
$this->assertNull(\Drupal::state()->get('marketing_cloud_token'));
$this->assertNull(\Drupal::state()->get('marketing_cloud_requesting_token'));
$this->assertNull($this->config('marketing_cloud.settings')->get('token'));
$this->assertNull($this->config('marketing_cloud.settings')->get('requesting_token'));
// Reset to new defaults.
$this->session->resetToken();
$this->assertFalse(\Drupal::state()->get('token'));
$this->assertFalse(\Drupal::state()->get('requesting_token'));
$this->assertFalse(\Drupal::state()->get('marketing_cloud_token'));
$this->assertFalse(\Drupal::state()->get('marketing_cloud_requesting_token'));
$this->assertNull($this->config('marketing_cloud.settings')->get('token'));
$this->assertNull($this->config('marketing_cloud.settings')->get('requesting_token'));
}
......
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