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

Issue #3195684 by john_a, pcambra: State API is a better fit to store the token

parent bb399693
......@@ -7,5 +7,3 @@ request_token_url: "https://YOUR_SUBDOMAIN.auth.exacttargetapis.com"
request_token_version: "v2"
login_attempts_max: 3
request_token_wait: 10
token: "0"
requesting_token: false
......@@ -31,9 +31,3 @@ marketing_cloud.settings:
request_token_wait:
type: integer
label: 'Seconds to wait before token re-request'
token:
type: string
label: 'Salesforce auth token'
requesting_token:
type: boolean
label: 'Salesforce auth token request in progress'
......@@ -66,3 +66,14 @@ function marketing_cloud_update_8003() {
$config->set('request_token_version', 'v2')->save();
}
}
/**
* Remove token and requesting_token from config.
*
* These have been moved to state vars (https://www.drupal.org/project/marketing_cloud/issues/3195684)
*/
function marketing_cloud_update_8004() {
$settings = Drupal::configFactory()->getEditable('marketing_cloud.settings');
$settings->clear('token')->save();
$settings->clear('requesting_token')->save();
}
......@@ -25,7 +25,14 @@ class MarketingCloudSession {
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
private $config;
protected $config;
/**
* Drupal state API.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $state;
/**
* MarketingCloudSession constructor.
......@@ -33,16 +40,15 @@ class MarketingCloudSession {
public function __construct() {
$this->config = \Drupal::configFactory()
->getEditable('marketing_cloud.settings');
$this->state = \Drupal::state();
}
/**
* Reset the stored token.
*/
public function resetToken() {
$this->config
->set('token', FALSE)
->set('requesting_token', FALSE)
->save();
$this->state->set('marketing_cloud_token', FALSE);
$this->state->set('marketing_cloud_requesting_token', FALSE);
}
/**
......@@ -56,24 +62,20 @@ class MarketingCloudSession {
* The token or FALSE on failure.
*/
public function token($forceLogin = FALSE) {
if ($this->config->get('requesting_token') == TRUE) {
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'));
}
$token = $this->config
->get('token');
$token = $this->state->get('marketing_cloud_token');
if ($token != FALSE && !$forceLogin) {
return $token;
}
// Prevent flooding by setting requesting_token to TRUE.
$this->config
->set('requesting_token', TRUE)
->save();
$this->state->set('marketing_cloud_requesting_token', TRUE);
$token = FALSE;
// Validate required params for token request.
......@@ -81,7 +83,9 @@ class MarketingCloudSession {
$clientId = $this->config
->get('client_id');
if (empty($clientId)) {
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData', ['%missingData' => 'client_id']);
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData',
['%missingData' => 'client_id']
);
$token_requisites = FALSE;
}
$clientSecret = $this->config
......@@ -100,10 +104,8 @@ class MarketingCloudSession {
}
}
$this->config
->set('token', $token)
->set('requesting_token', FALSE)
->save();
$this->state->set('marketing_cloud_token', $token);
$this->state->set('marketing_cloud_requesting_token', FALSE);
return $token;
}
......
......@@ -72,13 +72,17 @@ class MarketingCloudTest extends BrowserTestBase {
public function testMarketingCloudSession() {
// Expected start values.
$this->assertTrue($this->config('marketing_cloud.settings')->get('do_not_send'));
$this->assertEquals("0", $this->config('marketing_cloud.settings')->get('token'));
$this->assertFalse($this->config('marketing_cloud.settings')->get('requesting_token'));
$this->assertNull(\Drupal::state()->get('token'));
$this->assertNull(\Drupal::state()->get('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->assertEmpty($this->config('marketing_cloud.settings')->get('token'));
$this->assertEmpty($this->config('marketing_cloud.settings')->get('requesting_token'));
$this->assertFalse(\Drupal::state()->get('token'));
$this->assertFalse(\Drupal::state()->get('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