Hi,

When trying to secure any custom Views Rest Export (API) with other authentication methods than Basic Auth, the following error occurs:

User:
Notice: Undefined index: user in Drupal\rest\Plugin\views\display\RestExport->Drupal\rest\Plugin\views\display\{closure}() (line 460 of core/modules/rest/src/Plugin/views/display/RestExport.php).
Drupal\rest\Plugin\views\display\RestExport->Drupal\rest\Plugin\views\display\{closure}('user')
array_map(Object, Array) (Line: 461)
Drupal\rest\Plugin\views\display\RestExport->calculateDependencies() (Line: 47)

Simple OAuth:
Notice: Undefined index: simple_oauth in Drupal\rest\Plugin\views\display\RestExport->Drupal\rest\Plugin\views\display\{closure}() (line 460 of core/modules/rest/src/Plugin/views/display/RestExport.php).
Drupal\rest\Plugin\views\display\RestExport->Drupal\rest\Plugin\views\display\{closure}('simple_oauth')
array_map(Object, Array) (Line: 461)
Drupal\rest\Plugin\views\display\RestExport->calculateDependencies() (Line: 47)

This happens when you select the authentication method and save the view. I have attached to simple API-attachementsfor the Views Module.

It looks like the function calculateDependencies() in the RestExport.php is using the array values since array_map is used instead of the array_keys:

  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    $dependencies += ['module' => []];
    $modules = array_map(function ($authentication_provider) {
      // @TODO: Remove - Search in the authenticationProviders for the key of the selected options
      // @TODO: Remove - Uncomment Below to use the keys instead of the values
      // $authentication_provider = array_search($authentication_provider, $this->authenticationProviders);
      return $this->authenticationProviders[$authentication_provider];
    }, $this->getOption('auth'));
    $dependencies['module'] = array_merge($dependencies['module'], $modules);
    return $dependencies;
  }

When applying the fix above to use the array keys instead of the array values, unfortunately the API-route is still not accessible. The response from the API is:
{ "message": "The used authentication method is not allowed on this route."}

Am I missing something to activate the authentication on a custom route? Via Rest-UI I cannot grant access to this API

Comments

niekheezemans created an issue. See original summary.

niekheezemans’s picture

Title: Rest Export Authentication Error (USer, Oauth) » Rest Export Authentication Error (User, Oauth)
Component: views.module » rest.module

Updated to the rest.module instead of the views.module

wim leers’s picture

Title: Rest Export Authentication Error (User, Oauth) » REST views: Authentication Error (User, Oauth)
Issue tags: -REST, -REST API, -rest server, -views, -views ui

Thanks for reporting this! I think the steps to reproduce are clear. I'll ping the Simple OAuth module maintainer to try to reproduce this.

dawehner’s picture

This sounds really similar to #2825204: REST views: authentication is broken for me

wim leers’s picture

Title: REST views: Authentication Error (User, Oauth) » REST views: Authentication Error when using Simple OAuth authentication
Status: Active » Closed (duplicate)
Related issues: +#2825204: REST views: authentication is broken

Riiiight! I knew it sounded familiar!

dongen1289’s picture

Hi, I'm having the exact same problem in Drupal 8.4.0 and Simple_auth. Not sure to log it here or with simple.

when trying to add 'simple_oauth' as authentication provider to the view error is:

Notice: Undefined index: simple_oauth in Drupal\rest\Plugin\views\display\RestExport->Drupal\rest\Plugin\views\display\{closure}() (line 461 of core/modules/rest/src/Plugin/views/display/RestExport.php).
Drupal\rest\Plugin\views\display\RestExport->Drupal\rest\Plugin\views\display\{closure}('simple_oauth')
array_map(Object, Array) (Line: 462)
Drupal\rest\Plugin\views\display\RestExport->calculateDependencies() (Line: 47)
Drupal\Core\Config\Entity\ConfigEntityBase->calculatePluginDependencies(Object) (Line: 281)
Drupal\views\Entity\View->calculateDependencies() (Line: 346)
Drupal\Core\Config\Entity\ConfigEntityBase->preSave(Object) (Line: 291)
Drupal\views\Entity\View->preSave(Object) (Line: 434)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 389)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 259)
Drupal\Core\Config\Entity\ConfigEntityStorage->save(Object) (Line: 377)
Drupal\Core\Entity\Entity->save() (Line: 637)
Drupal\Core\Config\Entity\ConfigEntityBase->save() (Line: 989)
Drupal\views_ui\ViewUI->save() (Line: 324)
Drupal\views_ui\ViewEditForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('view_edit_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('view_edit_form', Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'edit', Array) (Line: 226)
Drupal\views_ui\Controller\ViewsUIController->edit(Object, NULL)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 576)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 153)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 67)
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 657)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

if continuing despite error the API reports that the authentication method is not supported for the end point.
Driving me nuts. How can I fix??

adrianpintilie’s picture

Wim, I am not sure is related. That issue talks about the export. The current bug is actually different. simple_oauth doesn't work with REST Views.

adrianpintilie’s picture

Applying this patch fixes the issue: https://www.drupal.org/node/2825204#comment-12306027

dawehner’s picture

Note: we would need an update written for simple_oauth as well ...

wim leers’s picture

Thanks, @adrianpintilie, for confirming that #2825204: REST views: authentication is broken fixes the problem.

@dawehner: why?