You are here

class RefreshToken__1_0 in RESTful 7.2

Class RefreshToken__1_0 @package Drupal\restful_token_auth\Plugin\resource

Plugin annotation


@Resource(
  name = "refresh_token:1.0",
  resource = "refresh_token",
  label = "Refresh token authentication",
  description = "Export the refresh token authentication resource.",
  authenticationOptional = TRUE,
  dataProvider = {
    "entityType": "restful_token_auth",
    "bundles": {
      "access_token"
    },
  },
  formatter = "single_json",
  menuItem = "refresh-token",
  majorVersion = 1,
  minorVersion = 0
)

Hierarchy

Expanded class hierarchy of RefreshToken__1_0

File

modules/restful_token_auth/src/Plugin/resource/RefreshToken__1_0.php, line 39
Contains Drupal\restful_token_auth\Plugin\resource\RefreshToken__1_0.

Namespace

Drupal\restful_token_auth\Plugin\resource
View source
class RefreshToken__1_0 extends TokenAuthenticationBase implements ResourceInterface {

  /**
   * Overrides \RestfulBase::controllersInfo().
   */
  public function controllersInfo() {
    return array(
      '.*' => array(
        // Get or create a new token.
        RequestInterface::METHOD_GET => 'refreshToken',
      ),
    );
  }

