SalesforceAuthTokenStorage.php in Salesforce Suite 8.3
File
src/Storage/SalesforceAuthTokenStorage.php
View source
<?php
namespace Drupal\salesforce\Storage;
use Drupal\Core\State\StateInterface;
use Drupal\salesforce\SalesforceAuthProviderPluginManager;
use Drupal\salesforce\Token\SalesforceToken;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
use OAuth\Common\Token\TokenInterface;
class SalesforceAuthTokenStorage implements SalesforceAuthTokenStorageInterface {
const TOKEN_STORAGE_PREFIX = "salesforce.auth_tokens";
const AUTH_STATE_STORAGE_PREFIX = "salesforce.auth_state";
const IDENTITY_STORAGE_PREFIX = "salesforce.auth_identity";
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
private function service() {
$oauth = SalesforceAuthProviderPluginManager::getAuthConfig();
return $oauth
->id();
}
public function updateToken() {
$this
->storeAccessToken($this
->service(), new SalesforceToken($this->state
->get('salesforce.access_token'), $this->state
->get('salesforce.refresh_token')));
return $this;
}
public function updateIdentity() {
$this
->storeIdentity($this
->service(), $this->state
->get('salesforce.identity'));
return $this;
}
protected static function getTokenStorageId($service) {
return self::TOKEN_STORAGE_PREFIX . '.' . $service;
}
protected static function getAuthStateStorageId($service) {
return self::AUTH_STATE_STORAGE_PREFIX . '.' . $service;
}
protected static function getIdentityStorageId($service) {
return self::IDENTITY_STORAGE_PREFIX . '.' . $service;
}
public function retrieveAccessToken($service) {
if ($token = $this->state
->get(self::getTokenStorageId($service))) {
return $token;
}
throw new TokenNotFoundException();
}
public function storeAccessToken($service, TokenInterface $token) {
$this->state
->set(self::getTokenStorageId($service), $token);
return $this;
}
public function hasAccessToken($service) {
try {
return !empty($this
->retrieveAccessToken($service));
} catch (TokenNotFoundException $e) {
return FALSE;
}
}
public function clearToken($service) {
$this->state
->delete(self::getTokenStorageId($service));
return $this;
}
public function clearAllTokens() {
return $this;
}
public function storeAuthorizationState($service, $state) {
$this->state
->set(self::getAuthStateStorageId($service), $state);
return $this;
}
public function hasAuthorizationState($service) {
return !empty($this
->retrieveAuthorizationState($service));
}
public function retrieveAuthorizationState($service) {
return $this->state
->get(self::getAuthStateStorageId($service));
}
public function clearAuthorizationState($service) {
$this->state
->delete(self::getAuthStateStorageId($service));
return $this;
}
public function clearAllAuthorizationStates() {
return $this;
}
public function storeIdentity($service, $identity) {
$this->state
->set(self::getIdentityStorageId($service), $identity);
return $this;
}
public function hasIdentity($service) {
return !empty($this
->retrieveIdentity($service));
}
public function retrieveIdentity($service) {
return $this->state
->get(self::getIdentityStorageId($service));
}
public function clearIdentity($service) {
$this->state
->delete(self::getIdentityStorageId($service));
return $this;
}
}