You are here

class CookieAuthentication in RESTful 7.2

Class CookieAuthentication @package Drupal\restful\Plugin\authentication

Plugin annotation


@Authentication(
  id = "cookie",
  label = "Cookie based authentication",
  description = "Authenticate requests based on the user cookie.",
)

Hierarchy

Expanded class hierarchy of CookieAuthentication

File

src/Plugin/authentication/CookieAuthentication.php, line 24
Contains \Drupal\restful\Plugin\authentication\CookieAuthentication

Namespace

Drupal\restful\Plugin\authentication
View source
class CookieAuthentication extends Authentication {

  /**
   * {@inheritdoc}
   */
  public function authenticate(RequestInterface $request) {
    if (!drupal_session_started() && !$this
      ->isCli($request)) {
      return NULL;
    }
    global $user;
    $account = user_load($user->uid);
    if (!$request::isWriteMethod($request
      ->getMethod()) || $request
      ->getApplicationData('rest_call')) {

      // Request is done via API not CURL, or not a write operation, so we don't
      // need to check for a CSRF token.
      return $account;
    }
    if (!RestfulManager::isRestfulPath($request)) {
      return $account;
    }
    if (!$request
      ->getCsrfToken()) {
      throw new BadRequestException('No CSRF token passed in the HTTP header.');
    }
    if (!drupal_valid_token($request
      ->getCsrfToken(), Authentication::TOKEN_VALUE)) {
      throw new ForbiddenException('CSRF token validation failed.');
    }

    // CSRF validation passed.
    return $account;
  }

  /**
   * Detects whether the script is running from a command line environment.
   *
   * @param RequestInterface $request.
   *   The request.
   *
   * @return bool
   *   TRUE if a command line environment is detected. FALSE otherwise.
   */
  protected function isCli(RequestInterface $request) {

    // Needed to detect if run-tests.sh is running the tests.
    $cli = $request
      ->getHeaders()
      ->get('User-Agent')
      ->getValueString() == 'Drupal command line';
    return $cli || drupal_is_cli();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Authentication::$settings protected property Settings from the plugin definition.
Authentication::applies public function Determines if the request can be checked for authentication. For example, when authenticating with HTTP header, return FALSE if the header values do not exist. Overrides AuthenticationInterface::applies 3
Authentication::getName public function Get the name of the authentication plugin. Overrides AuthenticationInterface::getName
Authentication::TOKEN_VALUE constant Token value for token generation functions.
ConfigurablePluginTrait::$instanceConfiguration protected property Plugin instance configuration.
ConfigurablePluginTrait::calculateDependencies public function
ConfigurablePluginTrait::defaultConfiguration public function 1
ConfigurablePluginTrait::getConfiguration public function
ConfigurablePluginTrait::setConfiguration public function
CookieAuthentication::authenticate public function Authenticate the request by trying to match a user. Overrides AuthenticationInterface::authenticate
CookieAuthentication::isCli protected function Detects whether the script is running from a command line environment.