symfonyjwtmercure

Mercure/Symfony: Authorization Error with JWT Key


I am using mercure from the symfony binary. After setting up a new project via

symfony new mercure
cd mercure
symfony composer req make mercure annotations
symfony console make:controller
symfony server:start

I write some minimal code into the newly created controller:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;

class MainController extends AbstractController
{
    /**
     * @Route("/", name="main")
     */
    public function index(HubInterface $hub): Response
    {
        $update = new Update(
            'http://mercure/asdf',
            json_encode(['message' => 'heyo']),
        );
        $hub->publish($update);

        return $this->json([
            'message' => 'Welcome to your new controller!',
            'path' => 'src/Controller/MainController.php',
        ]);
    }
}

Then, I generate an JWT key from here (linked from the symfony documentation) and enter !ChangeMe! in the bottom right, as this is the key used by the symfony binary. The JWT key is then set in .env:

MERCURE_URL=https://127.0.0.1:8000/.well-known/mercure
MERCURE_PUBLIC_URL=https://127.0.0.1:8000/.well-known/mercure
MERCURE_JWT_SECRET=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.obDjwCgqtPuIvwBlTxUEmibbBf0zypKCNzNKP7Op2UM

After restarting the server and going to localhost:8000, I get Failed to send an update. and HTTP/2 401 returned for "https://localhost:8000/.well-known/mercure"., which is probably because the key doesn't work correctly. Error on server: MERCUR Topic selectors not matched, not provided or authorization error

By setting MERCURE_JWT_SECRET="!ChangeMe!" (without encryption), it does work if the site is visited from the machine the server runs on, but from no other machine in the same network (via 192.168.XXX.XXX:8000).

What am I doing wrong?


Solution

  • It turns out that it was a problem with the configuration on my operating system. After pushing the project onto another machine, everything worked fine.