Requests for new features, bug fixed and all other ideas to make this framework useful are welcome. If you feel comfortable writing code, you could try to fix or .
Do not forget to .
If you think you have found a security issue, DO NOT open an issue. .
To perform cryptographic operations (signature/verification and encryption/decryption), you will need cryptographic keys.
JWK
A JWK object represents a key. It contains all parameters needed by the algorithm and may also provide information parameters.
This framework is able to create private and public keys easily. It can also generate those keys from external resources such as binary key files or certificates.
JWKSet
The keys can be grouped in key sets. A JWKSet object represents a key set. It can contain as many keys as you need.
We strongly recommend you to avoid mixing public, private or shared keys in the same key set.
Symfony Console
If the symfony/console component is available, you will see all commands executing the following line:
./bin/console
Key and Key Set Management
We recommend you to load these objects through environment variables. With Symfony, an environment variables processor is provided:
With the previous configuration, the environment variables MY_PRIVATE_KEY and MY_PUBLIC_KEYSET will be processed by Symfony and the container will contain the my_private_key and my_public_keyset with JWK and JWKSet objects respectively.
But it may not be sufficient for your project. You may need to load keys or key sets from other sources (e.g. key file) You may also want to use your keys as a container services you inject to other services.
Signature Algorithms
This framework comes with several signature algorithms. These algorithms are in the following namespace: Jose\Component\Signature\Algorithm.
Algorithm
Description
Signed Tokens
Signature algorithms are automatically handled by the Algorithm Manager Factory.
,
,
Symfony Bundle
This framework provides a Symfony bundle that will help you to use the components within your Symfony application.
With Symfony Flex
The bundle is automatically detected when Flex is available.
The variable $jws will be a valid JWS object with all computed signatures. Next step is the serialization of these signatures.
use Jose\Component\Signature\Serializer;
$manager = Serializer\JWSSerializerManager::create([
new Serializer\CompactSerializer(),
new Serializer\JsonFlattenedSerializer(),
new Serializer\JsonGeneralSerializer(),
]);
$tokenWithAllSignatures = $manager->serialize('jws_json_general', $jws);
$compactTokenWithSignatureAtIndex1 = $manager->serialize('jws_compact', $jws, 1);
$flattenedTokenWithSignatureAtIndex2 = $manager->serialize('jws_json_flattened', $jws, 2);
RSASSA-PSS
EdDSA (only with the Ed25519 curve)
Edwards-curve Digital Signature Algorithm (EdDSA)
none
Experimental Algorithms
The following signature algorithms are experimental and must not be used in production unless you know what you are doing. They are proposed for testing purpose only.
They are provided throught the package web-token/jwt-experimental.
Algorithm
Description
RS1
RSASSA-PKCS1 v1_5 with SHA-1 hashing function
HS1
HMAC with SHA-1 hashing function
ES256K
Elliptic curve secp256k1 support
How To Use
These algorithms have to be used with the Algorithm Manager. They do not need any arguments.
Example:
HS256
HS384
HS512
HMAC with SHA-2 Functions
ES256
ES384
ES512
Elliptic Curve Digital Signature Algorithm (ECDSA)
// $serializer corresponds to the Symfony serializer
$serializer->serialize($data, 'jws_compact');
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Signature\Algorithm\PS256;
use Jose\Component\Signature\Algorithm\ES512;
use Jose\Component\Signature\Algorithm\None;
$algorithm_manager = new AlgorithmManager([
new PS256(),
new ES512(),
new None(),
]);
As the recipes are third party ones not officially supported by Symfony, you will be asked to execute the recipe or not. When applied, the recipes will prompt a message such as Web Token Framework is ready.
Without Symfony Flex
If you don't use Symfony Flex, you must register the bundle manually.
Bundle Features
The bundle capabilities will depend on the components installed in your application.
Signed or Encrypted Tokens are not just the next trendy/popular way to authenticate users. They provide great features but when used incorrectly they can expose your application to major security issues.
Please read the following recommendations carefully.
Algorithms
Algorithm Choice
If all parties are able to protect their keys (e.g. private applications), symmetric algorithms are a good choice as they are faster in general. If you use public clients, you should prefer asymmetric algorithms.
Available Algorithms
This framework provides dozen of signature and encryption algorithms, but you do not need all of them. Most applications only support 1 or 2 algorithms.
You should only use necessary algorithms. For example, HS384 algorithm may be avoided if you already have HS256 and HS512.
Avoid Weak Algorithms
Some algorithms are not recommended as there are known security issues:
none: this algorithm is not a real algorithm. It should only be used when other security means exist. An encrypted connection is certainly not enough!
RSA1_5: there are known attacks using this algorithm. If you can avoid its use, then do it.
Keys And Key Sets
Key Size
A small key size is as secured as a password like 123456789. You should use at least 256 bits symmetric keys and at lease 2048 bits RSA keys.
In any case, you MUST use a true random number generator.
Additional Information
It is highly recommended to set the following parameters to your key:
kid: A unique key ID,
use: indicates the usage of the key. Either sig (signature/verification) or enc (encryption/decryption).
Key Rotation
A key is fine but may be cracked e.g. by bruteforce. Changing your keys after several days or weeks is encouraged.
Token Creation
Header Parameters
Broadly speaking, you should set your header parameter in the protected header. The use of the unprotected header should be limited to specific use cases.
When using encrypted tokens, the claims iss and aud should be duplicated into the header. This will avoid unwanted decryption when tokens are sent to a wrong audience.
Payload And Claims
There is no size constraint for the payload, but when tokens are used in a web context, it should be as small as possible. When used, claims should be limited to the minimum.
Does your application really need to get all the information about a user? For each context, you should choose carefully the claims you want to use.
Token Unique ID
A unique token ID should be set to all tokens you create. The associated claim is jti.
This claim is highly recommended as it can prevent replay attacks.
Time Indicators
The JWT specification introduces several claims to limit the period of validity of the tokens:
exp: expiration time,
iat: issuance time,
nbf
These claims are not mandatory, but it is recommended to define a period of time for the token validity. When used, the expiration time should be appropriate to its context in your application. A security token with 2 weeks lifetime is something you should avoid.
Issuer And Audience
The claims iss (issuer) and aud (audience) should always be set. When duplicated in the header, their values MUST be identical.
Application Communication
Secured Connection
Unless you use encrypted tokens, you should use a secured connection when transmitting tokens between parties. A secured communication is not only needed when transmitting tokens, but also when you exchange keys and key sets with other applications.
Loading Process
When you receive a token, the following steps should be followed in this order. If one failed, you must reject the whole token.
Unserialize the token
For each signature/recipient (may be possible when using the Json General Serialization Mode):
Check the complete header (protected and unprotected)
Regarding header verification, it is important to verify ALL of them. There are known vulnerabilities on e.g. algorithm swapping (alg changed to none) or Denial Of Service (p2c set to a very high value for overloading the server).
Unserialize The Token
You should only use the serialization mode(s) you need. If you intend to use your tokens in a web context, then use only the Compact Serialization. If an error occurred during this process, you should consider the token as invalid.
Check The Header
It is strongly recommended that all header parameters are checked before the cryptographic operations (signature verification or content decryption)
Please note that unknown header parameters must be ignored. If your token is verified, those parameters should not be used.
When used, unprotected header parameters should be handled with care.
Signature Verification / Payload Decryption
Let the component do its job. The most important step for developers is to ensure that the right key/ket set is used.
Check The Claims
This step is only required if the payload contains claims. When present, you should always check the exp, iat, nbf, iss and aud claims. Application specific claims should also always checked.
The whole token should be rejected in case of failure. Unknown claims should be ignored.
Stay Tuned
You should subscribe to security forums of similar websites and have a continuous technological watch. The tokens may be compromised because of malicious attacks on the algorithms, keys or other components related to the JWT (directly or indirectly).
The is a good start.
Provided Features
Supported Input Types:
JWS or JWE objects support every input that can be encoded into JSON:
string, array, integer, float...
Objects that implement the \JsonSerializable interface such as JWK or JWKSet
The is supported.
Supported Serialization Modes
Serialization syntax
JWS
JWE
Supported Compression Methods
Compression mode
Supported
Compression is not recommended. Please avoid its use. See for more information.
Supported Key Types (JWK)
Key Type
Supported
Comment
JWK objects support JSON Web Key Thumbprint ().
A none key type for the none algorithm. It is used to explicitly allow this unsecured algorithm.
Key Sets (JWKSet)
JWKSet is fully supported.
Supported Signature Algorithms
Signature Algorithm
Supported
Comment
Other signature algorithms like RS1, HS1 or HS256/64 are also available. These algorithms should be used for testing purpose only or for compatibility with old systems
Supported Key Encryption Algorithms
Key Encryption Algorithm
Supported
Other encryption algorithms like RSA-OEAP-384 or ChaCha20-Poly1305 are also available. These algorithms should be used for testing purpose only or for compatibility with old systems
The algorithms RSA1_5 and RSA-OAEP are now deprecated. Please use with caution.
Supported Content Encryption Algorithms
Content Encryption Algorithm
Supported
Other encryption algorithms like A128CTR, A192CTR and A256CTR are also available. These algorithms should be used for testing purpose only or for compatibility with old systems
Key Set (JWKSet)
You can create a JWKSet object using three static methods:
new JWKSet(array $keys): creates a JWKSet using a list of JWK objects.
JWKSet::createFromJson(string $json): creates a JWKSet using a JSON object.
JWKSet::createFromKeyData(array $values): creates a JWKSet using a decoded JSON object.
Hereafter all methods available for a JWKSet object. The variable $jwkset is a valid JWKSet object.
Please note a JWKSet object is an immutable object. When you add keys, you get a new JWKSet object
Header and Claim Checker Management
Checker Manager Factory Services
The Symfony Bundle provides Header and Claim Checker Manager Factory services.
Checker Manager Services
You can create Header and Claim Checker Managers using the bundle configuration.
With the previous configuration, the bundle will create public Header and Claim Checker Managers named jose.header_checker.checker1 and jose.claim_checker.checker1 with selected checkers.
Custom Header Or Claim Checker
Some claim or header checkers are provided by this framework, but it is important to create custom checkers that fit on your application requirements.
In the following example, we will assume that the class exist and implement either Jose\Component\Checker\HeaderChecker or Jose\Component\Checker\ClaimChecker.
These checkers will be loaded by the factories and you will be able to create a header or a claim checker manager using the aliases foo or bar.
Custom Tags
You can add custom tags and attributes to the header and claim checker managers.
Algorithm Management
Algorithm Manager Factory Service
The Symfony Bundle provides an Algorithm Manager Factory service. The available algorithms depends on the components installed on your application.
<?php
use Jose\Component\Core\AlgorithmManagerFactory;
$algorithmManagerFactory = $container->get(AlgorithmManagerFactory::class);
$algorithmManager = $algorithmManagerFactory->create(['RS256', 'HS512']);
Custom Algorithm
This factory handles all algorithms services tagged with jose.algorithm.
Example:
Your algorithm will be available through the algorithm manager factory service and the alias FOO.
PBES2-* Algorithms
When installed, the PBES2-* algorithms available throught the algorithm manager factory. They have the default configuration i.e. salt size = 62 bits and count = 4096. If these values does not fit on your needs, you can create a new algorithm service with your own values:
You can now use your custom alias:
JWS Creation
Now that you have an algorithm manager and a key, it is time to create your first signed token.
The computation is done by the JWSBuilder object. This object only requires the algorithm manager.
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\JWSBuilder;
// The algorithm manager with the HS256 algorithm.
$algorithmManager = new AlgorithmManager([
new HS256(),
]);
// Our key.
$jwk = new JWK([
'kty' => 'oct',
'k' => 'dzI6nbW4OcNF-AtfxGAmuyz7IpHRudBI0WgGjZWgaRJt6prBn3DARXgUR8NVwKhfL43QBIU2Un3AvCGCHRgY4TbEqhOi8-i98xxmCggNjde4oaW6wkJ2NgM3Ss9SOX9zS3lcVzdCMdum-RwVJ301kbin4UtGztuzJBeg5oVN00MGxjC2xWwyI0tgXVs-zJs5WlafCuGfX1HrVkIf5bvpE0MQCSjdJpSeVao6-RSTYDajZf7T88a2eVjeW31mMAg-jzAWfUrii61T_bYPJFOXW8kkRWoa1InLRdG6bKB9wQs9-VdXZP60Q4Yuj_WZ-lO7qV9AEFrUkkjpaDgZT86w2g',
]);
// We instantiate our JWS Builder.
$jwsBuilder = new JWSBuilder($algorithmManager);
Now let's create our first JWS object.
// The payload we want to sign. The payload MUST be a string hence we use our JSON Converter.
$payload = json_encode([
'iat' => time(),
'nbf' => time(),
'exp' => time() + 3600,
'iss' => 'My service',
'aud' => 'Your application',
]);
$jws = $jwsBuilder
->create() // We want to create a new JWS
->withPayload($payload) // We set the payload
->addSignature($jwk, ['alg' => 'HS256']) // We add a signature with a simple protected header
->build(); // We build it
Great! If everything is fine you will get a JWS object with one signature. We want to send it to the audience. Before that, it must be serialized.
We will use the compact serialization mode. This is the most common mode as it is URL safe and very compact. Perfect for a use in a web context!
All good! The variable $token now contains a string that should be something like this:
Other serialization modes exist. We will see them in the Advanced Topics section.
JWE creation
JWE Builder Factory Service
A JWEBuilderFactory is available as a service in your application container:
<?php
use Jose\Component\Encryption\JWEBuilderFactory;
$jweBuilderFactory = $container->get(JWEBuilderFactory::class);
With this factory, you will be able to create the JWEBuilder you need:
Available compression methods are:
DEF: deflate (recommended)
You can now use the JWEBuilder as explained in the JWE Creation section.
JWE Builder As Service
There is also another way to create a JWEBuilder object: using the bundle configuration.
With the previous configuration, the bundle will create a public JWE Builder service named jose.jwe_builder.builder1 with selected encryption algorithms.
Custom Tags
You can add custom tags and attributes to the services you create.
From v1.x to v2.0
Contrary to upgrade a minor version (where the middle number changes) where no difficulty should be encountered, upgrade a major version (where the first number changes) is subject to significant modifications.
Update the libraries
First of all, you have to make sure you are using the last v1.x release (1.3.8).
Spot deprecations
Next, you have to verify you don’t use any deprecated class, interface, method or property. If you have PHPUnit tests, .
List of deprecations:
Jose\Component\Core\JWK::create(): this static function is removed. Use the constructor instead
Jose\Component\Core\JWKSet::createFromKeys() : this static function is removed. Use the constructor instead
Jose\Component\Core\Converter\JsonConverter: this interface is removed. No replacement.
With the Symfony bundle, the configuration option jose.json_converter is removed.
Add missing dependencies
In v1.x, when you install the web-token/jwt-signature or web-token/jwt-encryption, the algorithms are automatically install.
In v2.0, you must explicitly install the algorithms you need. Please refer to the or to know what package you need to install.
Upgrade the libraries
It is now time to upgrade the libraries. In your composer.json, change all web-token/* dependencies from v1.x to v2.0. When done, execute composer update.
You can also update all other dependencies if needed. You can list upgradable libraries by calling composer outdated. This step is not mandatory, but highly recommended.
JWE serializers
JWE Serializer Manager Factory Service
A JWESerializerManagerFactory is available as a service in your application container:
With this factory, you will be able to create the JWESerializerManager you need:
You can now use the JWESerializerManager as explained in the JWE Creation/Loading section.
From v2.x to v3.0
Contrary to upgrade a minor version (where the middle number changes) where no difficulty should be encountered, upgrade a major version (where the first number changes) is subject to significant modifications.
Update the libraries
First of all, you have to make sure you are using the last v2.x release.
<?php
use Jose\Component\Checker\HeaderCheckerManagerFactory;
use Jose\Component\Checker\ClaimCheckerManagerFactory;
$headerCheckerManagerFactory = $container->get(HeaderCheckerManagerFactory::class);
$headerCheckerManager = $headerCheckerManagerFactory->create([...]);
$claimCheckerManagerFactory = $container->get(ClaimCheckerManagerFactory::class);
$claimCheckerManager = $claimCheckerManagerFactory->create([...]);
use Jose\Component\Signature\Serializer\CompactSerializer;
$serializer = new CompactSerializer(); // The serializer
$token = $serializer->serialize($jws, 0); // We serialize the signature at index 0 (we only have one signature).
spomky-labs/aes-key-wrap is required for *KW algorithms
<?php
// Returns all keys
$jwkset->all();
// Check if the key set has the key with the key ID 'KEY ID'.
$jwkset->has('KEY ID');
// Retreive the key with the key ID 'KEY ID'.
$jwkset->get('KEY ID');
// Counts the keys in the key set.
$jwkset->count(); // The method count($jwkset) has the same behaviour.
// Adds a key to the key set.
// /!\ As the JWKSet object is immutable, this method will create a new key set. The previous key set is unchanged.
$newJwkset = $jwkset->with($jwk);
// Removes a key to the key set.
// /!\ As the JWKSet object is immutable, this method will create a new key set. The previous key set is unchanged.
$newJwkset = $jwkset->without('KEY ID');
// Selects a key according to the requirements.
// The first argument is the key usage ("sig" of "enc")
// The second argument is the algorithm to be used (optional)
// The third argument is an associative array this constraints (optional)
$key = $jwkset->selectKey('sig', $algorithm, ['kid' => 'KEY ID']);
// You can iterate on a key set
foreach($jwkset as $kid => $jwk) {
// Action with the key done here
}
// The JWKSet object can be serialized into JSON
json_encode($jwkset);
There is also another way to create a JWESerializerManager object: using the bundle configuration.
With the previous configuration, the bundle will create a public JWE Serializer Manager service named jose.jwe_serializer.serializer1 with selected serialization modes.
Custom Tags
You can add custom tags and attributes to the services you create.
Jose\Easy\*: the `Easy` component was deprecated in v2.2 and removed in v3.0
Except if you use the Easy component, you should have no trouble upgrading the libraries as this major branch is mainly an upgrade from old PHP version to PHP8.1.
Symfony Flex
If you use the Symfony bundle and Symfony Flex, please note that Flex Recipes are now provided through a dedicated server.
It is highly recommended to declare this server within your application composer.json file.
You can adapt this command line depending on the other Flex servers you are using.
Upgrade the dependencies and the libraries
The version 3.0 now requires PHP 8.1. Please install this version or a newer one on your platform. Make sure the extensions are also installed. They are namely JSON and MBString. You may also need OpenSSL or Sodium depending on the algorithms you want to use.
If you require Symfony components, you shall have the version 5.4 or 6.x. for each of these components.
It is now time to upgrade the libraries. In your composer.json, change all web-token/* dependencies from v2.x to v3.0. When done, execute composer update.
You can also update all other dependencies if needed. You can list upgradable libraries by calling composer outdated. This step is not mandatory, but highly recommended.
Rector is a very nice tool from code upgrade. We highly recommend it.
JSON Web Tokens can be used to transport any kind of data. They are mainly used to transport claims. When you receive a tokens that contains claims, it is important to check the values of these claims.
Claim Checker Manager
In the following example, we will create a manager able to check the aud (Audience), iat (Issued At), nbf (Not Before) and exp (Expiration) claims.
When instantiated, call the method check to check the claims of a JWT object. This method only accept an associative array. You have to retrieve this array by converting the JWT payload.
In some cases, it could be interesting to reject tokens that do not contain some mandatory claims. A list of mandatory claims can be set as second argument. If one of those claims is missing an exception is thrown, even if the claim have not been checked.
In the following example, an exception will be thrown if the iss, sub or aud claim is missing.
Custom Claim Checker
Your application may use other claims that you will have to check therefore custom claim checkers have to be created.
In this example, we will create a class that will check the claim foo. The claim accept only a string with the value bar or bat. All claim checker have to implement the interface Jose\Component\Checker\ClaimChecker;
All done! Now you can instantiate your class and add it to your Claim Checker Manager.
Replicating Claims as Header Parameters
The allows to replicate some claims in the header. This behaviour is very useful with encrypted tokens as it helps to reject invalid tokens without decryption of the payload.
The Claim Checker Manager cannot check those replicated claims, you have to create a . However, to avoid duplicated classes, your claim checker can implement the Jose\Component\Checker\HeaderChecker interface.
Have a look at the IssuedAtChecker or the NotBeforeChecker classes. These checkers can be used for claim and header checks.
Claim Checker Manager Factory
Your application may use JSON Web Tokens in different contexts and thus the meaning of a claim may be different. You will need several Claim Checker Managers with dedicated claim checkers.
This framework provides an Claim Checker Manager Factory. This factory is able to accept as many claim checkers as you need. Each claim checker you add to this factory is associated to an alias. You will then be able to create a claim checker manager using those aliases.
JWS Loading
Signed tokens are loaded by a serializer or the serializer manager and verified by the JWSVerifier object. This JWSVerifier object just requires an algorithm manager.
Serializer And Verifier
In the following example, we will try to load a signed token. We will only use the HS256 algorithm.
Now we can deserialize the input we receive and check the signature using our key. We will continue with the data we got in the JWS creation section.
We do not check header parameters here, but it is very important to do it. This step is described in the .
The method verifyWithKey returns a boolean. If true, then your token signature is valid. You can then check the claims (if any) using the claim checker manager.
JWSLoader Object
To avoid duplication of code lines, you can create a JWSLoader object. This object contains a serializer, a verifier and an optional header checker (highly recommended).
In the following example, the JWSLoader object will try to unserialize the token $token, check the header parameters and verify the signature with the key $jwk. The variable $payload corresponds to the detached payload (null by default).
If the verification succeeded, the variable $signature will be set with the signature index and should be in case of multiple signatures. The method returns the JWS object.
In case you use a key set, you can use the method loadAndVerifyWithKeySet.
JWSLoaderFactory Object
This feature was introduced in version 1.1.
The JWSLoaderFactory object is able to create JWSLoader objects on demand. It requires the following factories:
JWSSerializerManagerFactory
JWSVerifierFactory
HeaderCheckerManagerFactory
JWE decryption
JWE Decrypter Factory Service
A JWEDecrypterFactory is available as a service in your application container:
With this factory, you will be able to create the JWEDecrypter you need:
You can now use the JWEDecrypter as explained in the JWE Creation section.
Reminder: it is important to check the token headers. See the checker section of this documentation.
JWE Decrypter As Service
There is also another way to create a JWEDecrypter object: using the bundle configuration.
With the previous configuration, the bundle will create a public JWE Decrypter service named jose.jwe_decrypter.decrypter1 with selected encryption algorithms.
Custom Tags
You can add custom tags and attributes to the services you create.
JWE Loader Service
The is available as a public service. You can retrieve it using the container or inject it into your services. It will help you to create JWELoader objects on demand.
You can also create JWELoader objects as services using the configuration of the bundle.
Or using the .
Unencoded Payload
The RFC7797 allows the use of an unencoded payload for the signed tokens. This behaviour is interesting when your tokens have a detached payload and may reduce the token computation.
Please note that when the Compact Serialization mode is used, the characters of the payload must be limited to the following ASCII ranges:
From 0x20 to 0x2d
From 0x2f to 0x7e
This feature is built in the framework and is enabled when the b64 header parameter is set to false. As per the RFC, this header MUST be protected and also listed as a critical (crit) header parameter.
Example:
As a remainder, both b64 and crit parameters MUST be in the protected header.
Encryption Algorithms
This framework comes with several encryption algorithms. These algorithms are in the following namespaces:
Encrypted tokens are loaded by a serializer or the serializer manager and decrypted by the JWEDecrypter object. This JWEDecrypter object requires several services for the process:
an algorithm manager with key encryption algorithms
an algorithm manager with content encryption algorithms
Pre-requisite
This framework needs at least:
PHP 8.1+,
Extensions:
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\JWSVerifier;
// The algorithm manager with the HS256 algorithm.
$algorithmManager = new AlgorithmManager([
new HS256(),
]);
// We instantiate our JWS Verifier.
$jwsVerifier = new JWSVerifier(
$algorithmManager
);
<?php
use Jose\Component\Encryption\JWEDecrypterFactory;
$jweDecrypterFactory = $container->get(JWEDecrypterFactory::class);
a compression method manager. No compression method is needed if you do not intent to compress the payload.
In the following example, we will use the same assumptions as the ones used during the JWE Creation process.
Compression is not recommended. Please avoid its use. See RFC8725 for more information.
Now we can try to deserialize and decrypt the input we receive. We will continue with the result we got during the JWE creation section.
We do not check header parameters here, but it is very important to do it. This step is described in the Header Checker section.
Note: we do not check header parameters here, but it is very important to do it. This step is described in the Header Checker section.
OK so if not exception is thrown, then your token is loaded and the payload correctly decrypted.
JWELoader Object
To avoid duplication of code lines, you can create a JWELoader object. This object contains a serializer, a decrypter and an optional header checker (highly recommended).
In the following example, the JWELoader object will try to unserialize the token $token, check the header parameters and decrypt with the key $key.
If the decryption succeeded, the variable $recipient will be set with the recipient index and should be in case of multiple recipients. The method returns the JWE object.
In case you use a key set, you can use the method loadAndDecryptWithKeySet.
JWELoaderFactory Object
This feature was introduced in version 1.1.
The JWELoaderFactory object is able to create JWELoader objects on demand. It requires the following factories:
JWESerializerManagerFactory
JWEDecrypterFactory
HeaderCheckerManagerFactory (optional)
<?php
use Jose\Component\Checker\ClaimCheckerManager;
use Jose\Component\Checker;
$claimCheckerManager = new ClaimCheckerManager(
[
new Checker\IssuedAtChecker(),
new Checker\NotBeforeChecker(),
new Checker\ExpirationTimeChecker(),
new Checker\AudienceChecker('Audience'),
]
);
<?php
declare(strict_types=1);
namespace Acme\Checker;
use Jose\Component\Checker\ClaimChecker;
use Jose\Component\Checker\InvalidClaimException;
/**
* Class FooChecker.
*/
final class FooChecker implements ClaimChecker
{
/**
* {@inheritdoc}
*/
public function checkClaim($value)
{
if (!is_string($value)) { // If the value is not a string, then we throw an exception
throw new InvalidClaimException('The claim "foo" must be a string.', 'foo', $value);
}
if (!in_array($value, ['bar', 'bat'])) { // Check if the value is allowed
throw new InvalidClaimException('The claim "foo" must be "bar" or "bat".', 'foo', $value);
}
}
/**
* {@inheritdoc}
*/
public function supportedClaim(): string
{
return 'foo'; //The claim to check.
}
}
<?php
use Jose\Component\Checker\ClaimCheckerManagerFactory;
use Jose\Component\Checker;
$claimCheckerManagerFactory = new ClaimCheckerManagerFactory();
$claimCheckerManagerFactory->add('iat', new Checker\IssuedAtChecker());
$claimCheckerManagerFactory->add('nbf', new Checker\NotBeforeChecker());
$claimCheckerManagerFactory->add('exp', new Checker\ExpirationTimeChecker());
$claimCheckerManagerFactory->add('aud1', new Checker\AudienceChecker('Audience for service #1'));
$claimCheckerManagerFactory->add('aud2', new Checker\AudienceChecker('Audience for service #2'));
$claimCheckerManager1 = $claimCheckerManagerFactory->create(['iat', 'exp', 'aud2']);
$claimCheckerManager2 = $claimCheckerManagerFactory->create(['aud1', 'exp']);
<?php
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Serializer\JWSSerializerManager;
use Jose\Component\Signature\Serializer\CompactSerializer;
// Our key.
$jwk = new JWK([
'kty' => 'oct',
'k' => 'dzI6nbW4OcNF-AtfxGAmuyz7IpHRudBI0WgGjZWgaRJt6prBn3DARXgUR8NVwKhfL43QBIU2Un3AvCGCHRgY4TbEqhOi8-i98xxmCggNjde4oaW6wkJ2NgM3Ss9SOX9zS3lcVzdCMdum-RwVJ301kbin4UtGztuzJBeg5oVN00MGxjC2xWwyI0tgXVs-zJs5WlafCuGfX1HrVkIf5bvpE0MQCSjdJpSeVao6-RSTYDajZf7T88a2eVjeW31mMAg-jzAWfUrii61T_bYPJFOXW8kkRWoa1InLRdG6bKB9wQs9-VdXZP60Q4Yuj_WZ-lO7qV9AEFrUkkjpaDgZT86w2g',
]);
// The serializer manager. We only use the JWS Compact Serialization Mode.
$serializerManager = new JWSSerializerManager([
new CompactSerializer(),
]);
// The input we want to check
$token = 'eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MDc4OTY5OTIsIm5iZiI6MTUwNzg5Njk5MiwiZXhwIjoxNTA3OTAwNTkyLCJpc3MiOiJNeSBzZXJ2aWNlIiwiYXVkIjoiWW91ciBhcHBsaWNhdGlvbiJ9.eycp9PTdgO4WA-68-AMoHPwsKDr68NhjIQKz4lUkiI0';
// We try to load the token.
$jws = $serializerManager->unserialize($token);
// We verify the signature. This method does NOT check the header.
// The arguments are:
// - The JWS object,
// - The key,
// - The index of the signature to check. See
$isVerified = $jwsVerifier->verifyWithKey($jws, $jwk, 0);
<?php
use Jose\Component\Signature\JWSLoader;
$jwsLoader = new JWSLoader(
$serializerManager,
$jwsVerifier,
$headerCheckerManager
);
$jws = $jwsLoader->loadAndVerifyWithKey($token, $jwk, $signature, $payload);
<?php
use Jose\Component\Signature\JWSLoaderFactory;
$jwsLoaderFactory = new JWSLoaderFactory(
$jwsSerializerManagerFactory,
$jwsVerifierFactory,
$headerCheckerManagerFactory
);
$jwsLoader = $jwsLoaderFactory->create(
['jws_compact'], // List of serializer aliases
['HS256'], // List of signature algorithm aliases
['alg'] // Optional list of header checker aliases
);
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Encryption\Algorithm\KeyEncryption\A256KW;
use Jose\Component\Encryption\Algorithm\ContentEncryption\A256CBCHS512;
//use Jose\Component\Encryption\Compression\CompressionMethodManager;
//use Jose\Component\Encryption\Compression\Deflate;
use Jose\Component\Encryption\JWEDecrypter;
// The key encryption algorithm manager with the A256KW algorithm.
$keyEncryptionAlgorithmManager = new AlgorithmManager([
new A256KW(),
]);
// The content encryption algorithm manager with the A256CBC-HS256 algorithm.
$contentEncryptionAlgorithmManager = new AlgorithmManager([
new A256CBCHS512(),
]);
// The compression method manager with the DEF (Deflate) method.
//$compressionMethodManager = new CompressionMethodManager([
// new Deflate(),
//]);
// We instantiate our JWE Decrypter.
$jweDecrypter = new JWEDecrypter(
$keyEncryptionAlgorithmManager,
$contentEncryptionAlgorithmManager,
//$compressionMethodManager
);
<?php
use Jose\Component\Core\JWK;
use Jose\Component\Encryption\Serializer\JWESerializerManager;
use Jose\Component\Encryption\Serializer\CompactSerializer;
// Our key.
$jwk = new JWK([
'kty' => 'oct',
'k' => 'dzI6nbW4OcNF-AtfxGAmuyz7IpHRudBI0WgGjZWgaRJt6prBn3DARXgUR8NVwKhfL43QBIU2Un3AvCGCHRgY4TbEqhOi8-i98xxmCggNjde4oaW6wkJ2NgM3Ss9SOX9zS3lcVzdCMdum-RwVJ301kbin4UtGztuzJBeg5oVN00MGxjC2xWwyI0tgXVs-zJs5WlafCuGfX1HrVkIf5bvpE0MQCSjdJpSeVao6-RSTYDajZf7T88a2eVjeW31mMAg-jzAWfUrii61T_bYPJFOXW8kkRWoa1InLRdG6bKB9wQs9-VdXZP60Q4Yuj_WZ-lO7qV9AEFrUkkjpaDgZT86w2g',
]);
// The serializer manager. We only use the JWE Compact Serialization Mode.
$serializerManager = new JWESerializerManager([
new CompactSerializer(),
]);
// The input we want to decrypt
$token = 'eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwiemlwIjoiREVGIn0.9RLpf3Gauf05QPNCMzPcH4XNBLmH0s3e-YWwOe57MTG844gnc-g2ywfXt_R0Q9qsR6WhkmQEhdLk2CBvfqr4ob4jFlvJK0yW.CCvfoTKO9tQlzCvbAuFAJg.PxrDlsbSRcxC5SuEJ84i9E9_R3tCyDQsEPTIllSCVxVcHiPOC2EdDlvUwYvznirYP6KMTdKMgLqxB4BwI3CWtys0fceSNxrEIu_uv1WhzJg.4DnyeLEAfB4I8Eq0UobnP8ymlX1UIfSSADaJCXr3RlU';
// We try to load the token.
$jwe = $serializerManager->unserialize($token);
// We decrypt the token. This method does NOT check the header.
$success = $jweDecrypter->decryptUsingKey($jwe, $jwk, 0);
<?php
use Jose\Component\Checker\HeaderCheckerManager;
use Jose\Component\Encryption\JWELoader;
$headerCheckerManager = new HeaderCheckerManager(
// Provide the allowed algorithms using the previously created
// AlgorithmManager.
[
new AlgorithmChecker(
$keyEncryptionAlgorithmManager->list()
)
],
// Provide the appropriate TokenTypeSupport[].
[
new JWETokenSupport(),
]
);
$jweLoader = new JWELoader(
$serializerManager,
$jweDecrypter,
$headerCheckerManager
);
$jwe = $jweLoader->loadAndDecryptWithKey($token, $key, $recipient);
$payload = $jwe->getPayload();
<?php
use Jose\Component\Encryption\JWELoaderFactory;
$jweLoaderFactory = new JWELoaderFactory(
$jweSerializerManagerFactory,
$jweDecrypterFactory,
$headerCheckerManagerFactory
);
$jweLoader = $jweLoaderFactory->create(
['jwe_compact'], // List of serializer aliases
['A128KW'], // List of key encryption algorithm aliases
['A128KW'], // List of content encryption algorithm aliases
//['DEF'], // List of compression method aliases
['alg', 'enc'] // Optional list of header checker aliases
);
Main Algorithms
Key Encryption
Algorithm
Additional header parameter
A128KW
A192KW
A256KW
No
A128GCMKW
A192GCMKW
A256GCMKW
iv: (initialization vector) this value is the base64url-encoded representation of the 96-bit IV value used for the key encryption operation.
tag: (authentication tag) the value is the base64url-encoded representation of the 128-bit Authentication Tag value resulting from the key encryption operation.
dir
No
ECDH-ES
ECDH-ES+A128KW
ECDH-ES+A192KW
ECDH-ES+A256KW
epk: (ephemeral public key) value created by the originator.
ECDH-SS
ECDH-SS+A128KW
ECDH-SS+A192KW
ECDH-SS+A256KW
No
PBES2-HS256+A128KW
PBES2-HS384+A192KW
PBES2-HS512+A256KW
Please note that the additional header parameters MUST be present and MUST be understood. Depending on the algorithm you use, you may be required to check headers BEFORE the decryption operation. Please create a custom Header Checker for theses parameters.
The algorithms ECDH-ES* are not recommended unless used with the OKP key type.
Experimental Algorithms
The following algorithms are experimental and must not be used in production unless you know what you are doing. They are proposed for testing purpose only.
Key Encryption
Algorithm
Description
A128CTR
A192CTR
A256CTR
AES CTR based encryption
Chacha20+Poly1305
Please note that this algorithm requires OpenSSL 1.1
RSA-OAEP-384
RSA-OAEP-512
Same algorithm as RSA-OAEP-256 but with SHA-384 and SHA-512 hashing functions
By default, PBES2* algorithms use the following parameter values:
Salt size: 64 bytes (512 bits)
Count: 4096
You may need to use other values. This can be done during the instantiation of the algorithm:
Example with 16 bytes (128 bits) salt and 1024 counts:
Detached Payload
As per the RFC7519,the payload of a JWS may be detached. This framework supports this feature.
JWS Creation
There is not much difference between the creation of a JWS with or without detached payload. The following example comes from the JWS Creation page. There is only one argument that will change during the call of withPayload.
And voilà ! When you will serialize this token, the payload will not be present.
JWS Loading
The loading of a signed token with a detached payload is as easy as when the payload is attached. The only difference is that you have to pass the payload to the JWS Verifier when you want to check the signature.
Events
How to use Symfony events
With the Symfony Bundle, you will be able to listen or subscribe to events.
All events can be found in the class Jose\Bundle\JoseFramework\Event\Events.
As well as the signed tokens, the encrypted tokens also have unprotected header. But with one difference: there are two unprotected headers:
Shared unprotected header applicable to all recipients.
Per-recipient unprotected header.
With the example below, we will create an encrypted token for two recipient and some unprotected header parameters:
The variable $jwe will be a valid JWE object built for two recipients. The unprotected header parameter author is applicable to the whole token while message and description are available only for the first and second recipient respectively.
Note: when an unprotected header is set, the Compact Serialization mode is not available.
From v3.x to v4.0
Contrary to upgrade a minor version (where the middle number changes) where no difficulty should be encountered, upgrade a major version (where the first number changes) is subject to significant modifications.
Update the libraries
First of all, you have to make sure you are using the last v3.x release.
Spot deprecations
Next, you have to verify you don’t use any deprecated class, interface, method or property. If you have PHPUnit tests, .
List of deprecations:
Deprecated Packages
The following packages are deprecated. Please install web-token/jwt-library or web-token/jwt-experimental instead.
In addtion to the new library or experimental packages, you may need to require third party dependencies such as ext-sodium, ext-openssl or .
web-token/jwt-util-ecc
web-token/jwt-key-mgmt
web-token/jwt-core
PSR-20 Clock
In previous versions, the classes that requires time used the PHP time function directly. It is now required to use a PSR-20 Clock implementation and pass it to the classes.
Jose\Component\Checker\ExpirationTimeChecker
Jose\Component\Checker\IssuedAtChecker
Jose\Component\Checker\NotBeforeChecker
For version 3.2.0+ and the Symfony Bundle, an internal implementation service named jose.internal_clock existed and is removed.
Simplified Algorithm Manager
Classes Jose\Component\Encryption\JWEBuilder and Jose\Component\Encryption\JWEDecrypter no longer need the Key Encryption and Content Encryption Algorithm Managers. You pass only one Algorithm Manager to the contructor.
Version Bumped
All previous major release of the following packages are not supported anymore. Please make sure your platform can use them.
PHP: 8.3+
brick/math: 0.12+
symfony/*: 7.0+ (expect symfony/polyfill-*
Upgrade the dependencies and the libraries
Please install this version or a newer one on your platform. Make sure the extensions are also installed. They are namely JSON and MBString. You may also need OpenSSL or Sodium depending on the algorithms you want to use.
It is now time to upgrade the libraries. In your composer.json, change all web-token/* dependencies from v3.x to v4.0. When done, execute composer update.
You can also update all other dependencies if needed. You can list upgradable libraries by calling composer outdated. This step is not mandatory, but highly recommended.
is a very nice tool from code upgrade. We highly recommend it.
Key (JWK)
You can create a JWK object using two static methods:
new JWK(array $values): creates a JWK using direct values.
JWK::createFromJson(string $json): creates a JWK using a JSON object.
Custom Algorithm
This framework provides dozens of signature or encryption algorithms. If your application uses a custom algorithm or if another algorithm has been recently approved in the JWT context, it may be interesting to use it with this framework.
Hopefully, this is very easy. In the following example, we will create a class to use the ChaCha20 + Poly 1305 (IETF variant) encryption algorithm as a Key Encryption Algorithm.
Key Management (JWK)
Keys As Services
When the component is installed, you will be able to define your keys in your application configuration and load your keys from several sources or formats. All these methods have the following option:
is_public
The Framework
The JWT Framework is the main package and contains all features and components described in this documentation. It is split in several sub-packages depending on your needs.
web-token/jwt-framework: The complete framework
web-token/jwt-library: The library and all its components
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Encryption\Algorithm\KeyEncryption\A128KW;
use Jose\Component\Encryption\Algorithm\KeyEncryption\PBES2HS256A128KW;
use Jose\Component\Encryption\Algorithm\ContentEncryption\A128CBCHS256;
$algorithmManager = new AlgorithmManager([
new A128KW(),
new PBES2HS256A128KW(),
new A128CBCHS256(),
]);
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Encryption\Algorithm\KeyEncryption\PBES2HS256A128KW;
$algorithmManager = new AlgorithmManager([
new PBES2HS256A128KW(16, 1024),
]);
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\JWSBuilder;
// The algorithm manager with the HS256 algorithm.
$algorithmManager = new AlgorithmManager([
new HS256(),
]);
// Our key.
$jwk = new JWK([
'kty' => 'oct',
'k' => 'dzI6nbW4OcNF-AtfxGAmuyz7IpHRudBI0WgGjZWgaRJt6prBn3DARXgUR8NVwKhfL43QBIU2Un3AvCGCHRgY4TbEqhOi8-i98xxmCggNjde4oaW6wkJ2NgM3Ss9SOX9zS3lcVzdCMdum-RwVJ301kbin4UtGztuzJBeg5oVN00MGxjC2xWwyI0tgXVs-zJs5WlafCuGfX1HrVkIf5bvpE0MQCSjdJpSeVao6-RSTYDajZf7T88a2eVjeW31mMAg-jzAWfUrii61T_bYPJFOXW8kkRWoa1InLRdG6bKB9wQs9-VdXZP60Q4Yuj_WZ-lO7qV9AEFrUkkjpaDgZT86w2g',
]);
// We instantiate our JWS Builder.
$jwsBuilder = new JWSBuilder(
$algorithmManager
);
// The payload we want to sign
$payload = json_encode([
'iat' => time(),
'nbf' => time(),
'exp' => time() + 3600,
'iss' => 'My service',
'aud' => 'Your application',
]);
$jws = $jwsBuilder
->create() // We want to create a new JWS
->withPayload($payload, true) // /!\ Here is the change! We set the payload and we indicate it is detached
->addSignature($jwk, ['alg' => 'HS256']) // We add a signature with a simple protected header
->build();
web-token/jwt-bundle: The Symfony bundle
web-token/jwt-experimental: All experimental features
We highly recommend the use of the dedicated package instead installing the whole JWT Framork
Other packages mentioned in the previous documentation version are deprecated and grouped under the web-token/jwt-library.
Please make sure all dependencies such as OpenSSL, libSodium, AESKW library and so on are kept.
p2s: (PBES2 salt input) encodes a Salt Input value, which is used as part of the PBKDF2 salt value.
p2c: (PBES2 count) contains the PBKDF2 iteration count, represented as a positive JSON integer.
To install the application, you just have to download the PHAR file attached to the release you need.
You are free to distribute the file within your project or use it locally (e.g. /usr/local/bin).
To use it, just execute the following line:
./jose.phar
You may need to set it as executable: chmod +x jose.phar
Hereafter all methods available for a JWK object. The variable $jwk is a valid JWK object.
Please note a JWK object is an immutable object. If you change a value using a setter, it will return a new object.
Generate A New Key
This framework is able to create private and public keys on the fly using the JWKFactory. 4 types of keys are supported:
Symmetric Key:
oct: octet string
Asymmetric Key:
RSA: RSA key pair
EC : Elliptic Curve key pair
OKP: Octet key pair
The none algorithm needs a key of type none. This is a specific key type that must only be used with this algorithm.
Octet String
The following example will show you how to create an oct key.
Additional parameters will be set to limit the scope of this key (e.g. signature/verification only with the HS256 algorithm).
If you already have a shared secret, you can use it to create an oct key:
RSA Key Pair
The following example will show you how to create a RSA key.
The key size must be of 384 bits at least, but nowadays the recommended size is 2048 bits.
Elliptic Curve Key Pair
The following example will show you how to create a EC key.
The supported curves are:
P-256
P-384
P-521 (note that this is 521 and not 512)
Octet Key Pair
The following example will show you how to create a OKP key.
The supported curves are:
Ed25519 for signature/verification only
X25519 for encryption/decryption only
None Key
The none key type is a special type used only for the none algorithm.
Create Key From External Sources
From Values
In case you already have key values, you can create a key by passing those values as an argument:
From A Key File
You can convert a PKCS#1 or PKCS#8 key file into a JWK. The following method supports PEM and DER formats. Encrypted keys are also supported.
From A PKCS#12 Certificate
You can convert a PKCS#12 Certificate into a JWK. Encrypted certificates are also supported.
From A X.509 Certificate
You can convert a X.509 Certificate into a JWK.
Please note that X.509 certificates only contains public keys.
: set the service public or private.
The key configuration will look like as follow:
The key will be available as a container service with the ID jose.key.key_name where key_name is the unique name of your key. Each key service will be an instance of the Jose\Component\Core\JWK class.
As any other configuration values, you can use environment variables.
From A Shared Secret
This method will directly get a shared secret.
From A JWK Object
This method will directly load a JWK object.
From A X509 Certificate File
This method will load a X509 Certificate file.
From A X509 Certificate
This method will load a key from a X509 Certificate.
From A PKCS#1/PKCS#8 Key File
This method will load a key from a PKCS#1 or PKCS#8 key file.
From A Key In A Key Set
This method will retrieve a key from a JWKSet service.
Custom Tags
You can add custom tags and attributes to the services you create.
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\JWSVerifier;
use Jose\Component\Signature\Serializer\JWSSerializerManager;
use Jose\Component\Signature\Serializer\CompactSerializer;
// The algorithm manager with the HS256 algorithm.
$algorithmManager = new AlgorithmManager([
new HS256(),
]);
// Our key.
$jwk = new JWK([
'kty' => 'oct',
'k' => 'dzI6nbW4OcNF-AtfxGAmuyz7IpHRudBI0WgGjZWgaRJt6prBn3DARXgUR8NVwKhfL43QBIU2Un3AvCGCHRgY4TbEqhOi8-i98xxmCggNjde4oaW6wkJ2NgM3Ss9SOX9zS3lcVzdCMdum-RwVJ301kbin4UtGztuzJBeg5oVN00MGxjC2xWwyI0tgXVs-zJs5WlafCuGfX1HrVkIf5bvpE0MQCSjdJpSeVao6-RSTYDajZf7T88a2eVjeW31mMAg-jzAWfUrii61T_bYPJFOXW8kkRWoa1InLRdG6bKB9wQs9-VdXZP60Q4Yuj_WZ-lO7qV9AEFrUkkjpaDgZT86w2g',
]);
// The serializer manager. We only use the JWS Compact Serialization Mode.
$serializerManager = new JWSSerializerManager([
new CompactSerializer(),
]);
// We instantiate our JWS Verifier.
$jwsVerifier = new JWSVerifier($algorithmManager);
// The detached payload
$payload = '{"iat":1507896992,"nbf":1507896992,"exp":1507900592,"iss":"My service","aud":"Your application"}';
// The input we want to check
$token = 'eyJhbGciOiJIUzI1NiJ9..eycp9PTdgO4WA-68-AMoHPwsKDr68NhjIQKz4lUkiI0';
// We try to load the token.
$jws = $serializerManager->unserialize($token);
// We verify the signature.
// /!\ The third argument is the detached payload.
$jwsVerifier->verifyWithKey($jws, $jwk, $payload);
App\EventSubscriber\JwsSubscriber.php
<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use Jose\Bundle\JoseFramework\Event\Events;
use Jose\Bundle\JoseFramework\Event\JWSBuiltSuccessEvent;
use Jose\Bundle\JoseFramework\Event\JWSVerificationFailureEvent;
use Jose\Bundle\JoseFramework\Event\JWSVerificationSuccessEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class JwsSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
Events::JWS_VERIFICATION_SUCCESS => ['onJwsVerificationSuccess'],
Events::JWS_VERIFICATION_FAILURE => ['onJwsVerificationFailure'],
Events::JWS_BUILT_SUCCESS => ['onJwsBuiltSuccess'],
Events::JWS_BUILT_FAILURE => ['onJwsBuiltFailure'],
];
}
public function onJwsVerificationSuccess(JWSVerificationSuccessEvent $event): void
{
// Do something here
}
public function onJwsVerificationFailure(JWSVerificationFailureEvent $event): void
{
// Do something here
}
public function onJwsBuiltSuccess(JWSBuiltSuccessEvent $event): void
{
// Do something here
}
public function onJwsBuiltFailure(JWSBuiltFailureEvent $event): void
{
// Do something here
}
}
<?php
declare(strict_types=1);
namespace Acme\Algorithm;
use Base64Url\Base64Url;
use Jose\Component\Core\JWK;
use const Sodium\CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES;
/**
* This algorithm is a custom algorithm that use the ChaCha20 + Poly 1305 with a 192 bits nonce (IETF variant).
*/
final class ChaCha20Poly1305IETF implements KeyEncryption //The algorithm acts as a Key Encryption algorithm
{
/**
* {@inheritdoc}
*/
public function name(): string
{
return 'ChaCha20+Poly1305+IETF'; // The name of our algorithm. This name will be used in our JWE headers
}
/**
* {@inheritdoc}
*/
public function allowedKeyTypes(): array
{
return ['oct']; // Key types for this algorithm are octet keys
}
/**
* {@inheritdoc}
*/
public function encryptKey(JWK $key, string $cek, array $completeHeader, array &$additionalHeader): string
{
$this->checkKey($key); // We check the key
$kek = Base64Url::decode($key->get('k')); // We retrieve the secret
$nonce = random_bytes(CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES); // We create a nonce
$additionalHeader['nonce'] = Base64Url::encode($nonce); // We add the nonce to the header
return sodium_crypto_aead_chacha20poly1305_ietf_encrypt($cek, '', $nonce, $kek); // We return the encrypted CEK
}
/**
* {@inheritdoc}
*/
public function decryptKey(JWK $key, string $encrypted_cek, array $header): string
{
$this->checkKey($key); // We check the key
$this->checkAdditionalParameters($header); // We verify the nonce is in the headers
$nonce = Base64Url::decode($header['nonce']); // We retrieve the nonce
$kek = Base64Url::decode($key->get('k')); // an the secret
$decrypted = sodium_crypto_aead_chacha20poly1305_ietf_decrypt($encrypted_cek, '', $nonce, $kek); // We try to decrypt the CEK
if (false === $decrypted) { // If it fails we throw an exception
throw new \RuntimeException('Unable to decrypt.');
}
return $decrypted; // Otherwise we return the decrypted CEK
}
/**
* @param JWK $key
*/
protected function checkKey(JWK $key)
{
if (!in_array($key->get('kty'), $this->allowedKeyTypes())) {
throw new \InvalidArgumentException('Wrong key type.');
}
if (!$key->has('k')) {
throw new \InvalidArgumentException('The key parameter "k" is missing.');
}
}
/**
* @param array $header
*/
protected function checkAdditionalParameters(array $header)
{
foreach (['nonce'] as $k) {
if (!array_key_exists($k, $header)) {
throw new \InvalidArgumentException(sprintf('Parameter "%s" is missing.', $k));
}
}
}
/**
* {@inheritdoc}
*/
public function getKeyManagementMode(): string
{
return self::MODE_ENCRYPT; //Key Management Mode is 'enc'.
}
}
<?php
// Check if the key has a parameter.
$jwk->has('kty');
// Retrieve the key parameter.
$jwk->get('kty');
// Retrieve all key parameters.
$jwk->all();
// Calculate the thumbprint of the key. Acceptable hash algorithms are those returned by the PHP function "hash_algos".
$jwk->thumbprint('sha256');
// If the key is a private key (RSA, EC, OKP), it can be converted into public:
$public_key = $jwk->toPublic();
// The JWK object can be serialized into JSON
json_encode($jwk);
<?php
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createOctKey(
1024, // Size in bits of the key. Should be at least of the same size as the hashing algorithm.
[
'alg' => 'HS256', // This key must only be used with the HS256 algorithm
'use' => 'sig' // This key is used for signature/verification operations only
]
);
<?php
use Jose\Component\KeyManagement\JWKFactory;
$jwk = JWKFactory::createFromSecret(
'My Secret Key', // The shared secret
[ // Optional additional members
'alg' => 'HS256',
'use' => 'sig'
]
);
<?php
use Jose\Component\KeyManagement\JWKFactory;
$private_key = JWKFactory::createRSAKey(
4096, // Size in bits of the key. We recommend at least 2048 bits.
[
'alg' => 'RSA-OAEP-256', // This key must only be used with the RSA-OAEP-256 algorithm
'use' => 'enc' // This key is used for encryption/decryption operations only
]);
<?php
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createECKey('P-256');
<?php
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createOKPKey('X25519');
<?php
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createNoneKey();
<?php
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createFromKeyFile(
'/path/to/my/key/file.pem', // The filename
'Secret', // Secret if the key is encrypted, otherwise null
[
'use' => 'sig', // Additional parameters
]
);
<?php
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createFromPKCS12CertificateFile(
'/path/to/my/key/file.p12', // The filename
'Secret', // Secret if the key is encrypted
[
'use' => 'sig', // Additional parameters
]
);
<?php
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createFromCertificateFile(
'/path/to/my/key/file.crt', // The filename
[
'use' => 'sig', // Additional parameters
]
);
jose: # Configuration of the JWT Framework
keys: # Configuration of the keys
key_name: # Unique key name
method_name: # Name of the method
...
is_public: true
jose:
keys:
key_name:
secret: # Method
secret: 'This is my shared secret'
additional_values:
use: 'sig'
alg: 'RS512'
When you receive a JWT (JWS or JWE), it is important to check ALL headers parameters BEFORE any other action. In case something went wrong, the token should be rejected.
This is a strong recommendation, there are known vulnerabilities on tokens that are processed without header verification.
Please note that some algorithms may use additional header parameters. Please read carefully the details on the or algorithms page.
The header parameters are checked by a Header Checker Manager. This manager can contain several header checkers.
The header parameter crit (critical) is always checked.
Even if the cypher process will check the alg/enc header parameters, it is interesting to check them before to reject tokens earlier.
Header Checker Manager
To create a header checker manager, you will need to add header checkers and at least one token type.
In the following example, we want to check the alg header parameter for the signed tokens (JWS) received by our application.
You can then call the check method.
The first parameter is the JWT to check,
The second one is the index of the signature/recipient. It could be ignored if you are not using the Flattened or General JSON Serialization Modes.
In some cases, it could be interesting to reject tokens that do not contain some mandatory header parameters. A list of mandatory parameters can be set as third argument. If one of those parameters is missing an exception is thrown, even if that header parameter have not been checked.
In the following example, an exception will be thrown if the alg, enc or crit parameters is missing.
Provided Header Checker Objects
The library provides several header checker classes you can instantiate and use at will. They are all located in the namespace Jose\Component\Checker.
Class
Header
Time-based
For time-based parameter checker classes, a PSR-20 clock object can be used and will be mandatory for 4.0+. This service is required for obtaining the current time or manipulating it in test environments.
Header Checker Manager Factory
The Header Checker Manager Factory will help you to create as many Header Checker Manager as you want to fit on your application requirements.
Custom Header Checker
With the previous examples, we will only check the alg (algorithm) header parameter. But your application may use other header parameters e.g. cty, typ...
The following header checkers are provided:
AlgorithmChecker: checks the alg header parameter.
AudienceChecker: checks the aud header parameter. This is a replicated claim as per the
If you need, you can create you own header checker. It must implement the interface Jose\Component\Checker\HeaderChecker. In the following example, we will check that the protected header parameter custom is an array with value foo or bar.
Encrypted Tokens
Encryption algorithms are automatically handled by the Algorithm Manager Factory.
You can use symfony/serializer to serialize/unserialize your tokens:
Standalone Application
Installation
To install the standalone application, you have to clone the repository and install the dependencies.
We consider that git and composer are correctly installed.
git clone https://github.com/web-token/jwt-app.git
cd jwt-app
composer install --no-dev --optimize-autoloader --classmap-authoritative
You will find the standalone console command in the bin folder.
IssuerChecker
iss
No
NotBeforeChecker
nbf
Yes
UnencodedPayloadChecker
b64
No
CallableChecker
Generic object that can execute a callable for checking a particular parameter
No
IsEqualChecker
Generic object that can compare a particular parameter to a predefined value
No
UnencodedPayloadChecker: checks the b64 header parameter. See unencoded payload for more information.
You may want to set data in a token header that are not important for your application (e.g. general information). The integrity protection of the data is therefore not needed at all.
The RFC7515 introduces an unprotected header. This header is supported by this framework.
With the example below, we will create a signed token with some unprotected header parameters:
The variable $jws will be a valid JWS object with one signature and both headers.
Note: when an unprotected header is set, the Compact Serialization mode is not available.
<?php
use Jose\Component\Checker\HeaderCheckerManager;
use Jose\Component\Checker\AlgorithmChecker;
use Jose\Component\Signature\JWSTokenSupport;
$headerCheckerManager = new HeaderCheckerManager(
[
new AlgorithmChecker(['HS256']),
// We want to verify that the header "alg" (algorithm)
// is present and contains "HS256"
],
[
new JWSTokenSupport(), // Adds JWS token type support
]
);
<?php
use Jose\Component\Checker\HeaderCheckerManagerFactory;
use Jose\Component\Checker\AlgorithmChecker;
use Jose\Component\Encryption\JWETokenSupport;
use Jose\Component\Signature\JWSTokenSupport;
$headerCheckerManagerFactory = new HeaderCheckerManagerFactory();
$headerCheckerManagerFactory->add('signature_alg', new AlgorithmChecker(['HS256']));
$headerCheckerManagerFactory->add('key_encryption_alg', new AlgorithmChecker(['RSA1_5']));
$headerCheckerManagerFactory->addTokenTypeSupport(new JWSTokenSupport());
$headerCheckerManagerFactory->addTokenTypeSupport(new JWETokenSupport());
$headerCheckerManagerForSignatures = $headerCheckerManagerFactory->create(['signature_alg']);
$headerCheckerManagerForEncryption = $headerCheckerManagerFactory->create(['key_encryption_alg']);
Acme\Checker\CustomChecker.php
<?php
namespace Acme\Checker;
use Jose\Component\Checker\HeaderChecker;
use Jose\Component\Checker\InvalidHeaderException;
final class CustomChecker implements HeaderChecker
{
public function checkHeader($value)
{
if (!is_array($value) || !in_array($value, ['foo', 'bar'], true)) {
throw new InvalidHeaderException('Invalid header "custom".', 'custom', $value);
}
}
// This header parameter name.
public function supportedHeader(): string
{
return 'custom';
}
// This method indicates if this parameter must be in the protected header or not.
public function protectedHeaderOnly(): bool
{
return true;
}
}
JWS creation
JWS Builder Factory Service
A JWSBuilderFactory is available as a service in your application container:
<?php
use Jose\Component\Signature\JWSBuilderFactory;
$jwsBuilderFactory = $container->get(JWSBuilderFactory::class);
With this factory, you will be able to create the JWSBuilder you need:
You can now use the JWSBuilder as explained in the JWS Creation section.
JWS Builder As Service
There is also another way to create a JWSBuilder object: using the bundle configuration.
With the previous configuration, the bundle will create a public JWS Builder service named jose.jws_builder.builder1 with selected signature algorithms.
Custom Tags
You can add custom tags and attributes to the services you create.
JWS serializers
JWS Serializer Manager Factory Service
A JWSSerializerManagerFactory is available as a service in your application container:
With this factory, you will be able to create the JWSSerializerManager you need:
You can now use the JWSSerializerManager as explained in the JWS Creation/Loading section.
Available JWS serialization modes are:
jws_compact
jws_json_general
jws_json_flattened
JWS Serializer Manager As Service
There is also another way to create a JWSSerializerManager object: using the bundle configuration.
With the previous configuration, the bundle will create a public JWS Serializer Manager service named jose.jws_serializer.serializer1 with selected serialization modes.
Custom Tags
You can add custom tags and attributes to the services you create.
Console
The project comes with console commands.
They are available:
JWE Creation
The computation of a JWE is done by the JWEBuilder object. This object requires the following services:
an algorithm manager with key encryption algorithms
an algorithm manager with content encryption algorithms
JWS verification
JWS Verifier Factory Service
A JWSVerifierFactory is available as a service in your application container:
With this factory, you will be able to create the JWSVerifier you need:
You can now use the JWSVerifier as explained in the JWS Creation section.
Algorithm Management (JWA)
For each cryptographic operation, you will need at least one algorithm and one key.
The algorithm list depends on the cypher operation to be performed (signature or encryption).
These algorithms are managed by an Algorithm Manager. In the following example, we will create an algorithm manager that will handle two algorithms: PS256 and ES512. See or algorithm pages for more information.
<?php
use Jose\Component\Signature\JWSSerializerManagerFactory;
$jwsSerializerManagerFactory = $container->get(JWSSerializerManagerFactory::class);
In the following example, we will call commands using ./jose.phar. If you need more information about a command, call the command with the option --help.
You can save the output in a file e.g. when you want to store a key or keyset in your local filesystem.
Key Management Commands
Private Key To Public Key Converter
This command will convert a private key into a public key. It has no effect on shared keys (e.g. oct keys).
Key Analyze
The following command will analyze the key passed as argument and find issues.
PKCS#1 Key Converter
This command will convert a RSA or EC key into PKCS#1 key.
Key Generators
The key generator commands will generate a private or shared key. The following options are available:
-u or --use: indicates the usage of the key (sig or enc): --use enc. This option is highly recommended.
-a or --alg: indicates the algorithm to be used with the key: --alg RSA-OAEP-256. This option is highly recommended.
Elliptic Curve Key
This command will generate an Elliptic Curve key (EC). The supported curves are P-256, P-384 and P-521.
RSA Key
This command will generate a RSA key. The key size must be at least 384 bits. Recommended size is 2048 bits or more.
Octet Key
This command will generate a octet key (oct). Recommended size is 128 bits or more.
Octet Key Pair Key
This command will generate a octet key pair key (OKP). Supported curves are X25519 (for encryption only) and Ed25519 (signature only).
None Key
This command will generate a none key. This key type is only used by the none algorithm. Key parameters alg and use are automatically set.
From An Existing Secret
If you already have a secret, you can use it to create an octet key (oct).
In case your secret is binary string, you will have to encode it first (Base64) and indicate it is encoded.
Key Loaders
The key loader commands will loader keys from various sources. The following options are available:
-u or --use: indicates the usage of the key (sig or enc): --use enc. This option is highly recommended.
-a or --alg: indicates the algorithm to be used with the key: --alg RSA-OAEP-256. This option is highly recommended.
Convert From PEM/DER Keys
This command can load and convert a DER/PEM key file into a JWK. It supports encrypted keys as well as PKCS#1 and PKCS#8 encodings or public/private keys.
Convert From PKCS#12 Keys
This command can load and convert a PKCS#12 key file into a JWK. It supports encrypted keys.
Convert From A X.509 Certificate
This command can load and convert a X.509 key file into a JWK.
RSA Key Optimization
This command optimizes a RSA key by calculating additional primes (CRT). The following option is available:
RSA keys generated by this framework are already optimized. This command may be needed when you import RSA keys from external sources.The optimization is not mandatory but highly recommended. cryptographic operations are up to 10 times faster.
Key Thumbprint
This command will calculate the key thumbprint as per the RFC7638. The following options are available:
--hash: the hashing method. Default is sha256. Supported methods are the one listed by hash_algos.
Keyset Management Commands
Private Keys To Public Keys Converter
This command has the same affect as key:convert:public except that it will convert all keys in the keyset. It has no effect on shared keys (e.g. oct keys).
Key Analyze
This command has the same behaviour as key:analyze except that it will analize all keys in the keyset.
Keyset Generators
The key set generator commands will generate key sets with random keys of the same type.
These commands have the same options as the key generator commands. The only difference is that you have to indicate the number of keys you want in the key set.
a compression method manager. No compression method is needed if you do not intent to compress the payload.
Compression is not recommended. Please avoid its use. See RFC8725 for more information.
Now let's create our first JWE object.
Great! If everything is fine you will get a JWE object with one recipient. We want to send it to the audience. Before that, it must be serialized.
We will use the compact serialization mode. This is the most common mode as it is URL safe and very compact. Perfect for a use in a web context!
All good! The variable $token now contains a string that should be something like that:
use Jose\Component\Encryption\Serializer\CompactSerializer;
$serializer = new CompactSerializer(); // The serializer
$token = $serializer->serialize($jwe, 0); // We serialize the recipient at index 0 (we only have one recipient).
Reminder: it is important to check the token headers. See the checker section of this documentation.
JWS Verifier As Service
There is also another way to create a JWSVerifier object: using the bundle configuration.
With the previous configuration, the bundle will create a public JWS Verifier service named jose.jws_verifier.verifier1 with selected signature algorithms.
Custom Tags
You can add custom tags and attributes to the services you create.
JWS Loader Service
The JWSLoaderFactory is available as a public service. You can retrieve it using the container or inject it into your services. It will help you to create JWSLoader objects on demand.
You can also create JWSLoader objects as services using the configuration of the bundle.
It is not possible to set the same algorithm twice in the same algorithm manager.
Algorithm Manager Factory
Your application may need several algorithm managers for several use cases. For example you application may use JWT for:
signed events,
authentication tokens.
To avoid mixing algorithms in one algorithm manager or instantiate several times the same algorithms, this framework provides an Algorithm Manager Factory.
This factory will create algorithm managers on demand. It allows the same algorithm to be instantiated multiple times but with different configuration options.
Each algorithm is identified using an alias.
The first argument of the method add is the alias for the algorithm. It must be unique. In general, this alias corresponds to the algorithm name.
As you can see in the example, we added the algorithm PBES2-HS512+A256KW twice:
with the default configuration,
with custom arguments.
Now our algorithm manager factory is ready. We can create several algorithm managers by passing a list of aliases to the method create:
./jose.phar key:analyze '{"kty":"oct","k":"N2aIJSQCxTo"}'
The parameter "alg" should be added.
The parameter "use" should be added.
The parameter "kid" should be added.
The key length is less than 128 bits.
./jose.phar key:load:key /path/to/file.pem "This is my secret to decrypt the key"
{"kty":"OKP","crv":"X25519","x":"TgTD7RS0KF3eU8HdTM6ACxu365uco3x2Cee9SBXiu2I","d":"BypCXV7KUai-zrwrdoAmgnHX6Kosw0sVpDVPwrXoNKY"}
./jose.phar key:load:p12 /path/to/file.p12 "This is my secret to decrypt the key"
{"kty":"OKP","crv":"X25519","x":"TgTD7RS0KF3eU8HdTM6ACxu365uco3x2Cee9SBXiu2I","d":"BypCXV7KUai-zrwrdoAmgnHX6Kosw0sVpDVPwrXoNKY"}
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Encryption\Algorithm\KeyEncryption\A256KW;
use Jose\Component\Encryption\Algorithm\ContentEncryption\A256CBCHS512;
//use Jose\Component\Encryption\Compression\CompressionMethodManager;
//use Jose\Component\Encryption\Compression\Deflate;
use Jose\Component\Encryption\JWEBuilder;
// The key encryption algorithm manager with the A256KW algorithm.
$keyEncryptionAlgorithmManager = new AlgorithmManager([
new A256KW(),
]);
// The content encryption algorithm manager with the A256CBC-HS256 algorithm.
$contentEncryptionAlgorithmManager = new AlgorithmManager([
new A256CBCHS512(),
]);
// The compression method manager with the DEF (Deflate) method.
//$compressionMethodManager = new CompressionMethodManager([
// new Deflate(),
//]);
// We instantiate our JWE Builder.
$jweBuilder = new JWEBuilder(
$keyEncryptionAlgorithmManager,
$contentEncryptionAlgorithmManager,
//$compressionMethodManager
);
use Jose\Component\Core\JWK;
// Our key.
$jwk = new JWK([
'kty' => 'oct',
'k' => 'dzI6nbW4OcNF-AtfxGAmuyz7IpHRudBI0WgGjZWgaRJt6prBn3DARXgUR8NVwKhfL43QBIU2Un3AvCGCHRgY4TbEqhOi8-i98xxmCggNjde4oaW6wkJ2NgM3Ss9SOX9zS3lcVzdCMdum-RwVJ301kbin4UtGztuzJBeg5oVN00MGxjC2xWwyI0tgXVs-zJs5WlafCuGfX1HrVkIf5bvpE0MQCSjdJpSeVao6-RSTYDajZf7T88a2eVjeW31mMAg-jzAWfUrii61T_bYPJFOXW8kkRWoa1InLRdG6bKB9wQs9-VdXZP60Q4Yuj_WZ-lO7qV9AEFrUkkjpaDgZT86w2g',
]);
// The payload we want to encrypt. It MUST be a string.
$payload = json_encode([
'iat' => time(),
'nbf' => time(),
'exp' => time() + 3600,
'iss' => 'My service',
'aud' => 'Your application',
]);
$jwe = $jweBuilder
->create() // We want to create a new JWE
->withPayload($payload) // We set the payload
->withSharedProtectedHeader([
'alg' => 'A256KW', // Key Encryption Algorithm
'enc' => 'A256CBC-HS512', // Content Encryption Algorithm
//'zip' => 'DEF' // Not recommended.
])
->addRecipient($jwk) // We add a recipient (a shared key or public key).
->build(); // We build it
<?php
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper;
...
ConfigurationHelper::addJWSLoader($container, 'jws_loader1', ['jws_compact'], ['HS256'], ['alg'], true);
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Signature\Algorithm\PS256;
use Jose\Component\Signature\Algorithm\ES512;
$algorithmManager = new AlgorithmManager([
new PS256(),
new ES512(),
]);
<?php
use Jose\Component\Core\AlgorithmManagerFactory;
use Jose\Component\Signature\Algorithm\PS256;
use Jose\Component\Encryption\Algorithm\KeyEncryption\PBES2HS512A256KW;
use Jose\Component\Encryption\Algorithm\ContentEncryption\A128CBCHS256;
$algorithmManagerFactory = new AlgorithmManagerFactory();
$algorithmManagerFactory
->add('PS256', new PS256())
->add('A128CBC-HS256', new A128CBCHS256())
->add('PBES2-HS512+A256KW', new PBES2HS512A256KW())
->add('PBES2-HS512+A256KW with custom configuration', new PBES2HS512A256KW(128, 8192))
;
You can easily check if an algorithm is fast enough on your platform. This project has tests for all operations with (almost) all algorithms.
All algorithms
To run those tests, you must install the library with all dev dependencies. When done, just run the following command:
The previous command will test ALL algorithms and may take more than 7 hours.
Algorithm Selection
We recommend you to run tests only for the algorithm(s) you want to use. Just run the following command:
The value of GROUP should be one of the following values:
All signature algorithms: JWS
All HMAC algorithms: hmac
HS256: HS256
HS384: HS384
HS512: HS512
All Elliptic Curves algorithms: ECDSA
ES256: ES256
ES384: ES384
All Edwards-curve algorithms: EdDSA
Ed25519: Ed25519
All RSA algorithms: RSASign
RS256: RS256
RS384: RS384
All key encryption algorithms: JWE
KW
Note 1: the dir algorithm is not tested as there is no key encryption or key decryption with this algorithm.
Note 2: tests consist in a full JWS/JWE creation and loading and sometimes using multiple key sizes.
Note 3: for JWE creation and loading, each key encryption algorithm is tested with the following content encryption algorithms:
A128GCM, A192GCM and A256GCM
Examples
Test all HMAC algorithms:
Test the RSA1_5 algorithm:
Benchmark Details
The result of the following command will only give you partial result. For example:
The main information is that the best algorithm takes only 31.275 µs... but you may need more information.
Just run the following command:
*The value of the --uuid option is given at the end of the previous command. You can also use latest.
A report.md file will be created. It contains a detailed report (Markdown format). Example:
If it is not enough for you, you can get a full report with the following command:
Nested Tokens
JWT can be signed or encrypted and both. A nested token is a signed token enclosed in an encrypted one. This order is very important: signed then encrypted.
The NestedTokenLoader and NestedTokenBuilder classes will help you to create nested tokens with ease. Just instal the package web-token/jwt-nested-token. It contains all the classes and dependencies will be directly managed by composer. You can install it if needed.
Nested Token Loading
To instantiate the NestedTokenLoader, you need a JWSLoader and a JWELoader.
Its use is very straightforward, you just have to call the method load using the token, the encryption and signature key sets.
The last argument ($signature in the following example) will represents the signature index of the verified signature. This is only useful when multiple signature support is used.
Nested Token Building
To instantiate the NestedTokenBuilder, you will need the following components:
a JWSBuilder,
a JWEBuilder,
a JWESerializerManager
Its use is a bit more complicated than the loading as the nested token may be designed for several recipients or may have several signatures.
As a reminder, if one of the following parameter is set, the compact serialization mode cannot be used:
signature unprotected header,
JWE shared unprotected header,
recipient unprotected header,
Symfony Bundle
Configuration
Hereafter an example of a Symfony application configuration:
This configuration will create two public services:
jose.nested_token_loader.loader_1
jose.nested_token_builder.builder_1
These services can be called from the container (unless private) or injected in your services.
Configuration Helper
As any other services, you can create a nested token loader or builder from another bundle extension. The following bundle extension class will create the same configuration and services as above.
Configuration Helper
When you want to create keys/key sets, JWS loader/verifier... services, you have to create a dedicated jose section in your configuration. It may confuse your users to configure your bundle and the Jose Framework bundle. Sometimes, you may also want to be sure that the configuration is correctly defined. Lastly, the configuration size increases with numerous details, options or service IDs and it becomes difficult to read or modify.
Hopefully, the Symfony bundle provide a configuration helper: Jose\Bundle\JoseFramework\Helper\ConfigurationHelper. This helper will configure the jose section for you. This helper has to be called in your bundle extension during the prepend step (your extension has to implement Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface).
Let say you want to create a JWK as a service:
For the key configuration, the arguments are:
The container
The name of the service (acme_my_key)
The key type (jwk)
Now a key service named jose.key.acme_my_key will be created. This service is public so you will be able to get it from your container or inject it to your services.
This is exactly the same configuration as the following one:
Other methods are:
For the jws section:
public static function addJWSBuilder(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = [])
Have a look at to see how we configure the Jose Bundle without dedicated configuration
Key Set Management (JWKSet)
Key Sets As Services
All these methods have the following common option:
is_public: set the service public or private.
The key set configuration will look like as follow:
The key set will be available as a container service with the ID jose.key_set.keyset_name where keyset_name is the unique name of your key set. Each key set service will be an instance of the Jose\Component\Core\JWKSet class.
As any other configuration values, you can use environment variables.
From A JWKSet Object
This method will directly get a JWKSet object.
Distant Key Sets
You can load key sets shared by a distant service (e.g. Google, Microsoft, Okta...). You must install and enable the .
When done, you have to create a client and enable the JKU Factory service by indicating the request factory service to use:
Important recommendations:
It is highly recommended to use a cache plugin for your HTTP client and thus avoid unnecessary calls to the key set endpoint.
The connection must be secured and certificate verification should not be disabled.
From A JKU (JWK Url)
The following example will allow you tu load a key set from a distant URI. The key set must be a JWKSet object.
From A X5U (X509 Certificates Url)
The following example will allow you tu load a key set from a distant URI. The key set must be a list of X509 certificates.
Shared Key Sets
It can be interesting to share your key sets through an Url. This can easily achieved by adding a dedicated controller. This controller is automatically created by the bundle.
You can enable these routes by adding the following configuration to your routing file.
Then you can share your key set.
Now when you go to the URL http://128.0.0.1:8000/certs, you will get your key set.
Custom Tags
You can add custom tags and attributes to the services you create.
<?php
declare(strict_types=1);
namespace AcmeBundle\DependencyInjection;
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
final class AcmeExtension extends Extension implements PrependExtensionInterface
{
...
/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
{
... // The Helper will be called here
}
}
ES512: ES512
RS512: RS512
PS256: PS256
PS384: PS384
PS512: PS512
A128KW:
A128KW
A192KW: A192KW
A256KW: A256KW
GCMKW
A128GCMKW: A128GCMKW
A192GCMKW: A192GCMKW
A256GCMKW: A256GCMKW
ECDHES
ECDH-ES: ECDHES
ECDHESKW
ECDHESA128KW: ECDH-ES+A128KW
ECDHESA192KW: ECDH-ES+A192KW
ECDHESA256KW: ECDH-ES+A256KW
PBES2
PBES2-HS256+A128KW: PBES2HS256A128KW
PBES2-HS384+A192KW: PBES2HS384A192KW
PBES2-HS512+A256KW: PBES2HS512A256KW
RSAEnc
RSA1_5: RSA1_5
RSA-OAEP: RSA-OAEP
RSA-OAEP-256: RSA-OAEP-256
A128CBC-HS256, A192CBC-HS384 and A256CBC-HS512
,
a JWSSerializerManager
Additional Authenticated Data.
An array with the expected values
An array with the custom tags (optional)
public static function addJWSVerifier(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = [])
public static function addJWSSerializer(ContainerBuilder $container, string $name, array $serializers, bool $is_public = true, array $tags = [])
use Jose\Component\NestedToken\NestedTokenBuilder;
$nestedTokenBuilder = new NestedTokenBuilder($jweLoader, $jweSerializerManager, $jwsLoader, $jwsSerializerManager);
$token = $builder->create(
$payload, // The payload to protect
[[ // A list of signatures. 'key' is mandatory and at least one of 'protected_header'/'header' has to be set.
'key' => $signature_key, // The key used to sign. Mandatory.
'protected_header' => ['alg' => 'PS256'], // The protected header. Optional.
'header' => ['foo' => 'bar'], // The unprotected header. Optional.
]],
'jws_json_flattened', // The serialization mode for the JWS
['alg' => 'RSA-OAEP', 'enc' => 'A128GCM'], // The shared protected header. Optional.
['foo' => 'bar'], // The shared unprotected header. Optional.
[[ // A list of recipients. 'key' is mandatory.
'key' => $encryption_key, // The recipient key.
'header' => ['bar' => 'foo'], // The recipient unprotected header.
]],
'jwe_json_flattened' // The serialization mode for the JWE.
'1, 2, 3, 4' // Additional Authenticated Data (AAD). Optional.
);
jose: # Configuration of the JWT Framework
key_sets: # Configuration of the keys
keyset_name: # Unique key name
method_name: # Name of the method
...
is_public: true
httplug: # Example of client configuration
plugins:
cache: # We use the cache plugin
cache_pool: 'cache.app' # We use the PSR-6 Cache service of the application
config:
default_ttl: 1800 # TTL set to 30 min
clients:
acme:
factory: 'httplug.factory.guzzle6'
plugins: ['httplug.plugin.cache'] # We enable the cache plugin for that client.
jose:
jku_factory:
enabled: true
client: 'httplug.client.acme' # The Httplug client
request_factory: 'httplug.message_factory' # In general, you will use the same message factory as the one used by Httplug
jose:
key_sets:
public_keyset: # The key set we want to share
jwkset:
value: '{"keys":[{"kty":"OKP","crv":"X25519","x":"ovuZiVcMXBN4r0VgCvJy_ChAsBv4YPJGC5w56PzndXY"},{"kty":"OKP","crv":"X25519","x":"4qyOJ4T9RkdciIn6LDxb2LdM1Ov-dtBSuj0jh6nCuyc"}]}'
jwk_uris:
shared_keyset:
id: 'jose.key_set.public_keyset' # The key set service to share
path: '/certs' # Path of the key set. Final path is hostname/route_prefix/path: https://www.foo.com/keys/certs
The RFC7515 (JWS) and RFC7516 (JWE) introduce several serialization modes.
Compact
JSON Flattened
JSON General
The Compact mode is most know and commonly used as it is compact and URL safe i.e. it is designed for web context. JSON Flattened and General are not URL safe, but provides features that may fit on your application context.
JWS Serialization
JWS Compact
This serialization mode is probably the one you know the most. It it a string composed of three parts encoded in Base64 Url Safe and separated by a dot (.).
The serializer class is Jose\Component\Signature\Serializer\CompactSerializer. The associated name is jws_compact.
Example:
There are some limitations when you use this serialization mode:
Unprotected header not supported.
Unencoded payload must contain characters within the following range of ASCII characters: 0x20-0x2d and 0x2f-0x7e
JWS JSON Flattened
This serialization mode is useful when you need to use the unprotected header. It it a simple JSON object.
The serializer class is Jose\Component\Signature\Serializer\JSONFlattenedSerializer. The associated name is jws_json_flattened.
Example:
JWS JSON General
This serialization mode is similar to the JWS JSON Flattened, but may contain more than one signature. It it a JSON object.
The serializer class is Jose\Component\Signature\Serializer\JSONGeneralSerializer. The associated name is jws_json_general.
Example:
JWS Serializer Manager
The serializer manager can be helpful when your application deals more than one serialization mode.
JWE Serialization
JWE Compact
This serialization mode is probably the one you know the most. It it a string composed of five parts encoded in Base64 Url Safe and separated by a dot (.).
The serializer class is Jose\Component\Encryption\Serializer\CompactSerializer. The associated name is jwe_compact.
Example:
There are some limitations when you use this serialization mode:
No Additional Authentication Data can be used.
No shared unprotected header or per-recipient header can be used.
JWE JSON Flattened
This serialization mode is useful when you need to use the unprotected header. It it a simple JSON object.
The serializer class is Jose\Component\Encryption\Serializer\JSONFlattenedSerializer. The associated name is jwe_json_flattened.
Example:
JWE JSON General
This serialization mode is similar to the JWE JSON Flattened, but may contain more than one recipient. It it a JSON object.
The serializer class is Jose\Component\Encryption\Serializer\JSONGeneralSerializer. The associated name is jwe_json_general.
Example:
JWE Serializer Manager
The serializer manager can be helpful when your application deals more than one serialization mode.
<?php
require_once 'vendor/autoload.php';
use Jose\Component\Signature\Serializer;
$manager = Serializer\JWSSerializerManager::create([
new Serializer\CompactSerializer(),
new Serializer\JSONFlattenedSerializer(),
new Serializer\JSONGeneralSerializer(),
]);
// Serializes the second signature (index = 1) of the variable $jws (JWS object) into JSON Flattened serialization mode.
$token = $manager->serialize('jws_json_flattened', $jws, 1);
// Retrieve the JWS object from a token
$jws = $manager->unserialize($token);
<?php
require_once 'vendor/autoload.php';
use Jose\Component\Encryption\Serializer;
$manager = Serializer\JWESerializerManager::create([
new Serializer\CompactSerializer(),
new Serializer\JSONFlattenedSerializer(),
new Serializer\JSONGeneralSerializer(),
]);
// Serializes the second recipient (index = 1) of the variable $jwe (JWE object) into JSON Flattened serialization mode.
$token = $manager->serialize('jwe_json_flattened', $jwe, 1);
// Retrieve the JWE object from a token
$jwe = $manager->unserialize($token);
Result table
The table hereafter is the result of all benchmarks with our development environment. It is given to help you to select the appropriate algorithms for your application.
The use of the algorithm ECDH-ES with curves P-256, P-384 or P-521 is not recommended with PHP7.1 or 7.2. The cryptographic operations with those curves are done using a pure PHP function and hence very slow.
The use of the RSA algorithms with a very long key (more that 4096 bits) is quite slow, but offers a good protection.
The PBES2* algorithms are quite slow, but also offer a good protection (see ). Default salt size (512 bits) and iterations (4096) and custom values (256/1024) used for the tests. Those values can be configured if needed.