RefreshToken__1_0.php in RESTful 7.2
File
modules/restful_token_auth/src/Plugin/resource/RefreshToken__1_0.php
View source
<?php
namespace Drupal\restful_token_auth\Plugin\resource;
use Drupal\restful\Exception\BadRequestException;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Plugin\resource\ResourceInterface;
use Drupal\restful\Util\EntityFieldQuery;
use Drupal\restful_token_auth\Entity\RestfulTokenAuth;
class RefreshToken__1_0 extends TokenAuthenticationBase implements ResourceInterface {
public function controllersInfo() {
return array(
'.*' => array(
RequestInterface::METHOD_GET => 'refreshToken',
),
);
}
public function refreshToken($token) {
$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.');
}
$refresh_token = entity_load_single('restful_token_auth', key($results['restful_token_auth']));
$uid = $refresh_token->uid;
$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();
$controller = entity_get_controller($this
->getEntityType());
$token = $controller
->generateAccessToken($uid);
return $this
->view($token->id);
}
}
Classes
Name |
Description |
RefreshToken__1_0 |
Class RefreshToken__1_0
@package Drupal\restful_token_auth\Plugin\resource |