Commit 0e9b1ec6 authored by john_a's avatar john_a Committed by John Avery
Browse files

Issue #2978219 by john_a: Salesforce token is not refreshing when stale

parent bc43d0a6
......@@ -202,6 +202,7 @@ abstract class MarketingCloudService {
$endpoint = $endpoint . '?' . implode('&', $arr);
}
$url = $marketingCloudConfig->get('base_url') . $endpoint;
// Prepare data.
$data = json_encode($data);
......@@ -211,9 +212,10 @@ abstract class MarketingCloudService {
return ['url' => $url, 'data' => $data];
}
// Fetch the token.
$response = $this->restCall($method, $url, $data);
if ($response == '401 Unauthorized') {
$this->loggerFactory->get(__METHOD__)->error('Stale token, fetching a fresh token and resending');
$this->loggerFactory->get(__METHOD__)->notice('Stale token, fetching a fresh token and resending');
$response = $this->restCall($method, $url, $data, TRUE);
}
......
......@@ -17,19 +17,21 @@ namespace Drupal\marketing_cloud;
*/
class MarketingCloudSession {
private $config;
/**
* MarketingCloudSession constructor.
*/
public function __construct() {
$this->config = \Drupal::configFactory()
->getEditable('marketing_cloud.settings');
}
/**
* Reset the stored token.
*/
public function resetToken() {
$config = \Drupal::configFactory()
->getEditable('marketing_cloud.settings');
$config
$this->config
->set('token', FALSE)
->set('requesting_token', FALSE)
->save();
......@@ -46,53 +48,60 @@ class MarketingCloudSession {
* The token or FALSE on failure.
*/
public function token($forceLogin = FALSE) {
$config = \Drupal::configFactory()->getEditable('marketing_cloud.settings');
if ($config->get('requesting_token') == TRUE) {
if ($this->config->get('requesting_token') == TRUE) {
// Wait n seconds to prevent overloading token request with simultaneous
// requests.
sleep($config->get('requestToken_wait'));
sleep($this->config
->get('requestToken_wait'));
}
$token = $config->get('token');
$token = $this->config
->get('token');
if ($token != FALSE && !$forceLogin) {
return $token;
}
$config->set('requesting_token', TRUE)->save();
$token_requisites = TRUE;
// Prevent flooding by setting requesting_token to TRUE.
$this->config
->set('requesting_token', TRUE)
->save();
$token = FALSE;
$clientId = $config->get('client_id');
// Validate required params for token request.
$token_requisites = TRUE;
$clientId = $this->config
->get('client_id');
if (empty($clientId)) {
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData', ['%missingData' => 'client_id']);
$token_requisites = FALSE;
}
$clientSecret = $config->get('client_secret');
$clientSecret = $this->config
->get('client_secret');
if (empty($clientSecret)) {
\Drupal::logger(__METHOD__)->error('Bad config data: %missingData', ['%missingData' => 'client_secret']);
$token_requisites = FALSE;
}
$url = $config->get('request_token_url');
$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;
}
if (!$config->get('do_not_send')) {
if ($token_requisites) {
$loginAttempts = 0;
while (!$token && $loginAttempts++ < $config->get('login_attempts_max')) {
$token = $this->requestToken($url, $clientId, $clientSecret);
}
}
else {
$token = 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);
}
}
else {
$token = TRUE;
}
$config->set('token', $token)->save();
$config->set('requesting_token', ($token === FALSE))->save();
$this->config
->set('token', $token)
->set('requesting_token', FALSE)
->save();
return $token;
}
......
......@@ -65,16 +65,8 @@ class MarketingCloudTest extends BrowserTestBase {
$this->assertEquals("0", $this->config('marketing_cloud.settings')->get('token'));
$this->assertFalse($this->config('marketing_cloud.settings')->get('requesting_token'));
$token = $this->session->token();
// Expect true, because we have switched on "do_not_send".
$this->assertTrue($token);
// Stored token should be the same as the returned value.
$this->assertEquals($token, $this->config('marketing_cloud.settings')->get('token'));
// Token fetched, so not requesting token status.
$this->assertFalse($this->config('marketing_cloud.settings')->get('requesting_token'));
$this->session->resetToken();
// Reset to new defaults.
$this->session->resetToken();
$this->assertFalse($this->config('marketing_cloud.settings')->get('token'));
$this->assertFalse($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