Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error Cannot declare trait ForwardCompatAuthenticatorTrait #996

Open
rpilote opened this issue Mar 17, 2022 · 16 comments
Open

Fatal error Cannot declare trait ForwardCompatAuthenticatorTrait #996

rpilote opened this issue Mar 17, 2022 · 16 comments

Comments

@rpilote
Copy link

rpilote commented Mar 17, 2022

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 rpilote changed the title Fatal error Cannot declare trait Fatal error Cannot declare trait ForwardCompatAuthenticatorTrait Mar 17, 2022
@yivi
Copy link
Contributor

yivi commented Mar 18, 2022

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.

@rpilote
Copy link
Author

rpilote commented Mar 25, 2022

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.

@fd6130
Copy link

fd6130 commented Apr 14, 2022

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?

@atlance
Copy link

atlance commented May 20, 2022

@fd6130 vimeo/psalm#7863

@nurtext
Copy link

nurtext commented Jun 14, 2022

Same for me using LexikJWTAuthenticationBundle in combination with preloading

@aerrasti
Copy link
Contributor

I got the same error with:

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

@chalasr
Copy link
Collaborator

chalasr commented Jun 28, 2022

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)

@fedale
Copy link

fedale commented Jul 4, 2022

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

@wouter-toppy
Copy link
Contributor

Same issue here. also only with the prealoading

@bartholdbos
Copy link

Same issue with:

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

@wouter-toppy
Copy link
Contributor

@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 82a0e67

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
Copy link
Contributor

@wouter-toppy
Copy link
Contributor

wouter-toppy commented Aug 10, 2022

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",
    }
}

chalasr added a commit that referenced this issue Aug 18, 2022
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Drop PHP <7.1 and Symfony <5.3 support

Fix for #996

For tag 3.x

Commits
-------

ac9c8a6 Drop PHP <7.1 and Symfony <5.3 support
@Loic-Hakisa
Copy link

Loic-Hakisa commented Dec 27, 2022

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 ;)

@myselfhimself
Copy link

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...

@Amegatron
Copy link

Amegatron commented Apr 10, 2023

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.

chalasr added a commit that referenced this issue Apr 11, 2023
…lavrom)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

Fix ForwardCompatAuthenticatorTrait with OPCache preload

Hi there,

Since the `eval()` in the `ForwardCompatAuthenticatorTrait` is creating problems when OPCache preload is enabled, here is a quick fix to avoid eval()'ing multiple times.

(Issue ref : #996)

P.S. : First time making a PR, feel free to tell me if I did something wrong

Commits
-------

325534b Fix ForwardCompatAuthenticatorTrait with OPCache preload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests