Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public static function controllersInfo() {
public function getOrCreateToken() {
$entity_type = $this->getEntityType();
$account = $this->getAccount();

if (!$account->uid) {
// If this handler was called programmatically we want to make sure the
// account is of an authenticated user.
throw new RestfulBadRequestException('Only authenticated users can have an access token.');
}

// Check if there is a token that did not expire yet.
$query = new EntityFieldQuery();
$result = $query
Expand Down
11 changes: 8 additions & 3 deletions tests/RestfulCsrfTokenTestCase.test
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ class RestfulCsrfTokenTestCase extends RestfulCurlBaseTestCase {
'edit any article content',
'delete any article content',
);
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
$user = $account;
$this->account = $this->drupalCreateUser($permissions);
$user = $this->account;

// Check CSRF is not checked for read operations.
$this->checkCsrfRequest(array(\RestfulInterface::GET), FALSE);
Expand Down Expand Up @@ -74,6 +73,7 @@ class RestfulCsrfTokenTestCase extends RestfulCurlBaseTestCase {
\RestfulInterface::PATCH,
\RestfulInterface::DELETE
);

$this->checkCsrfRequest($write_operations, FALSE, FALSE);
}

Expand All @@ -89,6 +89,11 @@ class RestfulCsrfTokenTestCase extends RestfulCurlBaseTestCase {
* Determine if a user should be created and logged in. Defaults to TRUE.
*/
protected function checkCsrfRequest($methods = array(), $csrf_required, $auth_user = TRUE) {
// Log-in user if needed.
if ($auth_user) {
$this->drupalLogin($this->account);
}

$params['@role'] = $auth_user ? 'authenticated' : 'anonymous';

foreach ($methods as $method) {
Expand Down