You are here

restful_token_auth.module in RESTful 7.2

Same filename and directory in other branches
  1. 7 modules/restful_token_auth/restful_token_auth.module

RESTful token authentication.

File

modules/restful_token_auth/restful_token_auth.module
View source
<?php

/**
 * @file
 * RESTful token authentication.
 */
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Authentication\AuthenticationManager;
use Drupal\restful\Plugin\resource\ResourceInterface;

/**
 * Implements hook_menu().
 */
function restful_token_auth_menu() {

  // Add administration page.
  $items['admin/config/services/restful/token-auth'] = array(
    'title' => 'Token Authentication',
    'description' => 'Administer the RESTful Token Authentication module.',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'restful_token_auth_admin_settings',
    ),
    'access arguments' => array(
      'administer restful',
    ),
    'file' => 'restful_token_auth.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_restful_parse_request_alter().
 */
function restful_token_auth_restful_parse_request_alter(RequestInterface &$request) {

  // In this hook we cannot rely on the service to be ready.
  $authentication_manager = new AuthenticationManager();
  try {

    // If the the authentication provider have not been added yet, add it.
    $authentication_manager
      ->addAuthenticationProvider('token');
    $plugin = $authentication_manager
      ->getPlugin('token');
  } catch (PluginNotFoundException $e) {
    watchdog_exception('restful_token_auth', $e);
    return;
  }
  $plugin_definition = $plugin
    ->getPluginDefinition();
  $param_name = $plugin_definition['options']['paramName'];
  $header = $request
    ->getHeaders()
    ->get($param_name);
  $request
    ->setApplicationData($param_name, $header
    ->getValueString());
}

/**
 * Implements hook_entity_info().
 */
function restful_token_auth_entity_info() {
  $items['restful_token_auth'] = array(
    'label' => t('Authentication token'),
    'entity class' => '\\Drupal\\restful_token_auth\\Entity\\RestfulTokenAuth',
    'controller class' => '\\Drupal\\restful_token_auth\\Entity\\RestfulTokenAuthController',
    'base table' => 'restful_token_auth',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
      'label' => 'name',
      'bundle' => 'type',
    ),
    'bundles' => array(
      'access_token' => array(
        'label' => t('Access token'),
      ),
      'refresh_token' => array(
        'label' => t('Refresh token'),
      ),
    ),
    'bundle keys' => array(
      'bundle' => 'type',
    ),
    'module' => 'restful_token_auth',
    'entity cache' => module_exists('entitycache'),
  );
  return $items;
}

/**
 * Implements hook_cron().
 *
 * Delete expired token auth entities.
 */
function restful_token_auth_cron() {
  if (!variable_get('restful_token_auth_delete_expired_tokens', TRUE)) {

    // We should not delete expired tokens.
    return;
  }
  $query = new \EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'restful_token_auth')
    ->propertyCondition('expire', REQUEST_TIME, '<')
    ->range(0, 50)
    ->execute();
  if (empty($result['restful_token_auth'])) {

    // No expired tokens.
    return;
  }
  $ids = array_keys($result['restful_token_auth']);
  entity_delete_multiple('restful_token_auth', $ids);
}

/**
 * Implements hook_restful_resource_alter().
 */
function restful_token_auth_restful_resource_alter(ResourceInterface &$resource) {
  $plugin_definition = $resource
    ->getPluginDefinition();
  if (empty($plugin_definition['dataProvider']['entityType']) || $plugin_definition['dataProvider']['entityType'] != 'restful_token_auth' || !empty($plugin_definition['formatter'])) {
    return;
  }

  // If this resource is based on access token entities and does not have an
  // explicit formatter attached to it, then use the single_json formatter.
  $plugin_definition['formatter'] = 'single_json';
  $resource
    ->setPluginDefinition($plugin_definition);
}

/**
 * Implements hook_user_update().
 */
function restful_token_auth_user_update(&$edit, $account, $category) {
  if ($edit['status']) {
    return;
  }
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'restful_token_auth')
    ->propertyCondition('uid', $account->uid)
    ->execute();
  if (empty($result['restful_token_auth'])) {
    return;
  }
  entity_delete_multiple('restful_token_auth', array_keys($result['restful_token_auth']));
}