avatarVladimir Kovalchuk

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1336

Abstract

putting that into config file i still had 401 for some requests untill i switched off the default extractor. So the final look of the token_extractors block should be as follows:</p><div id="3f96"><pre><span class="hljs-attr">lexik_jwt_authentication:</span> <span class="hljs-comment"># token extraction settings</span> <span class="hljs-attr">token_extractors:</span> <span class="hljs-comment"># check token in a cookie</span> <span class="hljs-attr">authorization_header:</span> <span class="hljs-attr">enabled:</span> <span class="hljs-literal">false</span> <span class="hljs-attr">prefix:</span> <span class="hljs-string">Bearer</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Authorization</span> <span class="hljs-attr">cookie:</span> <span class="hljs-attr">enabled:</span> <span class="hljs-literal">true</span> <span class="hljs-attr">name:</span> <span class="hljs-string">BEARER</span></pre></div><p id="75b7">TADA! That’s it.</p><p id="2064">Resuming. After completing the two parts of cookie-based authentication flow — lexik configuration should be:</p><div id="2b00"><pre><span class="hljs-attr">lexik_jwt_authentication:</span> <span class="hljs-comment"># token extraction settings</span>

Options

<span class="hljs-attr">token_extractors:</span> <span class="hljs-attr">authorization_header:</span> <span class="hljs-attr">enabled:</span> <span class="hljs-literal">false</span> <span class="hljs-attr">prefix:</span> <span class="hljs-string">Bearer</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Authorization</span> <span class="hljs-comment"># check token in a cookie</span> <span class="hljs-attr">cookie:</span> <span class="hljs-attr">enabled:</span> <span class="hljs-literal">true</span> <span class="hljs-attr">name:</span> <span class="hljs-string">BEARER</span> <span class="hljs-attr">set_cookies:</span> <span class="hljs-attr">BEARER:</span> <span class="hljs-attr">httpOnly:</span> <span class="hljs-literal">true</span> <span class="hljs-attr">samesite:</span> <span class="hljs-string">lax</span></pre></div><div id="0073"><pre> secret_key: '%env(resolve:JWT_SECRET_KEY)%' public_key: '%env(resolve:JWT_PUBLIC_KEY)%' pass_phrase: '%env(JWT_PASSPHRASE)%'</pre></div><p id="d483">Read <a href="https://readmedium.com/part-3-refreshing-the-token-45be20deda96"><b>Part 3</b></a> to know how can you refresh the token! <i>See ya there!</i></p></article></body>

Part 2. Read token from cookies and authenticate the user

In the Part 1 — i have described how to implement setting of auth token into the httpOnly cookies. This part of the story will be dedicated to How can we read the token back and authenticate the user.

Let’s go.

The developers of LexikJWTAuthenticationBundle claims on their docs pages that to make it true we need a slightly edit the config file (the one located here: /config/packages/lexik_jwt_authentication.yaml) as follows:

lexik_jwt_authentication:
    # token extraction settings
    token_extractors:
        # check token in a cookie
        cookie:
            enabled: true
            name:    BEARER

After putting that into config file i still had 401 for some requests untill i switched off the default extractor. So the final look of the token_extractors block should be as follows:

lexik_jwt_authentication:
    # token extraction settings
    token_extractors:
        # check token in a cookie
        authorization_header:
            enabled: false
            prefix:  Bearer
            name:    Authorization
        cookie:
            enabled: true
            name:    BEARER

TADA! That’s it.

Resuming. After completing the two parts of cookie-based authentication flow — lexik configuration should be:

lexik_jwt_authentication:
    # token extraction settings
    token_extractors:
        authorization_header:
            enabled: false
            prefix:  Bearer
            name:    Authorization
        # check token in a cookie
        cookie:
            enabled: true
            name:    BEARER
    set_cookies:
        BEARER:
            httpOnly: true
            samesite: lax
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'

Read Part 3 to know how can you refresh the token! See ya there!

Cookies
Security
Httponly
Symfony
Authentication
Recommended from ReadMedium