Configuration

In order to simplify the setup of the library, we provide the class Lcobucci\JWT\Configuration.

It's meant for:

  • Configuring the default algorithm (signer) and key(s) to be used
  • Configuring the default set of validation constraints
  • Providing custom implementation for the extension points
  • Retrieving components (encoder, decoder, parser, validator, and builder)

Usage

In order to use it, you must:

  1. Initialise the configuration object
  2. Customise the configuration object
  3. Retrieve components

Configuration initialisation

The Lcobucci\JWT\Signer\Key object is used for symmetric/asymmetric signature.

To initialise it, you can pass the key content as a string:

use Lcobucci\JWT\Signer\Key;

$key = new Key('my-key-as-plaintext');

Or provide a file path:

use Lcobucci\JWT\Signer\Key;

$key = new Key('file://' . __DIR__ . '/path-to-my-key-stored-in-a-file.pem');

For symmetric algorithms

Symmetric algorithms use the same key for both signature creation and verification. This means that it's really important that your key remains secret.

Tip

It is recommended that you use a key with lots of entropy, preferably generated using a cryptographically secure pseudo-random number generator (CSPRNG). You can use the CryptoKey tool to do this for you.

use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key;

$configuration = Configuration::forSymmetricSigner(
    // You may use any HMAC variations (256, 384, and 512)
    new Sha256(),
    // replace the value below with a key of your own!
    new Key('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw=')
);

For asymmetric algorithms

Asymmetric algorithms use a private key for signature creation and a public key for verification. This means that it's fine to distribute your public key. However, the private key should remain secret.

use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key;

$configuration = Configuration::forAsymmetricSigner(
    // You may use RSA or ECDSA and all their variations (256, 384, and 512)
    new Signer\RSA\Sha256(),
    new Key('file://' . __DIR__ . '/my-private-key.pem'),
    new Key('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw=')
);

For no algorithm

Warning

This configuration type is NOT recommended for production environments. It's only provided to allow people to have a simpler and faster setup for tests, avoiding any kind of signature creation/verification.

use Lcobucci\JWT\Configuration;

$configuration = Configuration::forUnsecuredSigner();

Customisation

By using the setters of the Lcobucci\JWT\Configuration you may customise the setup of this library.

Important

If you want to use a customised configuration, please make sure you call the setters before of invoking any getter. Otherwise, the default implementations will be used.

These are the available setters:

  • Lcobucci\JWT\Configuration#setBuilderFactory(): configures how the token builder should be created
  • Lcobucci\JWT\Configuration#setParser(): configures a custom token parser
  • Lcobucci\JWT\Configuration#setEncoder(): configures a custom encoder for the token builder
  • Lcobucci\JWT\Configuration#setDecoder(): configures a custom decoder for the parser
  • Lcobucci\JWT\Configuration#setValidator(): configures a custom validator
  • Lcobucci\JWT\Configuration#setValidationConstraints(): configures the default set of validation constraints

Retrieve components

Once you've made all the necessary configuration you can pass the configuration object around your code and use the getters to retrieve the components:

These are the available getters:

  • Lcobucci\JWT\Configuration#createBuilder(): retrieves the token builder (always creating a new instance)
  • Lcobucci\JWT\Configuration#getParser(): retrieves the token parser
  • Lcobucci\JWT\Configuration#getSigner(): retrieves the signer
  • Lcobucci\JWT\Configuration#getSigningKey(): retrieves the key for signature creation
  • Lcobucci\JWT\Configuration#getVerificationKey(): retrieves the key for signature verification
  • Lcobucci\JWT\Configuration#getValidator(): retrieves the token validator
  • Lcobucci\JWT\Configuration#getValidationConstraints(): retrieves the default set of validation constraints