SalesforceAuthTokenStorage.php in Salesforce Suite 8.4
File
src/Storage/SalesforceAuthTokenStorage.php
View source
<?php
namespace Drupal\salesforce\Storage;
use Drupal\Core\State\StateInterface;
use Drupal\salesforce\Rest\SalesforceIdentity;
use Drupal\salesforce\Rest\SalesforceIdentityInterface;
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;
}
protected static function getTokenStorageId($service) {
return static::TOKEN_STORAGE_PREFIX . '.' . $service;
}
protected static function getAuthStateStorageId($service) {
return static::AUTH_STATE_STORAGE_PREFIX . '.' . $service;
}
protected static function getIdentityStorageId($service) {
return static::IDENTITY_STORAGE_PREFIX . '.' . $service;
}
public function retrieveAccessToken($service) {
if ($token = $this->state
->get(static::getTokenStorageId($service))) {
return $token;
}
throw new TokenNotFoundException();
}
public function storeAccessToken($service, TokenInterface $token) {
if (!$token
->getRefreshToken()) {
$oldToken = $this->state
->get(static::getTokenStorageId($service));
if ($oldToken) {
$token
->setRefreshToken($oldToken
->getRefreshToken());
}
}
$this->state
->set(static::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(static::getTokenStorageId($service));
return $this;
}
public function clearAllTokens() {
return $this;
}
public function storeAuthorizationState($service, $state) {
$this->state
->set(static::getAuthStateStorageId($service), $state);
return $this;
}
public function hasAuthorizationState($service) {
return !empty($this
->retrieveAuthorizationState($service));
}
public function retrieveAuthorizationState($service) {
return $this->state
->get(static::getAuthStateStorageId($service));
}
public function clearAuthorizationState($service) {
$this->state
->delete(static::getAuthStateStorageId($service));
return $this;
}
public function clearAllAuthorizationStates() {
return $this;
}
public function storeIdentity($service, SalesforceIdentityInterface $identity) {
$this->state
->set(static::getIdentityStorageId($service), $identity);
return $this;
}
public function hasIdentity($service) {
return !empty($this
->retrieveIdentity($service));
}
public function retrieveIdentity($service) {
$identity = $this->state
->get(static::getIdentityStorageId($service));
if (is_array($identity)) {
$identity = SalesforceIdentity::create($identity);
}
return $identity;
}
public function clearIdentity($service) {
$this->state
->delete(static::getIdentityStorageId($service));
return $this;
}
}