Key Sets (JWKSet)
As JWK, the JWKSet object is also part of the core component (web-token/jwt-core). The constructor also changed in favor of a static method.
You can now create a key set using three ways:
Direct input of values (as before)
A list of JWK objects
A Json object string that represents a key set
Other important changes:
The JWKSet object does not implement
\ArrayAccessanymore. However, you still can iterate it (e.g. usingforeach).The JWKSet is immutable. When you add a key, you will get a new object.
The method
prependKeyhas been removed.You can select a key using parameters (key type, algorithm, key ID...)
Before
<?php
use Jose\Object\JWKSet;
$keyset = new JWKSet(['keys' => [
'71ee230371d19630bc17fb90ccf20ae632ad8cf8' => [
'kid' => '71ee230371d19630bc17fb90ccf20ae632ad8cf8',
'kty' => 'RSA',
'alg' => 'RS256',
'use' => 'sig',
'n' => 'vnMTRCMvsS04M1yaKR112aB8RxOkWHFixZO68wCRlVLxK4ugckXVD_Ebcq-kms1T2XpoWntVfBuX40r2GvcD9UsTFt_MZlgd1xyGwGV6U_tfQUll5mKxCPjr60h83LXKJ_zmLXIqkV8tAoIg78a5VRWoms_0Bn09DKT3-RBWFjk=',
'e' => 'AQAB',
]]]);
json_encode($keyset); // The key as a Json object
$keyset->addKey(new JWK(['kty' => 'none'])); // Add a key
$keyset->removeKey(1); // Remove a key
$keyset->prependKey(new JWK(['kty' => 'none'])); // Prepend a key
$keyset[0]; // Access keys like arrays do
foreach ($keyset as $key) { // Iterate on a key set
...
}After
About The Key Selector
The key selector is able to find a key that fits on several requirements:
First argument: key used either for signature (
sig) or encryption (enc).Second argument: algorithm you would like to use. If the key has no
algparameter but the key type allowed by the algorithm matches, then the key may be selected.Third argument: an associated list of specific requirements. Can be any key parameter (e.g.
kidor custom parameter).
The method returns the key that matches best otherwise null.
Removed Classes
The following classes have been removed.
Jose\Object\JWKSetsJose\Object\PublicJWKSetJose\Object\StorableJWKSetJose\Object\RotatableJWKSetJose\Object\JKUJWKSetJose\Object\X5UJWKSet
There is no replacement classes. The key set modification, rotation or the loading of distant keys (JKU/X5U) should now be done
through the dedicated console/standalone application (see this page),
using the
JWKFactoryorJKUFactory,using a custom key manager.
Keys/Key Sets And The Symfony Bundle
Env Var Processor
If you use Symfony 3.4+, you will be able to load a keys and key sets using an environment variable and process it:
It the environment variables are valid keys and key sets, the associated parameters will converted as a JWK or a JWKSet object.
These parameters can be injected a usual:
Associated service configuration:
Please note that, contrary to the keys and key sets loaded through the configuration or the Configuration Helper, the one loaded through an environment variable are not listed in the Symfony Debug Toolbar.
JKUFactory / X5UFactory
Before
After
The use of this feature is drastically different. JKUFactory and X5UFactory are now services that relies on HttPlug to get the key sets.
Make sure the following dependencies are installed:
(
web-token/jwt-bundleandweb-token/jwt-key-mgmt) orweb-token/jwt-frameworkphp-http/httplug-bundleand at least one adapter (I will usephp-http/guzzle6-adapterhere)
Do not forget to enable the associated bundles:
Create a request factory service
Configure the bundles:
When done, there are two possibilities to load JKU/X5U key sets:
Inject the
Jose\Component\KeyManagement\JKUFactoryorJose\Component\KeyManagement\X5UFactoryand call theloadFromUrlmethod:
Configure a key set in the bundle configuration
The associated service will be jose.key_set.microsoft_keys.
Last updated
Was this helpful?