Skip to content

Latest commit

 

History

History
82 lines (68 loc) · 2.13 KB

5-encoder-service.md

File metadata and controls

82 lines (68 loc) · 2.13 KB

JWT encoder service customization

This bundle comes with two built-in token encoders, one based on the namshi/jose library (default) and the later based on the lcobucci/jwt library. If both don't suit your needs, you can replace it with your own encoder service. Here's an example implementing a nixilla/php-jwt library based encoder.

Creating your own encoder

Create the encoder class

// src/AppBundle/Encoder/NixillaJWTEncoder.php
namespace AppBundle\Encoder;

use JWT\Authentication\JWT;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;

/**
 * NixillaJWTEncoder
 *
 * @author Nicolas Cabot <[email protected]>
 */
class NixillaJWTEncoder implements JWTEncoderInterface
{
    /**
     * @var string
     */
    protected $key;

    /**
     * __construct
     */
    public function __construct($key = 'super_secret_key')
    {
        $this->key = $key;
    }

    /**
     * {@inheritdoc}
     */
    public function encode(array $data)
    {
        return JWT::encode($data, $this->key);
    }

    /**
     * {@inheritdoc}
     */
    public function decode($token)
    {
        try {
            return (array) JWT::decode($token, $this->key);
        } catch (\Exception $e) {
            return false;
        }
    }
}

Declare it as a service

# services.yml
services:
    acme_api.encoder.nixilla_jwt_encoder:
        class: AppBundle\Encoder\NixillaJWTEncoder

Use it as encoder service

# config.yml
lexik_jwt_authentication:
    # ...
    encoder:
        service: acme_api.encoder.nixilla_jwt_encoder

Note
You can use the lexik_jwt_authentication.encoder.crypto_engine and lexik_jwt_authentication.encoder.signature_algorithm parameters that represent the corresponding configuration options by injecting them as argument of the encoder's service, then use them through the library on which the encoder is based on.
See the configuration reference for more informations.