0

I am trying to implement the LexikJwt package for my new API: https://github.com/lexik/LexikJWTAuthenticationBundle

I just added my custom command to create a proper JWT token including my default roles for local testing. The next step would be to actually protect my routes. What i can't find however is exactly on how to do that. This is my current controller implementation using annotation routes

/**
 * @Route(
 *     "/search",
 *     name="search",
 *     methods={"GET"},
 * )
 */
class Search
{
    /**
     * @param AddressService $addressService
     * @param Request $request
     * @return JsonApiResponse
     * @throws Exception
     */
    public function __invoke(AddressService $addressService, Request $request)
    {
        $address = $addressService->createFromParams($request->query->all());

        try {
            $addressCollection = $addressService->search($address);
        } catch (AddressNotFoundException $e) {
            $addressCollection = [];
        }

        return new JsonApiResponse($addressCollection);
    }
}

However the docs do not say anything about annotation routes and only on yml configs on security firewalls. The main thing i need is the token to be verified:

  • Is the token valid (matching the public key)
  • Is the token not expired
  • Does the route match the given roles

For example, i want the code above, which is an address service to match only if the token matches above and the token holds the role or scope: address:search.

Hope someone can help,

Pim

Dirkos
  • 488
  • 1
  • 10
  • 33
  • 2
    Have you added the config for the the bundle in `config/packages/security.yaml`? This should take care of checking if the token is valid or not already. Regarding blocking the route depending on roles, that's out of the scope of the bundle. You need to either use Symfony roles and restrict access in `security. access_control` or use Symfony role voters for more flexibility https://symfony.com/doc/current/security/voters.html – thomas.drbg Oct 22 '19 at 06:50

1 Answers1

0

The issue was that the role should start with ROLE_. It is possible to override it though but this was the use case.

Dirkos
  • 488
  • 1
  • 10
  • 33