  /**
   * Create a token for a user, and return its value.
   *
   * @param string $token
   *   The refresh token.
   *
   * @throws BadRequestException
   *
   * @return RestfulTokenAuth
   *   The new access token.
   */
  public function refreshToken($token) {

    // Check if there is a token that did not expire yet.

    /* @var \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntityInterface $data_provider */
    $data_provider = $this
      ->getDataProvider();
    $query = $data_provider
      ->EFQObject();
    $results = $query
      ->entityCondition('entity_type', $this->entityType)
      ->entityCondition('bundle', 'refresh_token')
      ->propertyCondition('token', $token)
      ->range(0, 1)
      ->execute();
    if (empty($results['restful_token_auth'])) {
      throw new BadRequestException('Invalid refresh token.');
    }

    // Remove the refresh token once used.
    $refresh_token = entity_load_single('restful_token_auth', key($results['restful_token_auth']));
    $uid = $refresh_token->uid;

    // Get the access token linked to this refresh token then do some cleanup.
    $access_token_query = new EntityFieldQuery();
    $access_token_reference = $access_token_query
      ->entityCondition('entity_type', 'restful_token_auth')
      ->entityCondition('bundle', 'access_token')
      ->fieldCondition('refresh_token_reference', 'target_id', $refresh_token->id)
      ->range(0, 1)
      ->execute();
    if (!empty($access_token_reference['restful_token_auth'])) {
      $access_token = key($access_token_reference['restful_token_auth']);
      entity_delete('restful_token_auth', $access_token);
    }
    $refresh_token
      ->delete();

    // Create the new access token and return it.

    /* @var \Drupal\restful_token_auth\Entity\RestfulTokenAuthController $controller */
    $controller = entity_get_controller($this
      ->getEntityType());
    $token = $controller
      ->generateAccessToken($uid);
    return $this
      ->view($token->id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurablePluginTrait::$instanceConfiguration protected property Plugin instance configuration.
ConfigurablePluginTrait::calculateDependencies public function
ConfigurablePluginTrait::getConfiguration public function
ConfigurablePluginTrait::setConfiguration public function
RefreshToken__1_0::controllersInfo public function Overrides \RestfulBase::controllersInfo(). Overrides Resource::controllersInfo
RefreshToken__1_0::refreshToken public function Create a token for a user, and return its value.
Resource::$authenticationManager protected property The authentication manager.
Resource::$dataProvider protected property The data provider.
Resource::$enabled protected property Indicates if the resource is enabled.
Resource::$fieldDefinitions protected property The field definition object.
Resource::$path protected property The requested path.
Resource::$request protected property The current request.
Resource::access public function Determine if user can access the handler. Overrides ResourceInterface::access 1
Resource::accessByAllowOrigin protected function Checks access based on the referer header and the allowOrigin setting.
Resource::create public function Basic implementation for create. Overrides ResourceInterface::create
Resource::defaultConfiguration public function Overrides ConfigurablePluginTrait::defaultConfiguration
Resource::disable public function Disable the resource. Overrides ResourceInterface::disable
Resource::discover public function Discovery controller callback. Overrides ResourceInterface::discover
Resource::doDelete public function Shorthand method to perform a quick DELETE request. Overrides ResourceInterface::doDelete
Resource::doGet public function Shorthand method to perform a quick GET request. Overrides ResourceInterface::doGet
Resource::doPatch public function Shorthand method to perform a quick PATCH request. Overrides ResourceInterface::doPatch
Resource::doPost public function Shorthand method to perform a quick POST request. Overrides ResourceInterface::doPost
Resource::doPut public function Shorthand method to perform a quick PUT request. Overrides ResourceInterface::doPut
Resource::doWrite private function
Resource::enable public function Enable the resource. Overrides ResourceInterface::enable
Resource::getAccount public function Get the user from for request. Overrides ResourceInterface::getAccount
Resource::getControllerFromPath public function Return the controller for a given path. Overrides ResourceInterface::getControllerFromPath
Resource::getControllers public function Gets the controllers for this resource. Overrides ResourceInterface::getControllers
Resource::getDataProvider public function Gets the data provider. Overrides ResourceInterface::getDataProvider
Resource::getFieldDefinitions public function Gets the field definitions. Overrides ResourceInterface::getFieldDefinitions
Resource::getPath public function Gets the path of the resource. Overrides ResourceInterface::getPath
Resource::getRequest public function Get the request object. Overrides ResourceInterface::getRequest
Resource::getResourceMachineName public function Gets the resource machine name. Overrides ResourceInterface::getResourceMachineName
Resource::getResourceName public function Gets the resource name. Overrides ResourceInterface::getResourceName
Resource::getUrl public function Helper method; Get the URL of the resource and query strings. Overrides ResourceInterface::getUrl
Resource::getVersion public function Return array keyed with the major and minor version of the resource. Overrides ResourceInterface::getVersion
Resource::index public function Basic implementation for listing. Overrides ResourceInterface::index 1
Resource::initAuthenticationManager protected function Initializes the authentication manager and adds the appropriate providers.
Resource::isEnabled public function Checks if the resource is enabled. Overrides ResourceInterface::isEnabled
Resource::preflight protected function Adds the Allowed-Origin headers.
Resource::process public function Controller function that passes the data along and executes right action. Overrides ResourceInterface::process
Resource::remove public function Basic implementation for update. Overrides ResourceInterface::remove
Resource::replace public function Basic implementation for update. Overrides ResourceInterface::replace
Resource::setAccount public function Overrides ResourceInterface::setAccount
Resource::setDataProvider public function Sets the data provider. Overrides ResourceInterface::setDataProvider
Resource::setFieldDefinitions public function Sets the field definitions. Overrides ResourceInterface::setFieldDefinitions
Resource::setPath public function Sets the path of the resource. Overrides ResourceInterface::setPath
Resource::setPluginDefinition public function Sets the plugin definition to the provided array. Overrides ResourceInterface::setPluginDefinition
Resource::setRequest public function Sets the request object. Overrides ResourceInterface::setRequest
Resource::switchUserBack public function Switches the user back from the original user for the session. Overrides ResourceInterface::switchUserBack 1
Resource::update public function Basic implementation for update. Overrides ResourceInterface::update
Resource::versionedUrl public function Gets a resource URL based on the current version. Overrides ResourceInterface::versionedUrl
Resource::view public function Basic implementation for view. Overrides ResourceInterface::view
ResourceEntity::$bundles protected property The entity bundles.
ResourceEntity::$entityType protected property The entity type.
ResourceEntity::dataProviderClassName protected function Data provider class. Overrides Resource::dataProviderClassName 3
ResourceEntity::dataProviderFactory public function Data provider factory. Overrides Resource::dataProviderFactory
ResourceEntity::getBundles public function Gets the entity bundle.
ResourceEntity::getEntitySelf public function Get the "self" url.
ResourceEntity::getEntityType public function Gets the entity type.
ResourceEntity::processPublicFields protected function Get the public fields with the default values applied to them. Overrides Resource::processPublicFields 2
ResourceEntity::viewModeFields protected function Get the public fields with default values based on view mode information.
ResourceEntity::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides Resource::__construct 2
ResourceInterface::IDS_SEPARATOR constant The string that separates multiple ids.
TokenAuthenticationBase::getTokenFromEntity public static function Get the token string from the token entity.
TokenAuthenticationBase::intervalInSeconds public static function Process callback helper to get the time difference in seconds.
TokenAuthenticationBase::publicFields public function Overrides ResourceEntity::publicFields(). Overrides ResourceEntity::publicFields