LexikJWTAuthenticationBundle icon indicating copy to clipboard operation
LexikJWTAuthenticationBundle copied to clipboard

Fatal error Cannot declare trait ForwardCompatAuthenticatorTrait

Open rpilote opened this issue 2 years ago • 16 comments

I got and error after install JWT package


Fatal error: Cannot declare trait Lexik\Bundle\JWTAuthenticationBundle\Security\Authenticator\ForwardCompatAuthenticatorTrait, because the name is already in use in /srv/api/vendor/lexik/jwt-authentication-bundle/Security/Authenticator/ForwardCompatAuthenticatorTrait.php(13) : eval()'d code on line 10

Is some one can't help me I can't found any clue why i gote this error

i try with php 8.0.2 and php 8.1 symfony 6.0 and both does work

image

rpilote avatar Mar 17 '22 23:03 rpilote

This seems related to preloading. I started getting this recently after some upgrades, and I disabled preloading temporarily until I figured out what's going on.

The issue seems to happen with preload activated and Symfony 6+.

On Symfony 5.4, all good.

yivi avatar Mar 18 '22 11:03 yivi

I start a new projet and i still got the error.

this is my security.yaml :

api/config/packages/security.yaml

security: # https://symfony.com/doc/current/security.html#c-hashing-passwords password_hashers: App\Entity\UserAccount: 'auto'

# https://symfony.com/doc/current/security/authenticator_manager.html
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
    # used to reload user from session & other features (e.g. switch_user)
    app_user_provider:
        entity:
            class: App\Entity\UserAccount
            property: username 

firewalls:
    dev:
        pattern: ^/_(profiler|wdt)
        security: false
        
    api:
        pattern: ^/api/
        stateless: true
        provider: app_user_provider
        jwt: ~
        
    main:
        stateless: true
        provider: app_user_provider
        json_login:
            check_path: /authentication_token
            username_path: username
            password_path: password
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure

access_control:
    - { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI
    - { path: ^/authentication_token, roles: PUBLIC_ACCESS }
    - { path: ^/, roles: IS_AUTHENTICATED_FULLY }

i can login and got the token but i can't access any route like /api/user_acocunts when i try to access to the route i gote

Fatal error: Cannot declare trait Lexik\Bundle\JWTAuthenticationBundle\Security\Authenticator\ForwardCompatAuthenticatorTrait, because the name is already in use in /srv/api/vendor/lexik/jwt-authentication-bundle/Security/Authenticator/ForwardCompatAuthenticatorTrait.php(13) : eval()'d code on line 10

I think i follow the documentation but at this point know I am lost.

rpilote avatar Mar 25 '22 12:03 rpilote

This seems related to preloading. I started getting this recently after some upgrades, and I disabled preloading temporarily until I figured out what's going on.

The issue seems to happen with preload activated and Symfony 6+.

On Symfony 5.4, all good.

Have you figure out what's wrong with the error?

fd6130 avatar Apr 14 '22 13:04 fd6130

@fd6130 https://github.com/vimeo/psalm/issues/7863

atlance avatar May 20 '22 03:05 atlance

Same for me using LexikJWTAuthenticationBundle in combination with preloading

nurtext avatar Jun 14 '22 12:06 nurtext

I got the same error with:

  • symfony 6.1
  • jwt-authentication-bundle 2.16.0
  • PHP 8.1
  • Preload activated

aerrasti avatar Jun 28 '22 14:06 aerrasti

Thank you, https://symfony.com/blog/new-in-symfony-5-1-configurable-php-preloading might help here. If one of you could try and confirm that would be great, or even submit a PR if possible :) Otherwise I'll have a look asap. Note that with PHP 8.1, preloading is not much useful (see e.g. https://symfony.com/blog/symfony-6-1-will-require-php-8-1)

chalasr avatar Jun 28 '22 15:06 chalasr

In my case this error occoured only in Docker, not if using symfony serve. What I did was to comment out theses line in php.ini: ; opcache.preload_user=www-data ; opcache.preload=/srv/api/config/preload.php

HTH Danilo

fedale avatar Jul 04 '22 09:07 fedale

Same issue here. also only with the prealoading

wouter-toppy avatar Jul 27 '22 11:07 wouter-toppy

Same issue with:

  • Symfony 6.0
  • jwt-authentication-bundle 2.16.0
  • PHP 8.0
  • Preload

bartholdbos avatar Jul 28 '22 13:07 bartholdbos

@yivi @chalasr

Created a pull to drop symfony 5.3 (and by requiring symfony 5.4, drop php 7.1)

Symfony 5.3 is EOL anyways. Symfony 4.4 will be in the coming quarter. So i dont see any reason to hold support for those versions.

PHP 7.1 is also EOL. same for any 7.x version. but i dont wanna go that far.

Main reason. the execption comes with the eval() function, which is created in commit https://github.com/lexik/LexikJWTAuthenticationBundle/commit/82a0e677caae1b1ba9edade027e5e2458d0cd8af

but as php self says it not a very safe function. and given if we drop support on symfony <5.3 we dont need it any more.

Please consider merging this in a 3.0-RC.1 tag

wouter-toppy avatar Aug 10 '22 14:08 wouter-toppy

If anyone alse has the RefreshTokenBundle:

See:

  • https://github.com/markitosgv/JWTRefreshTokenBundle/issues/337
  • https://github.com/markitosgv/JWTRefreshTokenBundle/pull/338

wouter-toppy avatar Aug 10 '22 14:08 wouter-toppy

looks fixed for me, with both branches. if somebody wants to test it with me?

I tested on Symfony 6.1 and PHP 8.1

you can require my forks

composer.json

{
    "repositories": [
        {
            "type": "vcs",
            "url": "[email protected]:wouter-toppy/LexikJWTAuthenticationBundle.git"
        },
        {
            "type": "vcs",
            "url": "[email protected]:wouter-toppy/JWTRefreshTokenBundle.git"
        }
    ],
    "require": {
        "gesdinet/jwt-refresh-token-bundle": "jwt-authentication-bundle-3-0-dev",
        "lexik/jwt-authentication-bundle": "3.x-dev",
    }
}

wouter-toppy avatar Aug 10 '22 15:08 wouter-toppy

Same problem when generating assets in prod environment only, On symfony 6.1 & PHP 8.1 using : bin/console assets:install --env=prod -vv

The fix provided by @wouter-toppy works well ! Waiting for the merge in official lexik bundle ;)

Loic-Hakisa avatar Dec 27 '22 10:12 Loic-Hakisa

Hello, when will this fixed be in a 2.x branch release ? Relying on foreign git repositories breaks CI builds on Github Workflows unless a special authorization token is generated and stored...

myselfhimself avatar Jan 26 '23 14:01 myselfhimself

No matter if we want to keep compatibility with Symfony 5.3 or not, this compatibility should be done in another way, I believe. First of all, eval is indeed a bad approach, because it can cause different trouble related to static analysis and other things (like the mentioned pre-loading). Secondly, it's also not the best approach to have branching inside a single file and have multiple Trait declarations based on other circumnstancies.

From my POV, and looking from a birds-eye view, a better approach would be to have separate versions of package if there are compatibility issues with different versions of other packages (Symfony in this case). But if it is problematic to have separate versions of the whole package, particular this compatibility may be extracted to separate versions.

What I mean that this exact trait (ForwardCompapAuthenticatorTrait) can be extracted to a separate package (like, lexik/compatibility). And while the main package still stays the same, it will require that compat-package, which, in turn, would have different versions depending on current Symfony installation. And this will be managed by composer itself.

Amegatron avatar Apr 10 '23 08:04 Amegatron