You are here

class RestfulAuthenticationToken in RESTful 7

@file Contains RestfulAuthenticationToken.

Hierarchy

Expanded class hierarchy of RestfulAuthenticationToken

1 string reference to 'RestfulAuthenticationToken'
token.inc in modules/restful_token_auth/plugins/authentication/token.inc

File

modules/restful_token_auth/plugins/authentication/RestfulAuthenticationToken.class.php, line 7
Contains RestfulAuthenticationToken.

View source
class RestfulAuthenticationToken extends \RestfulAuthenticationBase {

  /**
   * Extracting the token from a request by a key name, either dashed or not.
   *
   * @param $param_name
   *  The param name to check.
   * @param array $request
   *  The current request.
   *
   * @return string
   *  The token from the request or FALSE if token isn't exists.
   */
  protected function extractTokenFromRequest(array $request = array(), $param_name) {
    $key_name = !empty($param_name) ? $param_name : 'access_token';
    $dashed_key_name = str_replace('_', '-', $key_name);

    // Access token may be on the request, or in the headers
    // (may be a with dash instead of underscore).
    if (!empty($request['__application'][$key_name])) {
      return $request['__application'][$key_name];
    }
    elseif (!empty($request[$key_name])) {
      return $request[$key_name];
    }
    elseif (!empty($request['__application'][$dashed_key_name])) {
      return $request['__application'][$dashed_key_name];
    }
    elseif (!empty($request[$dashed_key_name])) {
      return $request[$dashed_key_name];
    }

    // Access token with that key name isn't exists.
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(array $request = array(), $method = \RestfulInterface::GET) {
    $options = $this
      ->getPluginKey('options');
    return $this
      ->extractTokenFromRequest($request, $options['param_name']);
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate(array $request = array(), $method = \RestfulInterface::GET) {
    $options = $this
      ->getPluginKey('options');
    $token = $this
      ->extractTokenFromRequest($request, $options['param_name']);

    // Check if there is a token that did not expire yet.
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'restful_token_auth')
      ->entityCondition('bundle', 'access_token')
      ->propertyCondition('token', $token)
      ->range(0, 1)
      ->execute();
    if (empty($result['restful_token_auth'])) {

      // No token exists.
      return;
    }
    $id = key($result['restful_token_auth']);
    $auth_token = entity_load_single('restful_token_auth', $id);
    if (!empty($auth_token->expire) && $auth_token->expire < REQUEST_TIME) {

      // Token is expired.
      if (variable_get('restful_token_auth_delete_expired_tokens', TRUE)) {

        // Token has expired, so we can delete this token.
        $auth_token
          ->delete();
      }
      return;
    }
    return user_load($auth_token->uid);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RestfulAuthenticationBase::$settings protected property Settings from the plugin definition.
RestfulAuthenticationBase::getName public function Get the name of the authentication plugin. Overrides RestfulAuthenticationInterface::getName
RestfulAuthenticationBase::__construct public function Constructor. Overrides RestfulPluginBase::__construct
RestfulAuthenticationToken::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 RestfulAuthenticationBase::applies
RestfulAuthenticationToken::authenticate public function Authenticate the request by trying to match a user. Overrides RestfulAuthenticationInterface::authenticate
RestfulAuthenticationToken::extractTokenFromRequest protected function Extracting the token from a request by a key name, either dashed or not.
RestfulPluginBase::$plugin protected property The plugin definition array.
RestfulPluginBase::getPlugin public function Gets information about the restful plugin. Overrides RestfulPluginInterface::getPlugin
RestfulPluginBase::getPluginKey public function Gets information about the restful plugin key. Overrides RestfulPluginInterface::getPluginKey
RestfulPluginBase::setPlugin public function Sets information about the restful plugin. Overrides RestfulPluginInterface::setPlugin
RestfulPluginBase::setPluginKey public function Gets information about the restful plugin key. Overrides RestfulPluginInterface::setPluginKey