You are here

class SalesforceAuthTokenStorage in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 src/Storage/SalesforceAuthTokenStorage.php \Drupal\salesforce\Storage\SalesforceAuthTokenStorage
  2. 5.0.x src/Storage/SalesforceAuthTokenStorage.php \Drupal\salesforce\Storage\SalesforceAuthTokenStorage

Salesforce auth token storage.

Hierarchy

Expanded class hierarchy of SalesforceAuthTokenStorage

1 string reference to 'SalesforceAuthTokenStorage'
salesforce.services.yml in ./salesforce.services.yml
salesforce.services.yml
1 service uses SalesforceAuthTokenStorage
salesforce.auth_token_storage in ./salesforce.services.yml
Drupal\salesforce\Storage\SalesforceAuthTokenStorage

File

src/Storage/SalesforceAuthTokenStorage.php, line 14

Namespace

Drupal\salesforce\Storage
View source
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";

  /**
   * State kv storage.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * SalesforceAuthTokenStorage constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   State service.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * Token storage key for given service.
   *
   * @return string
   *   Token storage key for given service.
   */
  protected static function getTokenStorageId($service) {
    return static::TOKEN_STORAGE_PREFIX . '.' . $service;
  }

  /**
   * Auth state storage key for given service.
   *
   * @return string
   *   Auth state storage key for given service.
   */
  protected static function getAuthStateStorageId($service) {
    return static::AUTH_STATE_STORAGE_PREFIX . '.' . $service;
  }

  /**
   * Identity storage key for given service.
   *
   * @return string
   *   Identity storage key for given service.
   */
  protected static function getIdentityStorageId($service) {
    return static::IDENTITY_STORAGE_PREFIX . '.' . $service;
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveAccessToken($service) {
    if ($token = $this->state
      ->get(static::getTokenStorageId($service))) {
      return $token;
    }
    throw new TokenNotFoundException();
  }

  /**
   * {@inheritdoc}
   */
  public function storeAccessToken($service, TokenInterface $token) {

    // Salesforce API doesn't return a refresh token when refreshing.
    // If $token refresh token is null, retain existing instead of overwriting.
    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;
  }

  /**
   * {@inheritdoc}
   */
  public function hasAccessToken($service) {
    try {
      return !empty($this
        ->retrieveAccessToken($service));
    } catch (TokenNotFoundException $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function clearToken($service) {
    $this->state
      ->delete(static::getTokenStorageId($service));
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function clearAllTokens() {

    // noop. We don't do this. Only here to satisfy interface.
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function storeAuthorizationState($service, $state) {
    $this->state
      ->set(static::getAuthStateStorageId($service), $state);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasAuthorizationState($service) {
    return !empty($this
      ->retrieveAuthorizationState($service));
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveAuthorizationState($service) {
    return $this->state
      ->get(static::getAuthStateStorageId($service));
  }

  /**
   * {@inheritdoc}
   */
  public function clearAuthorizationState($service) {
    $this->state
      ->delete(static::getAuthStateStorageId($service));
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function clearAllAuthorizationStates() {

    // noop. only here to satisfy interface. Use clearAuthorizationState().
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function storeIdentity($service, SalesforceIdentityInterface $identity) {
    $this->state
      ->set(static::getIdentityStorageId($service), $identity);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasIdentity($service) {
    return !empty($this
      ->retrieveIdentity($service));
  }

  /**
   * {@inheritdoc}
   */
  public function retrieveIdentity($service) {

    // Backwards compatibility in case someone missed the hook_update.
    $identity = $this->state
      ->get(static::getIdentityStorageId($service));
    if (is_array($identity)) {
      $identity = SalesforceIdentity::create($identity);
    }
    return $identity;
  }

  /**
   * {@inheritdoc}
   */
  public function clearIdentity($service) {
    $this->state
      ->delete(static::getIdentityStorageId($service));
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SalesforceAuthTokenStorage::$state protected property State kv storage.
SalesforceAuthTokenStorage::AUTH_STATE_STORAGE_PREFIX constant
SalesforceAuthTokenStorage::clearAllAuthorizationStates public function
SalesforceAuthTokenStorage::clearAllTokens public function
SalesforceAuthTokenStorage::clearAuthorizationState public function
SalesforceAuthTokenStorage::clearIdentity public function Clear identity for service. Overrides SalesforceAuthTokenStorageInterface::clearIdentity
SalesforceAuthTokenStorage::clearToken public function
SalesforceAuthTokenStorage::getAuthStateStorageId protected static function Auth state storage key for given service.
SalesforceAuthTokenStorage::getIdentityStorageId protected static function Identity storage key for given service.
SalesforceAuthTokenStorage::getTokenStorageId protected static function Token storage key for given service.
SalesforceAuthTokenStorage::hasAccessToken public function
SalesforceAuthTokenStorage::hasAuthorizationState public function
SalesforceAuthTokenStorage::hasIdentity public function Return boolean indicating whether this service has an identity. Overrides SalesforceAuthTokenStorageInterface::hasIdentity
SalesforceAuthTokenStorage::IDENTITY_STORAGE_PREFIX constant
SalesforceAuthTokenStorage::retrieveAccessToken public function
SalesforceAuthTokenStorage::retrieveAuthorizationState public function
SalesforceAuthTokenStorage::retrieveIdentity public function Identity for the given service. Overrides SalesforceAuthTokenStorageInterface::retrieveIdentity
SalesforceAuthTokenStorage::storeAccessToken public function
SalesforceAuthTokenStorage::storeAuthorizationState public function
SalesforceAuthTokenStorage::storeIdentity public function Setter for identity storage. Overrides SalesforceAuthTokenStorageInterface::storeIdentity
SalesforceAuthTokenStorage::TOKEN_STORAGE_PREFIX constant
SalesforceAuthTokenStorage::__construct public function SalesforceAuthTokenStorage constructor.