1

i am relatively new to drupal . thus i am trying to make an sso system work . i have this parent domain "domain.com" and the drupal site is running on sub.domain.com  . i can read the cookies and all . i come from a laravel background . i made a custom module following the docs . and i managed to make a middleware that intercepts all incoming requests . but i only need it to execute the actual middleware logic for anonymous useres which i am failing to do because var_dump(\Drupal::currentUser()->isAnonymous()); returns true no matter how low my middleware priority is . and it doesnt matter if i read the user from that statement or using the current_user service . its still and empty object because in my mind the middleware is executing before the session module

my services file. 

services:

  sso.manager:

    class: Drupal\sso\StackMiddleware\SsoManager

    tags:

      - { name: backend_overridable }

  http_middleware.sso:

    class: Drupal\sso\StackMiddleware\SsoMiddleware

    arguments: ['@current_user','@sso.manager']

    tags:

      - { name: http_middleware, priority: 20 } 

i set the priority to 20 because the sessions function at 50 . so it shoud execute after the session middleware is done right ? am i missing something?

my handle method

<?php

namespace Drupal\sso\StackMiddleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\sso\StackMiddleware\SsoManagerInterface;

/**
 * sso Middleware.
 *
 * @package Drupal\sso
 */
class SsoMiddleware implements HttpKernelInterface
{

    /**
     * The wrapped HTTP kernel.
     *
     * @var \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    protected $user;

    /**
     * The wrapped HTTP kernel.
     *
     * @var \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    protected $app;

    /**
     * sso manager instance.
     *
     * @var \Drupal\sso\StackMiddleware\SsoManagerInterface
     */
    protected $manager;

    /**
     * Constructs sso Middleware.
     *
     * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
     *   The wrapper HTTP kernel.
     * @param \Drupal\sso\SsoManagerInterface $manager
     *   The sso manager interface.
     */
    public function __construct(HttpKernelInterface $app, AccountProxy $user, SsoManagerInterface $manager)
    {
        $this->app = $app;
        $this->manager = $manager;
        $this->user = $user;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(Request $request, $type = self::SUB_REQUEST, $catch = TRUE)
    {

        $enabled = $this->manager->isEnabled();
        // var_dump($this->user);//service doesnt return one either 
        // var_dump(\Drupal::currentUser()->isAnonymous()); //no user

        // Only run the sso if it's not enabled.
        if ($enabled === TRUE && \Drupal::currentUser()->isAnonymous()) {
            $service_request = $this->manager->isServiceRequest($request);
            // If this is a Web Service request then dont run the sso service.
            if ($service_request === FALSE) {

                // If this is not a service request and the sso service is enabled
                // then get all the settings and send it to sso manager.
                $ssoPass = $this->manager->CheckValidUserCookie($request);
                if ($ssoPass === false) {
                    echo "user doesnt have a cookie let him be";
                    return $this->manager->respond();
                }
            }
        }
        return $this->app->handle($request, $type, $catch);
    }
}

    enter code here
2
  • It seems to me more like you want a custom event subscriber, that reacts to the request event. But I don't know about the middleware API, so I may be wrong on this.
    – Jaypan
    Commented Dec 8, 2019 at 1:26
  • The middleware API of Drupal is simply an option to define a middleware in a Drupal module, so that you don't need to hack core files to add a middleware. Other than that a middleware is by definition outside of the application kernel and has no access to states inside.
    – 4uk4
    Commented Dec 8, 2019 at 16:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.