JWTEncodeFailureException: Unable to create a signed JWT from the given configuration

434 Views Asked by At

I found this error when I launch unit test in project symfony

Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException: Unable to create a signed JWT from the given configuration.

application is working but in unit test I found this error, two keys public and private exist in config/jwt folder

.env.test:

KERNEL_CLASS='App\Kernel'

.env:

APP_SECRET=hello

###> lexik/jwt-authentication-bundle ###
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=hellohello
###< lexik/jwt-authentication-bundle ###

and when i add configurations lexikjwtauthentification in .env.test ,also error

1

There are 1 best solutions below

2
Hans FooBar On
  1. Generate SSL keys for the UnitTest
  2. Store the keys in a folder
  3. Configure the keys in phpunit.xml.dist

Example from a exists project configuration:

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="bin/.phpunit/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="tests/bootstrap.php"
>
    <php>
        <!-- Other config stuff -->
        
        <!-- ###+ lexik/jwt-authentication-bundle ### -->
        <env name="JWT_SECRET_KEY" value="%kernel.project_dir%/tests/Fixtures/jwt/private.pem"/>
        <env name="JWT_PUBLIC_KEY" value="%kernel.project_dir%/tests/Fixtures/jwt/public.pem"/>
        <env name="JWT_PASSPHRASE" value="your-passphrase"/>
        <!-- ###- lexik/jwt-authentication-bundle ### -->
        
        <!-- Other config stuff -->
    </php>
    <!-- Other config stuff -->
</phpunit>
# config/packages/lexik_jwt_authentication.yaml
lexik_jwt_authentication:
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'
    # Other config stuff