1

Good afternoon,

I try to use LexikJWTAuthenticationBundle in my project and I have a problem with the token which is not generated. I have set the private & public keys in var/jwt directory.

The API returns this response when I try use the login route :

{
    "code": 401,
    "message": "JWT Token not found"
}

Apache Virtualhost :

<VirtualHost *:80>
    ServerName ypostirixi
    DocumentRoot "/var/www/ypostirixi/public"

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</VirtualHost>

.htaccess file in public directory:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

security.yaml security:

encoders:
    App\Entity\User:
        algorithm: bcrypt
providers:
    doctrine_provider:
        entity:
            class: App\Entity\User
            property: email

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    api_doc:
        pattern:  ^/api/doc
        security: false
    api:
        pattern:   ^/api
        stateless: true
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
    main:
        pattern:   ^/
        stateless: true
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
        provider: doctrine_provider

access_control:
    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

I expect to successfully use the login route and generate a valid token on the others routes.

3 Answers 3

1

You are not allowing anonymous access to any firewalls. You should add anonymous option to your main firewall.

    main:
        pattern:   ^/
        stateless: true
        anonymous: true
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
        provider: doctrine_provider
Sign up to request clarification or add additional context in comments.

Comments

1
  • Maybe you forgot to configure firewall of login in section firewalls or login parameters (email as username) ..

check with this

1 config/packages/security.yaml

    firewalls:
        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            form_login:
                check_path:               /api/login_check
                username_parameter: email
                password_parameter: password
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

        api:
            pattern:   ^/api
            stateless: true
            guard:
               authenticators:
                   - lexik_jwt_authentication.jwt_token_authenticator
        access_control:
            - { path: ^/api/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/api/, role: IS_AUTHENTICATED_FULLY }

2 config/routes.yaml

api_login_check:
    path: /api/login_check

3 Test it with curl

X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"email":"[email protected]","password":"pass"}'

NB: if not working for you , maybe you skipped a step on the configuration or you did not configure the bundle properly , You must see on the documentation https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#installation

Comments

0

Thank you for your help.

I have discover a problem about this upgrade, but I have a solution.

In lexik_jwt_authentication.yaml file :

lexik_jwt_authentication:
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'
    token_ttl: '%env(JWT_TTL)%'
    token_extractors:
        authorization_header:
            enabled: true
            prefix:  '%env(JWT_TOKEN_PREFIX)%'
            name:    Authorization
    user_identity_field: email

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.