You are here

public function RestfulAuthenticationToken::authenticate in RESTful 7

Authenticate the request by trying to match a user.

Parameters

array $request: The request.

string $method: The HTTP method. Defaults to "get".

Return value

\stdClass|null The user object.

Overrides RestfulAuthenticationInterface::authenticate

File

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

Class

RestfulAuthenticationToken
@file Contains RestfulAuthenticationToken.

Code

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);
}